Commit ff2cc18b authored by Phil Winder's avatar Phil Winder Committed by GitHub

Merge pull request #26 from microservices-demo/refactor/prometheus

Rationalise prometheus to produce RED metrics.
parents 3e98fc26 65d444bb
...@@ -14,12 +14,13 @@ ...@@ -14,12 +14,13 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version> <version>1.4.4.RELEASE</version>
</parent> </parent>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<prometheus.version>0.0.21</prometheus.version>
</properties> </properties>
<dependencies> <dependencies>
...@@ -36,6 +37,21 @@ ...@@ -36,6 +37,21 @@
<artifactId>spring-cloud-starter-zipkin</artifactId> <artifactId>spring-cloud-starter-zipkin</artifactId>
<version>1.1.0.RELEASE</version> <version>1.1.0.RELEASE</version>
</dependency> </dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>${prometheus.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>${prometheus.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_servlet</artifactId>
<version>${prometheus.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId> <artifactId>spring-data-rest-hal-browser</artifactId>
......
package works.weave.socks.cart.configuration;
import io.prometheus.client.exporter.MetricsServlet;
import io.prometheus.client.hotspot.DefaultExports;
import io.prometheus.client.spring.boot.SpringBootMetricsCollector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.endpoint.PublicMetrics;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Collection;
@Configuration
@ConditionalOnClass(SpringBootMetricsCollector.class)
class PrometheusAutoConfiguration {
@Bean
@ConditionalOnMissingBean(SpringBootMetricsCollector.class)
SpringBootMetricsCollector springBootMetricsCollector(Collection<PublicMetrics> publicMetrics) {
SpringBootMetricsCollector springBootMetricsCollector = new SpringBootMetricsCollector
(publicMetrics);
springBootMetricsCollector.register();
return springBootMetricsCollector;
}
@Bean
@ConditionalOnMissingBean(name = "prometheusMetricsServletRegistrationBean")
ServletRegistrationBean prometheusMetricsServletRegistrationBean(@Value("${prometheus.metrics" +
".path:/metrics}") String metricsPath) {
DefaultExports.initialize();
return new ServletRegistrationBean(new MetricsServlet(), metricsPath);
}
}
package works.weave.socks.cart.configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import works.weave.socks.cart.middleware.HTTPMonitoringInterceptor;
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Autowired
private HTTPMonitoringInterceptor httpMonitoringInterceptor;
@Bean
HTTPMonitoringInterceptor httpMonitoringInterceptor() {
return new HTTPMonitoringInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(httpMonitoringInterceptor)
.addPathPatterns("/**");
}
}
package works.weave.socks.cart.middleware;
import io.prometheus.client.Histogram;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HTTPMonitoringInterceptor implements HandlerInterceptor {
static final Histogram requestLatency = Histogram.build()
.name("request_duration_seconds")
.help("Request duration in seconds.")
.labelNames("service", "method", "route", "status_code")
.register();
private static final String startTimeKey = "startTime";
@Value("${spring.application.name:carts}")
private String serviceName;
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse
httpServletResponse, Object o) throws Exception {
httpServletRequest.setAttribute(startTimeKey, System.nanoTime());
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse
httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
long start = (long) httpServletRequest.getAttribute(startTimeKey);
long elapsed = System.nanoTime() - start;
double seconds = (double) elapsed / 1000000000.0;
requestLatency.labels(
serviceName,
httpServletRequest.getMethod(),
httpServletRequest.getServletPath(),
Integer.toString(httpServletResponse.getStatus())
).observe(seconds);
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse
httpServletResponse, Object o, Exception e) throws Exception {
}
}
...@@ -4,3 +4,5 @@ endpoints.health.enabled=false ...@@ -4,3 +4,5 @@ endpoints.health.enabled=false
spring.zipkin.baseUrl=http://${zipkin:zipkin}:9411/ spring.zipkin.baseUrl=http://${zipkin:zipkin}:9411/
spring.sleuth.sampler.percentage=1.0 spring.sleuth.sampler.percentage=1.0
spring.application.name=carts spring.application.name=carts
# Disable actuator metrics endpoints
endpoints.metrics.enabled=false
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment