net.javalib.isb.AutoConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of isb Show documentation
Show all versions of isb Show documentation
Instruments spring boot applications
package net.javalib.isb;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Properties;
import com.codahale.metrics.MetricRegistry;
import com.ryantenney.metrics.spring.config.annotation.EnableMetrics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@ComponentScan
@EnableMetrics
public class AutoConfig {
private static final Logger logger = LoggerFactory.getLogger(AutoConfig.class);
public AutoConfig() {
logger.info("ISB AutoConfig ON");
}
@Bean
WebMvcConfigurerAdapter webMvcConfigurerAdapter(final @Value("${isb.swagger:true}") boolean enableSwagger, final MetricRegistry metricRegistry) {
WebMvcConfigurerAdapter config = new WebMvcConfigurerAdapter() {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (enableSwagger) {
registry.addResourceHandler("/swagger/**").addResourceLocations("classpath:/isb-swagger/");
}
}
};
return config;
}
@Autowired
public void setInfoProperties(ConfigurableEnvironment env) {
Properties props = new Properties();
RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
props.put("info.isb.startTime",runtimeBean.getStartTime());
env.getPropertySources().addFirst(new PropertiesPropertySource("extra-info-props",props));
}
}