This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from microservices-demo/refactor/prometheus
Rationalise prometheus to produce RED metrics.
- Loading branch information
Showing
5 changed files
with
127 additions
and
1 deletion.
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
35 changes: 35 additions & 0 deletions
35
src/main/java/works/weave/socks/cart/configuration/PrometheusAutoConfiguration.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,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); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/works/weave/socks/cart/configuration/WebMvcConfig.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,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("/**"); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/works/weave/socks/cart/middleware/HTTPMonitoringInterceptor.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,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 { | ||
} | ||
} |
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