Skip to content

Commit 2bf4287

Browse files
committed
Merge pull request #10278 from Venil Noronha
* gh-10278: Polish "Align prefix match in BufferCounterService with DefaultCounterService" Align prefix match in BufferCounterService with DefaultCounterService
2 parents eb2c0fa + aca3095 commit 2bf4287

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
* Fast implementation of {@link CounterService} using {@link CounterBuffers}.
2626
*
2727
* @author Dave Syer
28+
* @author Venil Noronha
2829
* @since 1.3.0
2930
*/
3031
@UsesJava8
@@ -62,7 +63,7 @@ private String wrap(String metricName) {
6263
if (cached != null) {
6364
return cached;
6465
}
65-
if (metricName.startsWith("counter") || metricName.startsWith("meter")) {
66+
if (metricName.startsWith("counter.") || metricName.startsWith("meter.")) {
6667
return metricName;
6768
}
6869
String name = "counter." + metricName;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.metrics.buffer;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.boot.actuate.metrics.CounterService;
22+
import org.springframework.boot.actuate.metrics.Metric;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests for {@link BufferCounterService}.
28+
*
29+
* @author Venil Noronha
30+
*/
31+
public class BufferCounterServiceTests {
32+
33+
private CounterBuffers counters = new CounterBuffers();
34+
35+
private CounterService service = new BufferCounterService(this.counters);
36+
37+
private BufferMetricReader reader = new BufferMetricReader(this.counters,
38+
new GaugeBuffers());
39+
40+
@Test
41+
public void matchExtendedPrefix() {
42+
this.service.increment("foo");
43+
assertThat(this.reader.findOne("foo")).isNull();
44+
Metric<?> counterFooMetric = this.reader.findOne("counter.foo");
45+
assertThat(counterFooMetric).isNotNull();
46+
assertThat(counterFooMetric.getValue()).isEqualTo(1L);
47+
}
48+
49+
@Test
50+
public void matchCounterPrefix() {
51+
this.service.increment("counterfoo");
52+
assertThat(this.reader.findOne("counterfoo")).isNull();
53+
Metric<?> counterCounterfooMetric = this.reader.findOne("counter.counterfoo");
54+
assertThat(counterCounterfooMetric).isNotNull();
55+
assertThat(counterCounterfooMetric.getValue()).isEqualTo(1L);
56+
}
57+
58+
@Test
59+
public void matchCounterDotPrefix() {
60+
this.service.increment("counter.foo");
61+
assertThat(this.reader.findOne("counter.counter.foo")).isNull();
62+
Metric<?> counterFooMetric = this.reader.findOne("counter.foo");
63+
assertThat(counterFooMetric).isNotNull();
64+
assertThat(counterFooMetric.getValue()).isEqualTo(1L);
65+
}
66+
67+
@Test
68+
public void matchMeterPrefix() {
69+
this.service.increment("meterfoo");
70+
assertThat(this.reader.findOne("meterfoo")).isNull();
71+
Metric<?> counterMeterfooMetric = this.reader.findOne("counter.meterfoo");
72+
assertThat(counterMeterfooMetric).isNotNull();
73+
assertThat(counterMeterfooMetric.getValue()).isEqualTo(1L);
74+
}
75+
76+
@Test
77+
public void matchMeterDotPrefix() {
78+
this.service.increment("meter.foo");
79+
assertThat(this.reader.findOne("counter.meter.foo")).isNull();
80+
Metric<?> meterFooMetric = this.reader.findOne("meter.foo");
81+
assertThat(meterFooMetric).isNotNull();
82+
assertThat(meterFooMetric.getValue()).isEqualTo(1L);
83+
}
84+
85+
}

0 commit comments

Comments
 (0)