Instrument RestTemplate for Observability

This commit introduces Micrometer as an API dependency to the spring-web
module. Micrometer is used here to instrument `RestTemplate` and record
`Observation` for HTTP client exchanges.

This will replace Spring Boot's `MetricsClientHttpRequestInterceptor`
which uses the request interceptor contract for instrumentation.
This approach is limited as measurements and tags aren't always precise
and overhead is more important than a direct instrumentation.

See gh-28341
This commit is contained in:
Brian Clozel
2022-09-12 11:36:32 +02:00
parent a68b46a2b6
commit a0ddcd07c8
9 changed files with 804 additions and 12 deletions

View File

@@ -0,0 +1,118 @@
/*
* Copyright 2002-2022 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
*
* https://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.http.client.observation;
import java.io.IOException;
import io.micrometer.common.KeyValue;
import io.micrometer.observation.Observation;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.testfixture.http.client.MockClientHttpRequest;
import org.springframework.web.testfixture.http.client.MockClientHttpResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link DefaultClientHttpObservationConvention}.
*
* @author Brian Clozel
*/
class DefaultClientHttpObservationConventionTests {
private final DefaultClientHttpObservationConvention observationConvention = new DefaultClientHttpObservationConvention();
@Test
void supportsOnlyClientHttpObservationContext() {
assertThat(this.observationConvention.supportsContext(new ClientHttpObservationContext())).isTrue();
assertThat(this.observationConvention.supportsContext(new Observation.Context())).isFalse();
}
@Test
void addsKeyValuesForNullExchange() {
ClientHttpObservationContext context = new ClientHttpObservationContext();
assertThat(this.observationConvention.getLowCardinalityKeyValues(context)).hasSize(5)
.contains(KeyValue.of("method", "none"), KeyValue.of("uri", "none"), KeyValue.of("status", "CLIENT_ERROR"),
KeyValue.of("exception", "none"), KeyValue.of("outcome", "UNKNOWN"));
assertThat(this.observationConvention.getHighCardinalityKeyValues(context)).hasSize(2)
.contains(KeyValue.of("client.name", "none"), KeyValue.of("uri.expanded", "none"));
}
@Test
void addsKeyValuesForExchangeWithException() {
ClientHttpObservationContext context = new ClientHttpObservationContext();
context.setError(new IllegalStateException("Could not create client request"));
assertThat(this.observationConvention.getLowCardinalityKeyValues(context)).hasSize(5)
.contains(KeyValue.of("method", "none"), KeyValue.of("uri", "none"), KeyValue.of("status", "CLIENT_ERROR"),
KeyValue.of("exception", "IllegalStateException"), KeyValue.of("outcome", "UNKNOWN"));
assertThat(this.observationConvention.getHighCardinalityKeyValues(context)).hasSize(2)
.contains(KeyValue.of("client.name", "none"), KeyValue.of("uri.expanded", "none"));
}
@Test
void addsKeyValuesForRequestWithUriTemplate() {
ClientHttpObservationContext context = createContext(
new MockClientHttpRequest(HttpMethod.GET, "/resource/{id}", 42), new MockClientHttpResponse());
context.setUriTemplate("/resource/{id}");
assertThat(this.observationConvention.getLowCardinalityKeyValues(context))
.contains(KeyValue.of("exception", "none"), KeyValue.of("method", "GET"), KeyValue.of("uri", "/resource/{id}"),
KeyValue.of("status", "200"), KeyValue.of("outcome", "SUCCESSFUL"));
assertThat(this.observationConvention.getHighCardinalityKeyValues(context)).hasSize(2)
.contains(KeyValue.of("client.name", "none"), KeyValue.of("uri.expanded", "/resource/42"));
}
@Test
void addsKeyValuesForRequestWithoutUriTemplate() {
ClientHttpObservationContext context = createContext(
new MockClientHttpRequest(HttpMethod.GET, "/resource/42"), new MockClientHttpResponse());
assertThat(this.observationConvention.getLowCardinalityKeyValues(context))
.contains(KeyValue.of("method", "GET"), KeyValue.of("uri", "none"));
assertThat(this.observationConvention.getHighCardinalityKeyValues(context)).hasSize(2)
.contains(KeyValue.of("uri.expanded", "/resource/42"));
}
@Test
void addsClientNameForRequestWithHost() {
ClientHttpObservationContext context = createContext(
new MockClientHttpRequest(HttpMethod.GET, "https://localhost:8080/resource/42"),
new MockClientHttpResponse());
assertThat(this.observationConvention.getHighCardinalityKeyValues(context)).contains(KeyValue.of("client.name", "localhost"));
}
@Test
void addsKeyValueForNonResolvableStatus() throws Exception {
ClientHttpObservationContext context = new ClientHttpObservationContext();
ClientHttpResponse response = mock(ClientHttpResponse.class);
context.setResponse(response);
given(response.getStatusCode()).willThrow(new IOException("test error"));
assertThat(this.observationConvention.getLowCardinalityKeyValues(context)).contains(KeyValue.of("status", "IO_ERROR"));
}
private ClientHttpObservationContext createContext(ClientHttpRequest request, ClientHttpResponse response) {
ClientHttpObservationContext context = new ClientHttpObservationContext();
context.setCarrier(request);
context.setResponse(response);
return context;
}
}

View File

