|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.metrics.web.client; |
| 18 | + |
| 19 | +import io.micrometer.observation.Observation; |
| 20 | +import io.micrometer.observation.ObservationRegistry; |
| 21 | + |
| 22 | +import org.springframework.beans.factory.ObjectProvider; |
| 23 | +import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties; |
| 24 | +import org.springframework.boot.actuate.autoconfigure.observation.ObservationProperties; |
| 25 | +import org.springframework.boot.actuate.metrics.web.client.ObservationRestTemplateCustomizer; |
| 26 | +import org.springframework.boot.actuate.metrics.web.client.RestTemplateExchangeTagsProvider; |
| 27 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| 28 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 29 | +import org.springframework.boot.web.client.RestTemplateBuilder; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.http.client.observation.ClientHttpObservationConvention; |
| 33 | +import org.springframework.http.client.observation.DefaultClientHttpObservationConvention; |
| 34 | +import org.springframework.web.client.RestTemplate; |
| 35 | + |
| 36 | +/** |
| 37 | + * Configure the instrumentation of {@link RestTemplate}. |
| 38 | + * |
| 39 | + * @author Brian Clozel |
| 40 | + */ |
| 41 | +@Configuration(proxyBeanMethods = false) |
| 42 | +@ConditionalOnClass({ RestTemplate.class, Observation.class }) |
| 43 | +@ConditionalOnBean({ RestTemplateBuilder.class, ObservationRegistry.class }) |
| 44 | +@SuppressWarnings("deprecation") |
| 45 | +class RestTemplateObservationConfiguration { |
| 46 | + |
| 47 | + @Bean |
| 48 | + ObservationRestTemplateCustomizer metricsRestTemplateCustomizer(ObservationRegistry observationRegistry, |
| 49 | + ObservationProperties observationProperties, MetricsProperties metricsProperties, |
| 50 | + ObjectProvider<RestTemplateExchangeTagsProvider> optionalTagsProvider) { |
| 51 | + String metricName = metricsProperties.getWeb().getClient().getRequest().getMetricName(); |
| 52 | + String observationName = observationProperties.getHttp().getClient().getRequests().getName(); |
| 53 | + String name = (observationName != null) ? observationName : metricName; |
| 54 | + RestTemplateExchangeTagsProvider tagsProvider = optionalTagsProvider.getIfAvailable(); |
| 55 | + ClientHttpObservationConvention observationConvention = (tagsProvider != null) |
| 56 | + ? new ClientHttpObservationConventionAdapter(name, tagsProvider) |
| 57 | + : new DefaultClientHttpObservationConvention(name); |
| 58 | + return new ObservationRestTemplateCustomizer(observationRegistry, observationConvention); |
| 59 | + } |
| 60 | + |
| 61 | +} |
0 commit comments