-
Notifications
You must be signed in to change notification settings - Fork 40.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto-configure Observation support for RestTemplate
Prior to this commit, Spring Boot would auto-configure a customizer that instruments `RestTemplate` through a `RestTemplateBuilder`. This would install a request interceptor that instrumented client exchanges for producing metrics. As of spring-projects/spring-framework#28341, the instrumentation is done at the `RestTemplate` level directly using the `Observation` API. The `Tag` (now `KeyValue`) extraction, observation name and instrumentation behavior now lives in the Spring Framework project. This commit updates the auto-configuration to switch from Boot-specific Metrics instrumentation to a generic Observation instrumentation. As a migration path, some configuration properties are deprecated in favor of the new `management.observations.*` namespace. Closes gh-32484
- Loading branch information
Showing
24 changed files
with
621 additions
and
677 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...boot/actuate/autoconfigure/metrics/web/client/ClientHttpObservationConventionAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2012-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.boot.actuate.autoconfigure.metrics.web.client; | ||
|
||
import io.micrometer.common.KeyValues; | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.observation.Observation; | ||
|
||
import org.springframework.boot.actuate.metrics.web.client.RestTemplateExchangeTagsProvider; | ||
import org.springframework.http.client.observation.ClientHttpObservationContext; | ||
import org.springframework.http.client.observation.ClientHttpObservationConvention; | ||
|
||
/** | ||
* Adapter class that applies {@link RestTemplateExchangeTagsProvider} tags as a | ||
* {@link ClientHttpObservationConvention}. | ||
* | ||
* @author Brian Clozel | ||
*/ | ||
@SuppressWarnings("deprecation") | ||
class ClientHttpObservationConventionAdapter implements ClientHttpObservationConvention { | ||
|
||
private final String metricName; | ||
|
||
private final RestTemplateExchangeTagsProvider tagsProvider; | ||
|
||
ClientHttpObservationConventionAdapter(String metricName, RestTemplateExchangeTagsProvider tagsProvider) { | ||
this.metricName = metricName; | ||
this.tagsProvider = tagsProvider; | ||
} | ||
|
||
@Override | ||
public boolean supportsContext(Observation.Context context) { | ||
return context instanceof ClientHttpObservationContext; | ||
} | ||
|
||
@Override | ||
public KeyValues getLowCardinalityKeyValues(ClientHttpObservationContext context) { | ||
KeyValues keyValues = KeyValues.empty(); | ||
Iterable<Tag> tags = this.tagsProvider.getTags(context.getUriTemplate(), context.getCarrier(), | ||
context.getResponse()); | ||
for (Tag tag : tags) { | ||
keyValues = keyValues.and(tag.getKey(), tag.getValue()); | ||
} | ||
return keyValues; | ||
} | ||
|
||
@Override | ||
public KeyValues getHighCardinalityKeyValues(ClientHttpObservationContext context) { | ||
return KeyValues.empty(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return this.metricName; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 0 additions & 60 deletions
60
...ework/boot/actuate/autoconfigure/metrics/web/client/RestTemplateMetricsConfiguration.java
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
...k/boot/actuate/autoconfigure/metrics/web/client/RestTemplateObservationConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2012-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.boot.actuate.autoconfigure.metrics.web.client; | ||
|
||
import io.micrometer.observation.Observation; | ||
import io.micrometer.observation.ObservationRegistry; | ||
|
||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties; | ||
import org.springframework.boot.actuate.autoconfigure.observation.ObservationProperties; | ||
import org.springframework.boot.actuate.metrics.web.client.ObservationRestTemplateCustomizer; | ||
import org.springframework.boot.actuate.metrics.web.client.RestTemplateExchangeTagsProvider; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.client.observation.ClientHttpObservationConvention; | ||
import org.springframework.http.client.observation.DefaultClientHttpObservationConvention; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
/** | ||
* Configure the instrumentation of {@link RestTemplate}. | ||
* | ||
* @author Brian Clozel | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
@ConditionalOnClass({ RestTemplate.class, Observation.class }) | ||
@ConditionalOnBean({ RestTemplateBuilder.class, ObservationRegistry.class }) | ||
@SuppressWarnings("deprecation") | ||
class RestTemplateObservationConfiguration { | ||
|
||
@Bean | ||
ObservationRestTemplateCustomizer metricsRestTemplateCustomizer(ObservationRegistry observationRegistry, | ||
ObservationProperties observationProperties, MetricsProperties metricsProperties, | ||
ObjectProvider<RestTemplateExchangeTagsProvider> optionalTagsProvider) { | ||
String metricName = metricsProperties.getWeb().getClient().getRequest().getMetricName(); | ||
String observationName = observationProperties.getHttp().getClient().getRequests().getName(); | ||
String name = (observationName != null) ? observationName : metricName; | ||
RestTemplateExchangeTagsProvider tagsProvider = optionalTagsProvider.getIfAvailable(); | ||
ClientHttpObservationConvention observationConvention = (tagsProvider != null) | ||
? new ClientHttpObservationConventionAdapter(name, tagsProvider) | ||
: new DefaultClientHttpObservationConvention(name); | ||
return new ObservationRestTemplateCustomizer(observationRegistry, observationConvention); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...ava/org/springframework/boot/actuate/autoconfigure/observation/ObservationProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2012-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.boot.actuate.autoconfigure.observation; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
/** | ||
* {@link ConfigurationProperties @ConfigurationProperties} for configuring Micrometer | ||
* observations. | ||
* | ||
* @author Brian Clozel | ||
* @since 3.0.0 | ||
*/ | ||
@ConfigurationProperties("management.observations") | ||
public class ObservationProperties { | ||
|
||
private final Http http = new Http(); | ||
|
||
public Http getHttp() { | ||
return this.http; | ||
} | ||
|
||
public static class Http { | ||
|
||
private final Client client = new Client(); | ||
|
||
public Client getClient() { | ||
return this.client; | ||
} | ||
|
||
public static class Client { | ||
|
||
private final ClientRequests requests = new ClientRequests(); | ||
|
||
public ClientRequests getRequests() { | ||
return this.requests; | ||
} | ||
|
||
public static class ClientRequests { | ||
|
||
/** | ||
* Name of the observation for client requests. If empty, will use the | ||
* default "http.client.requests". | ||
*/ | ||
private String name; | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.