-
Notifications
You must be signed in to change notification settings - Fork 990
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split up tag configurers for web monitoring, shuffle configurations f…
…or Boot 1.x support
- Loading branch information
1 parent
c2bae7c
commit edd579c
Showing
27 changed files
with
812 additions
and
596 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
240 changes: 0 additions & 240 deletions
240
src/main/java/org/springframework/metrics/boot/EnableMetricsBoot1.java
This file was deleted.
Oops, something went wrong.
98 changes: 98 additions & 0 deletions
98
src/main/java/org/springframework/metrics/boot/InstrumentRestTemplateConfiguration.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,98 @@ | ||
/** | ||
* Copyright 2017 Pivotal Software, Inc. | ||
* | ||
* 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 | ||
* | ||
* http://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.metrics.boot; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.http.client.ClientHttpRequestInterceptor; | ||
import org.springframework.metrics.instrument.MeterRegistry; | ||
import org.springframework.metrics.instrument.TagFormatter; | ||
import org.springframework.metrics.instrument.web.MetricsRestTemplateInterceptor; | ||
import org.springframework.metrics.instrument.web.RestTemplateTagConfigurer; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Configuration | ||
@ConditionalOnClass(RestTemplate.class) | ||
class InstrumentRestTemplateConfiguration { | ||
@Autowired(required = false) | ||
RestTemplateTagConfigurer tagConfigurer; | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(RestTemplateTagConfigurer.class) | ||
RestTemplateTagConfigurer restTemplateTagConfigurer(TagFormatter tagFormatter) { | ||
if(tagConfigurer != null) | ||
return tagConfigurer; | ||
this.tagConfigurer = new RestTemplateTagConfigurer(tagFormatter); | ||
return tagConfigurer; | ||
} | ||
|
||
@Bean | ||
MetricsRestTemplateInterceptor clientHttpRequestInterceptor(MeterRegistry meterRegistry, | ||
TagFormatter tagFormatter, | ||
Environment environment) { | ||
return new MetricsRestTemplateInterceptor(meterRegistry, restTemplateTagConfigurer(tagFormatter), | ||
environment.getProperty("spring.metrics.web.client_requests.name", "http_client_requests")); | ||
} | ||
|
||
@Bean | ||
static BeanPostProcessor restTemplateInterceptorPostProcessor() { | ||
return new MetricsInterceptorPostProcessor(); | ||
} | ||
|
||
private static class MetricsInterceptorPostProcessor | ||
implements BeanPostProcessor, ApplicationContextAware { | ||
private ApplicationContext context; | ||
private MetricsRestTemplateInterceptor interceptor; | ||
|
||
@Override | ||
public Object postProcessBeforeInitialization(Object bean, String beanName) { | ||
return bean; | ||
} | ||
|
||
@Override | ||
public Object postProcessAfterInitialization(Object bean, String beanName) { | ||
if (bean instanceof RestTemplate) { | ||
if (this.interceptor == null) { | ||
this.interceptor = this.context | ||
.getBean(MetricsRestTemplateInterceptor.class); | ||
} | ||
RestTemplate restTemplate = (RestTemplate) bean; | ||
// create a new list as the old one may be unmodifiable (ie Arrays.asList()) | ||
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); | ||
interceptors.add(interceptor); | ||
interceptors.addAll(restTemplate.getInterceptors()); | ||
restTemplate.setInterceptors(interceptors); | ||
} | ||
return bean; | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext context) throws BeansException { | ||
this.context = context; | ||
} | ||
} | ||
} |
Oops, something went wrong.