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

Open observation scope in RestClient #33404

Closed
wants to merge 1 commit into from
Closed
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 @@ -520,6 +520,7 @@ private <T> T exchangeInternal(ExchangeFunction<T> exchangeFunction, boolean clo

ClientHttpResponse clientResponse = null;
Observation observation = null;
Observation.Scope scope = null;
URI uri = null;
try {
uri = initUri();
Expand All @@ -532,6 +533,7 @@ private <T> T exchangeInternal(ExchangeFunction<T> exchangeFunction, boolean clo
observationContext.setUriTemplate((String) attributes.get(URI_TEMPLATE_ATTRIBUTE));
observation = ClientHttpObservationDocumentation.HTTP_CLIENT_EXCHANGES.observation(observationConvention,
DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, observationRegistry).start();
scope = observation.openScope();
if (this.body != null) {
this.body.writeTo(clientRequest);
}
Expand Down Expand Up @@ -559,8 +561,13 @@ private <T> T exchangeInternal(ExchangeFunction<T> exchangeFunction, boolean clo
throw error;
}
finally {
if (close && clientResponse != null) {
clientResponse.close();
if (close) {
if (clientResponse != null) {
clientResponse.close();
}
if (scope != null) {
scope.close();
}
if (observation != null) {
observation.stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ void registerObservationWhenReadingBody() throws Exception {
assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "SUCCESS");
}

@Test
void exchangeInternalShouldOpenAndCloseObservationScope() throws Exception {
mockSentRequest(GET, "https://example.org");
mockResponseStatus(HttpStatus.OK);
mockResponseBody("Hello World", MediaType.TEXT_PLAIN);

client.get().uri("https://example.org").exchange((request, response) -> response.bodyTo(String.class));
assertThat(observationRegistry).isNotNull();
}

@Test
void registerObservationWhenReadingStream() throws Exception {
mockSentRequest(GET, "https://example.org");
Expand Down