@@ -0,0 +1,191 @@
/*
* Copyright 2002-2022 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
*
* https://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.web.client;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import io.micrometer.observation.tck.TestObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistryAssert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.BDDMockito;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.converter.HttpMessageConverter;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.http.HttpMethod.GET;
/**
* Tests for the client HTTP observations with {@link RestTemplate}.
* @author Brian Clozel
*/
public class RestTemplateObservationTests {
private final TestObservationRegistry observationRegistry = TestObservationRegistry.create();
private final ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class);
private final ClientHttpRequest request = mock(ClientHttpRequest.class);
private final ClientHttpResponse response = mock(ClientHttpResponse.class);
private final ResponseErrorHandler errorHandler = mock(ResponseErrorHandler.class);
@SuppressWarnings("unchecked")
private final HttpMessageConverter<String> converter = mock(HttpMessageConverter.class);
private final RestTemplate template = new RestTemplate(List.of(converter));
@BeforeEach
void setupEach() {
this.template.setRequestFactory(this.requestFactory);
this.template.setErrorHandler(this.errorHandler);
this.template.setObservationRegistry(this.observationRegistry);
}
@Test
void executeVarArgsAddsUriTemplateAsKeyValue() throws Exception {
mockSentRequest(GET, "https://example.com/hotels/42/bookings/21");
mockResponseStatus(HttpStatus.OK);
template.execute("https://example.com/hotels/{hotel}/bookings/{booking}", GET,
null, null, "42", "21");
assertThatHttpObservation().hasLowCardinalityKeyValue("uri", "https://example.com/hotels/{hotel}/bookings/{booking}");
}
@Test
void executeArgsMapAddsUriTemplateAsKeyValue() throws Exception {
mockSentRequest(GET, "https://example.com/hotels/42/bookings/21");
mockResponseStatus(HttpStatus.OK);
Map<String, String> vars = Map.of("hotel", "42", "booking", "21");
template.execute("https://example.com/hotels/{hotel}/bookings/{booking}", GET,
null, null, vars);
assertThatHttpObservation().hasLowCardinalityKeyValue("uri", "https://example.com/hotels/{hotel}/bookings/{booking}");
}
@Test
void executeAddsSucessAsOutcome() throws Exception {
mockSentRequest(GET, "https://example.org");
mockResponseStatus(HttpStatus.OK);
mockResponseBody("Hello World", MediaType.TEXT_PLAIN);
template.execute("https://example.org", GET, null, null);
assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "SUCCESSFUL");
}
@Test
void executeAddsServerErrorAsOutcome() throws Exception {
String url = "https://example.org";
mockSentRequest(GET, url);
mockResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR);
BDDMockito.willThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR))
.given(errorHandler).handleError(new URI(url), GET, response);
assertThatExceptionOfType(HttpServerErrorException.class).isThrownBy(() ->
template.execute(url, GET, null, null));
assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "SERVER_ERROR");
}
@Test
void executeAddsExceptionAsKeyValue() throws Exception {
mockSentRequest(GET, "https://example.org/resource");
mockResponseStatus(HttpStatus.OK);
given(converter.canRead(String.class, null)).willReturn(true);
MediaType supportedMediaType = new MediaType("test", "supported");
given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(supportedMediaType));
MediaType other = new MediaType("test", "other");
mockResponseBody("Test Body", other);
given(converter.canRead(String.class, other)).willReturn(false);
assertThatExceptionOfType(RestClientException.class).isThrownBy(() ->
template.getForObject("https://example.org/{p}", String.class, "resource"));
assertThatHttpObservation().hasLowCardinalityKeyValue("exception", "UnknownContentTypeException");
}
@Test
void executeWithIoExceptionAddsUnknownOutcome() throws Exception {
String url = "https://example.org/resource";
mockSentRequest(GET, url);
given(request.execute()).willThrow(new IOException("Socket failure"));
assertThatExceptionOfType(ResourceAccessException.class).isThrownBy(() ->
template.getForObject(url, String.class));
assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "UNKNOWN");
}
private void mockSentRequest(HttpMethod method, String uri) throws Exception {
mockSentRequest(method, uri, new HttpHeaders());
}
private void mockSentRequest(HttpMethod method, String uri, HttpHeaders requestHeaders) throws Exception {
given(requestFactory.createRequest(new URI(uri), method)).willReturn(request);
given(request.getHeaders()).willReturn(requestHeaders);
given(request.getMethod()).willReturn(method);
given(request.getURI()).willReturn(URI.create(uri));
}
private void mockResponseStatus(HttpStatus responseStatus) throws Exception {
given(request.execute()).willReturn(response);
given(errorHandler.hasError(response)).willReturn(responseStatus.isError());
given(response.getStatusCode()).willReturn(responseStatus);
given(response.getStatusText()).willReturn(responseStatus.getReasonPhrase());
}
private void mockResponseBody(String expectedBody, MediaType mediaType) throws Exception {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(mediaType);
responseHeaders.setContentLength(expectedBody.length());
given(response.getHeaders()).willReturn(responseHeaders);
given(response.getBody()).willReturn(new ByteArrayInputStream(expectedBody.getBytes()));
given(converter.read(eq(String.class), any(HttpInputMessage.class))).willReturn(expectedBody);
}
private TestObservationRegistryAssert.TestObservationRegistryAssertReturningObservationContextAssert assertThatHttpObservation() {
return TestObservationRegistryAssert.assertThat(this.observationRegistry)
.hasObservationWithNameEqualTo("http.client.requests").that();
}
}