From dcb81a3da573afebcdf17dffe77d9d224ae6a370 Mon Sep 17 00:00:00 2001 From: Venil Noronha Date: Tue, 12 Sep 2017 19:57:36 -0700 Subject: [PATCH 1/2] Align prefix match in BufferCounterService with DefaultCounterService Closes gh-10278 --- .../metrics/buffer/BufferCounterService.java | 3 +- .../buffer/BufferCounterServiceTests.java | 91 +++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterServiceTests.java diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterService.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterService.java index 0717b3df2b4..2ae37d84eeb 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterService.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterService.java @@ -25,6 +25,7 @@ import org.springframework.lang.UsesJava8; * Fast implementation of {@link CounterService} using {@link CounterBuffers}. * * @author Dave Syer + * @author Venil Noronha * @since 1.3.0 */ @UsesJava8 @@ -62,7 +63,7 @@ public class BufferCounterService implements CounterService { if (cached != null) { return cached; } - if (metricName.startsWith("counter") || metricName.startsWith("meter")) { + if (metricName.startsWith("counter.") || metricName.startsWith("meter.")) { return metricName; } String name = "counter." + metricName; diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterServiceTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterServiceTests.java new file mode 100644 index 00000000000..9b24d6481e4 --- /dev/null +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterServiceTests.java @@ -0,0 +1,91 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.metrics.buffer; + +import org.junit.Test; + +import org.springframework.boot.actuate.metrics.CounterService; +import org.springframework.boot.actuate.metrics.Metric; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +/** + * Standard tests for {@link BufferCounterService}. + * + * @author Venil Noronha + */ +public class BufferCounterServiceTests { + + private CounterBuffers counters = new CounterBuffers(); + + private CounterService service = new BufferCounterService(this.counters); + + private BufferMetricReader reader = new BufferMetricReader(this.counters, new GaugeBuffers()); + + @Test + public void matchExtendedPrefix() { + this.service.increment("foo"); + Metric fooMetric = this.reader.findOne("foo"); + Metric counterFooMetric = this.reader.findOne("counter.foo"); + assertNull(fooMetric); + assertNotNull(counterFooMetric); + assertEquals(1L, counterFooMetric.getValue()); + } + + @Test + public void matchCounterPrefix() { + this.service.increment("counterfoo"); + Metric counterfooMetric = this.reader.findOne("counterfoo"); + Metric counterCounterfooMetric = this.reader.findOne("counter.counterfoo"); + assertNull(counterfooMetric); + assertNotNull(counterCounterfooMetric); + assertEquals(1L, counterCounterfooMetric.getValue()); + } + + @Test + public void matchCounterDotPrefix() { + this.service.increment("counter.foo"); + Metric counterFooMetric = this.reader.findOne("counter.foo"); + Metric counterCounterFooMetric = this.reader.findOne("counter.counter.foo"); + assertNull(counterCounterFooMetric); + assertNotNull(counterFooMetric); + assertEquals(1L, counterFooMetric.getValue()); + } + + @Test + public void matchMeterPrefix() { + this.service.increment("meterfoo"); + Metric meterfooMetric = this.reader.findOne("meterfoo"); + Metric counterMeterfooMetric = this.reader.findOne("counter.meterfoo"); + assertNull(meterfooMetric); + assertNotNull(counterMeterfooMetric); + assertEquals(1L, counterMeterfooMetric.getValue()); + } + + @Test + public void matchMeterDotPrefix() { + this.service.increment("meter.foo"); + Metric meterFooMetric = this.reader.findOne("meter.foo"); + Metric counterMeterFooMetric = this.reader.findOne("counter.meter.foo"); + assertNull(counterMeterFooMetric); + assertNotNull(meterFooMetric); + assertEquals(1L, meterFooMetric.getValue()); + } + +} From aca30950cf468dfabfac28594a5508aa297c934f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 20 Sep 2017 06:51:22 +0100 Subject: [PATCH 2/2] Polish "Align prefix match in BufferCounterService with DefaultCounterService" See gh-10278 --- .../metrics/buffer/BufferCounterService.java | 2 +- .../buffer/BufferCounterServiceTests.java | 46 ++++++++----------- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterService.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterService.java index 2ae37d84eeb..93f341e57d0 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterService.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterService.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterServiceTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterServiceTests.java index 9b24d6481e4..aab0d765c5c 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterServiceTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/buffer/BufferCounterServiceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,12 +21,10 @@ import org.junit.Test; import org.springframework.boot.actuate.metrics.CounterService; import org.springframework.boot.actuate.metrics.Metric; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.assertj.core.api.Assertions.assertThat; /** - * Standard tests for {@link BufferCounterService}. + * Tests for {@link BufferCounterService}. * * @author Venil Noronha */ @@ -36,56 +34,52 @@ public class BufferCounterServiceTests { private CounterService service = new BufferCounterService(this.counters); - private BufferMetricReader reader = new BufferMetricReader(this.counters, new GaugeBuffers()); + private BufferMetricReader reader = new BufferMetricReader(this.counters, + new GaugeBuffers()); @Test public void matchExtendedPrefix() { this.service.increment("foo"); - Metric fooMetric = this.reader.findOne("foo"); + assertThat(this.reader.findOne("foo")).isNull(); Metric counterFooMetric = this.reader.findOne("counter.foo"); - assertNull(fooMetric); - assertNotNull(counterFooMetric); - assertEquals(1L, counterFooMetric.getValue()); + assertThat(counterFooMetric).isNotNull(); + assertThat(counterFooMetric.getValue()).isEqualTo(1L); } @Test public void matchCounterPrefix() { this.service.increment("counterfoo"); - Metric counterfooMetric = this.reader.findOne("counterfoo"); + assertThat(this.reader.findOne("counterfoo")).isNull(); Metric counterCounterfooMetric = this.reader.findOne("counter.counterfoo"); - assertNull(counterfooMetric); - assertNotNull(counterCounterfooMetric); - assertEquals(1L, counterCounterfooMetric.getValue()); + assertThat(counterCounterfooMetric).isNotNull(); + assertThat(counterCounterfooMetric.getValue()).isEqualTo(1L); } @Test public void matchCounterDotPrefix() { this.service.increment("counter.foo"); + assertThat(this.reader.findOne("counter.counter.foo")).isNull(); Metric counterFooMetric = this.reader.findOne("counter.foo"); - Metric counterCounterFooMetric = this.reader.findOne("counter.counter.foo"); - assertNull(counterCounterFooMetric); - assertNotNull(counterFooMetric); - assertEquals(1L, counterFooMetric.getValue()); + assertThat(counterFooMetric).isNotNull(); + assertThat(counterFooMetric.getValue()).isEqualTo(1L); } @Test public void matchMeterPrefix() { this.service.increment("meterfoo"); - Metric meterfooMetric = this.reader.findOne("meterfoo"); + assertThat(this.reader.findOne("meterfoo")).isNull(); Metric counterMeterfooMetric = this.reader.findOne("counter.meterfoo"); - assertNull(meterfooMetric); - assertNotNull(counterMeterfooMetric); - assertEquals(1L, counterMeterfooMetric.getValue()); + assertThat(counterMeterfooMetric).isNotNull(); + assertThat(counterMeterfooMetric.getValue()).isEqualTo(1L); } @Test public void matchMeterDotPrefix() { this.service.increment("meter.foo"); + assertThat(this.reader.findOne("counter.meter.foo")).isNull(); Metric meterFooMetric = this.reader.findOne("meter.foo"); - Metric counterMeterFooMetric = this.reader.findOne("counter.meter.foo"); - assertNull(counterMeterFooMetric); - assertNotNull(meterFooMetric); - assertEquals(1L, meterFooMetric.getValue()); + assertThat(meterFooMetric).isNotNull(); + assertThat(meterFooMetric.getValue()).isEqualTo(1L); } }