Reputation: 71
I'm using prometheus metric servlet to expose my metrics, with the java client api prometheus supply.
I registered the servlet the same way of registering any servelt, see below:
@Bean
public ServletRegistrationBean registerPrometheusExporterServlet(CollectorRegistry metricRegistry) {
return new ServletRegistrationBean(new MetricsServlet(metricRegistry), "/metrics");
}
However, I would like to add this servlet to the management port or if it is possible that the prometheus version will replace the default /metrics service of springboot. Can something like this can be done? and how?
Thanks, Daniela
Upvotes: 3
Views: 7655
Reputation: 8353
I don't know if you were able to integrate Spring Boot with Prometheus, but now there is a dedicated connector in the official Prometheus client-java
project.
The Github page of the project is the following: simpleclient_spring_boot
You can use it adding the following dependency to you pom.xml
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.0.17</version>
</dependency>
To use it, add a Spring Boot configuration to your project, like the following.
@Configuration
public class MetricsConfiguration {
@Bean
public ServletRegistrationBean servletRegistrationBean() {
DefaultExports.initialize();
return new ServletRegistrationBean(new MetricsServlet(), "/prometheus");
}
@Bean
public SpringBootMetricsCollector springBootMetricsCollector(Collection<PublicMetrics> publicMetrics) {
SpringBootMetricsCollector springBootMetricsCollector = new SpringBootMetricsCollector(
publicMetrics);
springBootMetricsCollector.register();
return springBootMetricsCollector;
}
}
Now on, the metrics exposed by Spring Boot Actuator will be available as Prometheus Counters and Gauges.
The information are published to the path /prometheus
of your application. Then you have to instruct Prometheus to consume this information, with a configuration like the following.
# my global config
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
evaluation_interval: 15s # By default, scrape targets every 15 seconds.
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
- job_name: 'your-application-name'
scrape_interval: 5s
metrics_path: '/prometheus'
static_configs:
- targets: ['localhost:8080']
If you point your browser to /metrics
you will continue to see information in Spring Boot format. But, pointing the browser to http://localhost:9090/graph
you will query into such information directly into Prometheus query browser.
Try to have a look also to this Github pull-request.
UPDATE
In the next release of the simpleclient_spring_boot
, 0.0.18, it will be sufficient to add the annotation @EnablePrometheusEndpoint
to the configuration class of Spring Boot to automagically configure the Prometheus adapter (have a look to this test)!
Hope it helps.
Upvotes: 6