From 303cb017adb50c035b639e1fd3b311e67a407d73 Mon Sep 17 00:00:00 2001 From: rnavarropiris Date: Tue, 7 Mar 2023 16:03:59 +0100 Subject: [PATCH 1/4] Add http_method tag to micrometer metrics --- .../src/main/java/feign/metrics4/BaseMeteredClient.java | 4 ++++ .../src/main/java/feign/metrics5/BaseMeteredClient.java | 2 ++ .../src/main/java/feign/micrometer/BaseMeteredClient.java | 1 + .../test/java/feign/micrometer/AbstractMetricsTestBase.java | 2 +- 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/dropwizard-metrics4/src/main/java/feign/metrics4/BaseMeteredClient.java b/dropwizard-metrics4/src/main/java/feign/metrics4/BaseMeteredClient.java index 82810801c..a29e36c13 100644 --- a/dropwizard-metrics4/src/main/java/feign/metrics4/BaseMeteredClient.java +++ b/dropwizard-metrics4/src/main/java/feign/metrics4/BaseMeteredClient.java @@ -53,6 +53,8 @@ protected void recordSuccess(RequestTemplate template, Response response) { response.status() / 100 + "xx", "http_status", String.valueOf(response.status()), + "http_method", + template.methodMetadata().template().method(), "uri", template.methodMetadata().template().path()), metricSuppliers.meters()) @@ -72,6 +74,8 @@ protected void recordFailure(RequestTemplate template, FeignException e) { e.status() / 100 + "xx", "http_status", String.valueOf(e.status()), + "http_method", + template.methodMetadata().template().method(), "uri", template.methodMetadata().template().path()), metricSuppliers.meters()) diff --git a/dropwizard-metrics5/src/main/java/feign/metrics5/BaseMeteredClient.java b/dropwizard-metrics5/src/main/java/feign/metrics5/BaseMeteredClient.java index 24796cd14..10d015c24 100644 --- a/dropwizard-metrics5/src/main/java/feign/metrics5/BaseMeteredClient.java +++ b/dropwizard-metrics5/src/main/java/feign/metrics5/BaseMeteredClient.java @@ -52,6 +52,7 @@ protected void recordSuccess(RequestTemplate template, Response response) { httpResponseCode(template) .tagged("http_status", String.valueOf(response.status())) .tagged("status_group", response.status() / 100 + "xx") + .tagged("http_method", template.methodMetadata().template().method()) .tagged("uri", template.methodMetadata().template().path())) .inc(); } @@ -65,6 +66,7 @@ protected void recordFailure(RequestTemplate template, FeignException e) { ExceptionUtils.getRootCause(e).getClass().getSimpleName()) .tagged("http_status", String.valueOf(e.status())) .tagged("status_group", e.status() / 100 + "xx") + .tagged("http_method", template.methodMetadata().template().method()) .tagged("uri", template.methodMetadata().template().path())) .inc(); } diff --git a/micrometer/src/main/java/feign/micrometer/BaseMeteredClient.java b/micrometer/src/main/java/feign/micrometer/BaseMeteredClient.java index ed35b091d..e4cbe6e49 100644 --- a/micrometer/src/main/java/feign/micrometer/BaseMeteredClient.java +++ b/micrometer/src/main/java/feign/micrometer/BaseMeteredClient.java @@ -53,6 +53,7 @@ protected void countResponseCode( e, Tag.of("http_status", String.valueOf(responseStatus)), Tag.of("status_group", responseStatus / 100 + "xx"), + Tag.of("http_method", template.methodMetadata().template().method()), Tag.of("uri", template.methodMetadata().template().path())) .and(extraTags); meterRegistry.counter(metricName.name("http_response_code"), allTags).increment(); diff --git a/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java b/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java index 9245a0ba6..322631a0b 100644 --- a/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java +++ b/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java @@ -169,7 +169,7 @@ public void clientPropagatesUncheckedException() { } assertThat( - getMetric("http_response_code", "http_status", "404", "status_group", "4xx"), + getMetric("http_response_code", "http_status", "404", "status_group", "4xx", "http_method", "GET"), notNullValue()); } From c2be290e648eb1a9c435b4a24c439da169f49123 Mon Sep 17 00:00:00 2001 From: rnavarropiris Date: Tue, 7 Mar 2023 16:18:31 +0100 Subject: [PATCH 2/4] Limit line length to 100 chars --- .../test/java/feign/micrometer/AbstractMetricsTestBase.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java b/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java index 322631a0b..61ca6b9d1 100644 --- a/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java +++ b/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java @@ -169,7 +169,9 @@ public void clientPropagatesUncheckedException() { } assertThat( - getMetric("http_response_code", "http_status", "404", "status_group", "4xx", "http_method", "GET"), + getMetric( + "http_response_code", "http_status", "404", "status_group", "4xx", "http_method", "GET" + ), notNullValue()); } From dfec8a0ee62ef63ed136e2e63098c3ea0cdcb627 Mon Sep 17 00:00:00 2001 From: rnavarropiris Date: Tue, 7 Mar 2023 17:16:35 +0100 Subject: [PATCH 3/4] Applies eclipse-java-style to AbstractMetricsTestBase --- .../micrometer/AbstractMetricsTestBase.java | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java b/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java index 61ca6b9d1..ceeef2e08 100644 --- a/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java +++ b/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThrows; import static org.junit.Assert.fail; + import feign.AsyncFeign; import feign.Capability; import feign.Feign; @@ -30,10 +31,12 @@ import feign.mock.HttpMethod; import feign.mock.MockClient; import feign.mock.MockTarget; + import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; + import org.junit.Before; import org.junit.Test; @@ -45,12 +48,14 @@ public interface SimpleSource { String get(String body); } + public interface CompletableSource { @RequestLine("GET /get") CompletableFuture get(String body); } + protected MR metricsRegistry; @Before @@ -66,7 +71,7 @@ public final void addMetricsCapability() { customizeBuilder(Feign.builder() .client(new MockClient().ok(HttpMethod.GET, "/get", "1234567890abcde")) .addCapability(createMetricCapability())) - .target(new MockTarget<>(SimpleSource.class)); + .target(new MockTarget<>(SimpleSource.class)); source.get("0x3456789"); @@ -79,7 +84,7 @@ public final void addAsyncMetricsCapability() { customizeBuilder(AsyncFeign.builder() .client(new MockClient().ok(HttpMethod.GET, "/get", "1234567890abcde")) .addCapability(createMetricCapability())) - .target(new MockTarget<>(CompletableSource.class)); + .target(new MockTarget<>(CompletableSource.class)); source.get("0x3456789").join(); @@ -159,7 +164,7 @@ public void clientPropagatesUncheckedException() { throw notFound.get(); }) .addCapability(createMetricCapability())) - .target(new MockTarget<>(MicrometerCapabilityTest.SimpleSource.class)); + .target(new MockTarget<>(MicrometerCapabilityTest.SimpleSource.class)); try { source.get("0x3456789"); @@ -169,9 +174,8 @@ public void clientPropagatesUncheckedException() { } assertThat( - getMetric( - "http_response_code", "http_status", "404", "status_group", "4xx", "http_method", "GET" - ), + getMetric("http_response_code", "http_status", "404", "status_group", "4xx", + "http_method", "GET"), notNullValue()); } @@ -190,7 +194,7 @@ public void decoderPropagatesUncheckedException() { throw notFound.get(); }) .addCapability(createMetricCapability())) - .target(new MockTarget<>(MicrometerCapabilityTest.SimpleSource.class)); + .target(new MockTarget<>(MicrometerCapabilityTest.SimpleSource.class)); FeignException.NotFound thrown = assertThrows(FeignException.NotFound.class, () -> source.get("0x3456789")); @@ -206,7 +210,7 @@ public void shouldMetricCollectionWithCustomException() { throw new RuntimeException("Test error"); }) .addCapability(createMetricCapability())) - .target(new MockTarget<>(MicrometerCapabilityTest.SimpleSource.class)); + .target(new MockTarget<>(MicrometerCapabilityTest.SimpleSource.class)); RuntimeException thrown = assertThrows(RuntimeException.class, () -> source.get("0x3456789")); assertThat(thrown.getMessage(), equalTo("Test error")); @@ -223,7 +227,7 @@ public void clientMetricsHaveUriLabel() { customizeBuilder(Feign.builder() .client(new MockClient().ok(HttpMethod.GET, "/get", "1234567890abcde")) .addCapability(createMetricCapability())) - .target(new MockTarget<>(SimpleSource.class)); + .target(new MockTarget<>(SimpleSource.class)); source.get("0x3456789"); @@ -252,7 +256,7 @@ public void clientMetricsHaveUriLabelWithPathExpression() { customizeBuilder(Feign.builder() .client(new MockClient().ok(HttpMethod.GET, "/get/123", "1234567890abcde")) .addCapability(createMetricCapability())) - .target(new MockTarget<>(SourceWithPathExpressions.class)); + .target(new MockTarget<>(SourceWithPathExpressions.class)); source.get("123", "0x3456789"); @@ -283,7 +287,7 @@ public void decoderExceptionCounterHasUriLabelWithPathExpression() { throw notFound.get(); }) .addCapability(createMetricCapability())) - .target(new MockTarget<>(MicrometerCapabilityTest.SourceWithPathExpressions.class)); + .target(new MockTarget<>(MicrometerCapabilityTest.SourceWithPathExpressions.class)); FeignException.NotFound thrown = assertThrows(FeignException.NotFound.class, () -> source.get("123", "0x3456789")); From 5da34a51ad4f876cfca7153086d13bec99d7a029 Mon Sep 17 00:00:00 2001 From: rnavarropiris Date: Fri, 17 Mar 2023 08:49:46 +0100 Subject: [PATCH 4/4] Fix AbstractMetricsTestBase format --- .../micrometer/AbstractMetricsTestBase.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java b/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java index ceeef2e08..eb34bfaa4 100644 --- a/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java +++ b/micrometer/src/test/java/feign/micrometer/AbstractMetricsTestBase.java @@ -21,7 +21,6 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThrows; import static org.junit.Assert.fail; - import feign.AsyncFeign; import feign.Capability; import feign.Feign; @@ -31,12 +30,10 @@ import feign.mock.HttpMethod; import feign.mock.MockClient; import feign.mock.MockTarget; - import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; - import org.junit.Before; import org.junit.Test; @@ -71,7 +68,7 @@ public final void addMetricsCapability() { customizeBuilder(Feign.builder() .client(new MockClient().ok(HttpMethod.GET, "/get", "1234567890abcde")) .addCapability(createMetricCapability())) - .target(new MockTarget<>(SimpleSource.class)); + .target(new MockTarget<>(SimpleSource.class)); source.get("0x3456789"); @@ -84,7 +81,7 @@ public final void addAsyncMetricsCapability() { customizeBuilder(AsyncFeign.builder() .client(new MockClient().ok(HttpMethod.GET, "/get", "1234567890abcde")) .addCapability(createMetricCapability())) - .target(new MockTarget<>(CompletableSource.class)); + .target(new MockTarget<>(CompletableSource.class)); source.get("0x3456789").join(); @@ -164,7 +161,7 @@ public void clientPropagatesUncheckedException() { throw notFound.get(); }) .addCapability(createMetricCapability())) - .target(new MockTarget<>(MicrometerCapabilityTest.SimpleSource.class)); + .target(new MockTarget<>(MicrometerCapabilityTest.SimpleSource.class)); try { source.get("0x3456789"); @@ -194,7 +191,7 @@ public void decoderPropagatesUncheckedException() { throw notFound.get(); }) .addCapability(createMetricCapability())) - .target(new MockTarget<>(MicrometerCapabilityTest.SimpleSource.class)); + .target(new MockTarget<>(MicrometerCapabilityTest.SimpleSource.class)); FeignException.NotFound thrown = assertThrows(FeignException.NotFound.class, () -> source.get("0x3456789")); @@ -210,7 +207,7 @@ public void shouldMetricCollectionWithCustomException() { throw new RuntimeException("Test error"); }) .addCapability(createMetricCapability())) - .target(new MockTarget<>(MicrometerCapabilityTest.SimpleSource.class)); + .target(new MockTarget<>(MicrometerCapabilityTest.SimpleSource.class)); RuntimeException thrown = assertThrows(RuntimeException.class, () -> source.get("0x3456789")); assertThat(thrown.getMessage(), equalTo("Test error")); @@ -227,7 +224,7 @@ public void clientMetricsHaveUriLabel() { customizeBuilder(Feign.builder() .client(new MockClient().ok(HttpMethod.GET, "/get", "1234567890abcde")) .addCapability(createMetricCapability())) - .target(new MockTarget<>(SimpleSource.class)); + .target(new MockTarget<>(SimpleSource.class)); source.get("0x3456789"); @@ -256,7 +253,7 @@ public void clientMetricsHaveUriLabelWithPathExpression() { customizeBuilder(Feign.builder() .client(new MockClient().ok(HttpMethod.GET, "/get/123", "1234567890abcde")) .addCapability(createMetricCapability())) - .target(new MockTarget<>(SourceWithPathExpressions.class)); + .target(new MockTarget<>(SourceWithPathExpressions.class)); source.get("123", "0x3456789"); @@ -287,7 +284,7 @@ public void decoderExceptionCounterHasUriLabelWithPathExpression() { throw notFound.get(); }) .addCapability(createMetricCapability())) - .target(new MockTarget<>(MicrometerCapabilityTest.SourceWithPathExpressions.class)); + .target(new MockTarget<>(MicrometerCapabilityTest.SourceWithPathExpressions.class)); FeignException.NotFound thrown = assertThrows(FeignException.NotFound.class, () -> source.get("123", "0x3456789"));