📜 ⬆️ ⬇️

Micrometer Connection for Java Web Application

Micrometer is used to collect JVM application metrics and allows you to export data to various monitoring systems. In this article I will tell you how to connect Micrometer for a spring web application and export data to Prometheus (a good post about it ). Since my application is written in Spring 3, there is no possibility to use the Spring boot without an upgrade. So you have to work with your hands.

First of all, we need Prometheus dependencies, the client itself and the servlet to export data:

pom.xml
<dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient</artifactId> <version>0.6.0</version> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_servlet</artifactId> <version>0.6.0</version> </dependency> 


Next, add the metrics export servlet to web.xml and specify the path by which the metrics will be available:

 <servlet> <servlet-name>prometheus</servlet-name> <servlet-class>io.prometheus.client.exporter.MetricsServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>prometheus</servlet-name> <url-pattern>/metrics</url-pattern> </servlet-mapping> 

Do not forget to add, if necessary, access policy:
')
 <http pattern="/metrics" security="none"/> 

Now statistics will be available at localhost: 8080 / metrics, but for now this is a blank page. In order for the data to appear in it, you need to register the collection of application metrics.

To use Micrometer add dependencies:

pom.xml
 <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-core</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>1.1.3</version> </dependency> 


Next you need to register the necessary metric collectors:

 @WebListener public class PrometheusInitListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { PrometheusMeterRegistry meterRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT, CollectorRegistry.defaultRegistry, Clock.SYSTEM); new ClassLoaderMetrics().bindTo(meterRegistry); new JvmMemoryMetrics().bindTo(meterRegistry); new JvmGcMetrics().bindTo(meterRegistry); new ProcessorMetrics().bindTo(meterRegistry); new JvmThreadMetrics().bindTo(meterRegistry); } @Override public void contextDestroyed(ServletContextEvent sce) { } } 

Done, now if we go to localhost: 8080 / metrics, we’ll get JVM activity data.

It looks like this:



Prometheus has its own simpleclient_hotspot JVM application statistics library , which is configured by calling DefaultExports.initialize (); in the web listener initialization method.

To configure metrics collection in prometheus.yml, add a target:

 scrape_configs: - job_name: 'test-server' # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s metrics_path: /metrics static_configs: - targets: ['localhost:8080'] 

Collected metrics in Prometheus can be viewed using the special PromQL language. Plus Micrometr'a that you can not write, and use ready-made plugins for visualization systems metrics.


Displaying application metrics in Grafana, plugin 4683

Source: https://habr.com/ru/post/442080/


All Articles