Skip to content

Commit

Permalink
Split up tag configurers for web monitoring, shuffle configurations f…
Browse files Browse the repository at this point in the history
…or Boot 1.x support
  • Loading branch information
jkschneider committed May 23, 2017
1 parent c2bae7c commit edd579c
Show file tree
Hide file tree
Showing 27 changed files with 812 additions and 596 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ jmh {
duplicateClassesStrategy = 'warn'
}

bintray.pkg {
labels = ['spring', 'metrics', 'prometheus', 'spectator']
if(project.extensions.findByName('bintray')) {
bintray.pkg {
labels = ['spring', 'metrics', 'prometheus', 'spectator']
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.springframework.metrics.boot;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.lang.annotation.*;
Expand All @@ -27,12 +26,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(MetricsBoot1Configuration.class)
@Import({ MetricsConfiguration.class, MetricsBoot1Configuration.class })
public @interface EnableMetrics {
}

@Configuration
class MetricsConfiguration {
// TODO when we figure out if or how Boot 2 might be different, implement this
// https://github.com/spring-projects/spring-metrics/wiki/Enhancements-To-Spring-5.0-and-Boot-2.0
}
240 changes: 0 additions & 240 deletions src/main/java/org/springframework/metrics/boot/EnableMetricsBoot1.java

This file was deleted.

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;
}
}
}
Loading

0 comments on commit edd579c

Please sign in to comment.