Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add http_method tag to micrometer metrics #1968

Merged
merged 7 commits into from
Mar 17, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -45,12 +48,14 @@ public interface SimpleSource {
String get(String body);
}


public interface CompletableSource {

@RequestLine("GET /get")
CompletableFuture<String> get(String body);
}


protected MR metricsRegistry;

@Before
Expand All @@ -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");

Expand All @@ -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();

Expand Down Expand Up @@ -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");
Expand All @@ -169,7 +174,8 @@ 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());
}

Expand All @@ -188,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"));
Expand All @@ -204,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"));
Expand All @@ -221,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");

Expand Down Expand Up @@ -250,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");

Expand Down Expand Up @@ -281,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"));
Expand Down