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

Remove special handling of 404/301 from JDK HTTP client instrumentation #5838

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,15 @@ public KeyValues getLowCardinalityKeyValues(HttpClientContext context) {
return KeyValues.of(
HttpClientObservationDocumentation.LowCardinalityKeys.METHOD.withValue(httpRequest.method()),
HttpClientObservationDocumentation.LowCardinalityKeys.URI
.withValue(getUri(httpRequest, context.getResponse(), context.getUriMapper())),
.withValue(getUri(httpRequest, context.getUriMapper())),
HttpClientObservationDocumentation.LowCardinalityKeys.STATUS
.withValue(getStatus(context.getResponse())),
HttpClientObservationDocumentation.LowCardinalityKeys.OUTCOME
.withValue(getOutcome(context.getResponse())));
}

String getUri(HttpRequest request, @Nullable HttpResponse<?> httpResponse,
Function<HttpRequest, String> uriMapper) {
return httpResponse != null && (httpResponse.statusCode() == 404 || httpResponse.statusCode() == 301)
? "NOT_FOUND" : uriMapper.apply(request);
String getUri(HttpRequest request, Function<HttpRequest, String> uriMapper) {
return uriMapper.apply(request);
}

String getStatus(@Nullable HttpResponse<?> response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private <T> void stopObservationOrTimer(
instrumentation.stop(DefaultHttpClientObservationConvention.INSTANCE.getName(), "Timer for JDK's HttpClient",
() -> Tags.of(HttpClientObservationDocumentation.LowCardinalityKeys.METHOD.asString(), request.method(),
HttpClientObservationDocumentation.LowCardinalityKeys.URI.asString(),
DefaultHttpClientObservationConvention.INSTANCE.getUri(request, res, uriMapper),
DefaultHttpClientObservationConvention.INSTANCE.getUri(request, uriMapper),
HttpClientObservationDocumentation.LowCardinalityKeys.STATUS.asString(),
DefaultHttpClientObservationConvention.INSTANCE.getStatus(res),
HttpClientObservationDocumentation.LowCardinalityKeys.OUTCOME.asString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void setup() {
stubFor(any(urlEqualTo("/metrics")).willReturn(ok().withBody("body")));
stubFor(any(urlEqualTo("/test-fault"))
.willReturn(new ResponseDefinitionBuilder().withFault(Fault.CONNECTION_RESET_BY_PEER)));
stubFor(any(urlEqualTo("/resources/1")).willReturn(notFound()));
}

@Test
Expand Down Expand Up @@ -98,6 +99,46 @@ void shouldInstrumentHttpClientWithTimer(WireMockRuntimeInfo wmInfo) throws IOEx
thenMeterRegistryContainsHttpClientTags();
}

@Test
void shouldInstrumentHttpClientWhenNotFound(WireMockRuntimeInfo wmInfo) throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(URI.create(wmInfo.getHttpBaseUrl() + "/resources/1"))
.build();

HttpClient observedClient = MicrometerHttpClient.instrumentationBuilder(httpClient, meterRegistry).build();
observedClient.send(request, HttpResponse.BodyHandlers.ofString());

then(meterRegistry.get("http.client.requests")
.tag("method", "GET")
.tag("status", "404")
.tag("outcome", "CLIENT_ERROR")
.tag("uri", "UNKNOWN")
.timer()).isNotNull();
}

@Test
void shouldInstrumentHttpClientWithUriPatternHeaderWhenNotFound(WireMockRuntimeInfo wmInfo)
throws IOException, InterruptedException {
String uriPattern = "/resources/{id}";

HttpRequest request = HttpRequest.newBuilder()
.header(MicrometerHttpClient.URI_PATTERN_HEADER, uriPattern)
.GET()
.uri(URI.create(wmInfo.getHttpBaseUrl() + "/resources/1"))
.build();

HttpClient observedClient = MicrometerHttpClient.instrumentationBuilder(httpClient, meterRegistry).build();
observedClient.send(request, HttpResponse.BodyHandlers.ofString());

then(meterRegistry.get("http.client.requests")
.tag("method", "GET")
.tag("status", "404")
.tag("outcome", "CLIENT_ERROR")
.tag("uri", uriPattern)
.timer()).isNotNull();
}

@Test
@Issue("#5136")
void shouldThrowErrorFromSendAsync(WireMockRuntimeInfo wmInfo) {
Expand Down