Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #26 from microservices-demo/refactor/prometheus
Browse files Browse the repository at this point in the history
Rationalise prometheus to produce RED metrics.
  • Loading branch information
Phil Winder authored Mar 14, 2017
2 parents 3e98fc2 + 65d444b commit ff2cc18
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<version>1.4.4.RELEASE</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<prometheus.version>0.0.21</prometheus.version>
</properties>

<dependencies>
Expand All @@ -36,6 +37,21 @@
<artifactId>spring-cloud-starter-zipkin</artifactId>
<version>1.1.0.RELEASE</version>
</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>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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("/**");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 {
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ endpoints.health.enabled=false
spring.zipkin.baseUrl=http://${zipkin:zipkin}:9411/
spring.sleuth.sampler.percentage=1.0
spring.application.name=carts
# Disable actuator metrics endpoints
endpoints.metrics.enabled=false

0 comments on commit ff2cc18

Please sign in to comment.