prometheus / client_java

Prometheus instrumentation library for JVM applications
http://prometheus.github.io/client_java/
Apache License 2.0
2.18k stars 798 forks source link

Spring Boot 2.x example missing #854

Open bu3 opened 1 year ago

bu3 commented 1 year ago

Hi,

I have an existing Spring Boot 2.7.14 application where metrics are collected with Dropwizard. I don't plan to migrate to MicroMeter in the short term so I cannot use io.micrometer:micrometer-registry-prometheus to enable the Spring provided Prometheus endpoint.

So I've been trying to use:

it looks like simpleclient_spring_boot is not compatible with Spring boot 2 because it relies on class PublicMetrics that has been deprecated in Spring Boot 2.

Am I looking at the wrong place? Is it possible to enable the Prometheus Endpoint with Spring Boot 2 in other ways?

Thanks

fstab commented 1 year ago

Hi,

Yes, it is possible to enable the Prometheus endpoint with Spring Boot 2. I think the easiest way is to use the Prometheus MetricsServlet (or PrometheusMetricsServlet in 1.0.x) and register it with your Spring application, something like this:

@Bean
public ServletRegistrationBean createPrometheusServlet() {
    return new ServletRegistrationBean(new MetricsServlet(),"/metrics/*");
}

We are currently working on the preview version of the 1.0.0 releases, so you have two options which MetricsServlet to use:

This is the current release with io.prometheus.client.servlet.jakarta.exporter.MetricsServlet.

<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_servlet_jakarta</artifactId>
    <version>0.16.0</version>
</dependency>

This is the alpha preview of the upcoming 1.0.0 release with io.prometheus.metrics.exporter.servlet.jakarta.PrometheusMetricsServlet:

<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>prometheus-metrics-exporter-servlet-jakarta</artifactId>
    <version>1.0.0-alpha-2</version>
</dependency>

The source code of the 1.0.0 version is on the 1.0.x branch.

Here's some context of the current status with Prometheus and Micrometer: I think it would be great if Micrometer used the new Prometheus 1.0.0 library directly. I created a PR for that: https://github.com/micrometer-metrics/micrometer/pull/3987. The PR description has links to a modified Spring Boot version and an example application. If the PR gets accepted we would have the following:

Anyway, whether this will happen or not depends on what the Micrometer folks think of this. I will eventually add Spring Boot 2 and Spring Boot 3 examples, but I'll wait a couple of weeks to see what's coming out of the PR discussion, because the examples will depend on whether Micrometer will have direct support for Prometheus Metrics 1.0.0 or not.

fstab commented 1 year ago

In case you are wondering: The goal is to have the 1.0.0 release out before PromCon, which will be 28/29 September 2023.

bu3 commented 1 year ago

That's amazing! Thanks for sharing @fstab. I'll give it a try immediately