Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitor Java web-app using Prometheus

I got a simple java web application running on Tomcat which I would like to monitor using Prometheus. I added some counters and gauges to my java app using the official client, I installed Prometheus on another server and not would like to pull the metrics from my java app using Prometheus.

Prometheus got tons of exporters but I'm not clear about which one fits my use case. I considered using Prometheus push gateway but according to the documentation the only valid use case for the Pushgateway is for capturing the outcome of a service-level batch job, which is not my case. What should I do in order to pull the metrics from my java app?

like image 419
forhas Avatar asked Oct 19 '25 16:10

forhas


2 Answers

In order to register MetricsServlet:

If you are using spring-boot register a bean to represent the end-point:

@Bean
ServletRegistrationBean registerPrometheusExporterServlet(CollectorRegistry metricRegistry) {
    ServletRegistrationBean srb = new ServletRegistrationBean();
    srb.setServlet(new MetricsServlet(metricRegistry));
    srb.setUrlMappings(Arrays.asList("/prometheus"));
    return srb;
}

In case of a non-spring app add the servlet in web.xml:

<servlet>
        <servlet-name>metrics</servlet-name>
        <servlet-class>io.prometheus.client.exporter.MetricsServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>metrics</servlet-name>
    <url-pattern>/metrics/</url-pattern>
</servlet-mapping>
like image 168
forhas Avatar answered Oct 21 '25 06:10

forhas


You should have your application expose the metrics over HTTP. You probably want to hook the servlet into your existing webserver.

like image 22
brian-brazil Avatar answered Oct 21 '25 07:10

brian-brazil