Skip to content

Commit dcb81a3

Browse files
venilnoronhawilkinsona
authored andcommitted
Align prefix match in BufferCounterService with DefaultCounterService
Closes gh-10278
1 parent eb2c0fa commit dcb81a3

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,91 @@
1+
/*
2+
* Copyright 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.junit.Assert.assertEquals;
25+
import static org.junit.Assert.assertNotNull;
26+
import static org.junit.Assert.assertNull;
27+
28+
/**
29+
* Standard tests for {@link BufferCounterService}.
30+
*
31+
* @author Venil Noronha
32+
*/
33+
public class BufferCounterServiceTests {
34+
35+
private CounterBuffers counters = new CounterBuffers();
36+
37+
private CounterService service = new BufferCounterService(this.counters);
38+
39+
private BufferMetricReader reader = new BufferMetricReader(this.counters, new GaugeBuffers());
40+
41+
@Test
42+
public void matchExtendedPrefix() {
43+
this.service.increment("foo");
44+
Metric<?> fooMetric = this.reader.findOne("foo");
45+
Metric<?> counterFooMetric = this.reader.findOne("counter.foo");
46+
assertNull(fooMetric);
47+
assertNotNull(counterFooMetric);
48+
assertEquals(1L, counterFooMetric.getValue());
49+
}
50+
51+
@Test
52+
public void matchCounterPrefix() {
53+
this.service.increment("counterfoo");
54+
Metric<?> counterfooMetric = this.reader.findOne("counterfoo");
55+
Metric<?> counterCounterfooMetric = this.reader.findOne("counter.counterfoo");
56+
assertNull(counterfooMetric);
57+
assertNotNull(counterCounterfooMetric);
58+
assertEquals(1L, counterCounterfooMetric.getValue());
59+
}
60+
61+
@Test
62+
public void matchCounterDotPrefix() {
63+
this.service.increment("counter.foo");
64+
Metric<?> counterFooMetric = this.reader.findOne("counter.foo");
65+
Metric<?> counterCounterFooMetric = this.reader.findOne("counter.counter.foo");
66+
assertNull(counterCounterFooMetric);
67+
assertNotNull(counterFooMetric);
68+
assertEquals(1L, counterFooMetric.getValue());
69+
}
70+
71+
@Test
72+
public void matchMeterPrefix() {
73+
this.service.increment("meterfoo");
74+
Metric<?> meterfooMetric = this.reader.findOne("meterfoo");
75+
Metric<?> counterMeterfooMetric = this.reader.findOne("counter.meterfoo");
76+
assertNull(meterfooMetric);
77+
assertNotNull(counterMeterfooMetric);
78+
assertEquals(1L, counterMeterfooMetric.getValue());
79+
}
80+
81+
@Test
82+
public void matchMeterDotPrefix() {
83+
this.service.increment("meter.foo");
84+
Metric<?> meterFooMetric = this.reader.findOne("meter.foo");
85+
Metric<?> counterMeterFooMetric = this.reader.findOne("counter.meter.foo");
86+
assertNull(counterMeterFooMetric);
87+
assertNotNull(meterFooMetric);
88+
assertEquals(1L, meterFooMetric.getValue());
89+
}
90+
91+
}

0 commit comments

Comments
 (0)