I want to configure monitoring. I looked in the Internet, and found many examples of implementation this for Spring Bout + Micrometer + Prometheus + Grafana.
But my problem is that I have an old project on Spring MVC 3.2.9. I have a multi -module project:
- module ui
- module requests
- module employees
Module ui on Spring MVC. The remaining 2 modules are connected with it as SOAP Web Services (I use the annotation @WebService).
I try to configure monitoring manually. Firstly, I added to the ui-module pom-file several dependencies:
<!-- client prometheus -->
<dependency>
<groupid> io.prometheus </ groupid>
<artifactid> simpleclient </rtifactid>
<version> 0.6.0 </ version>
</fendency>
<!-- Servlet for data export in Prometheus -->
<dependency>
<groupid> io.prometheus </ groupid>
<artifactid> simpleclient_servlet </ artifactid>
<version> 0.6.0 </ version>
</fendency>
<!-- Micrometer library -->
<dependency>
<groupid> io.micrometer </ groupid>
<artifactid> micrometer-core </rtifactid>
<version> 1.1.3 </ version>
</fendency>
<!-- Allows to Micrometer work with Prometheus -->
<dependency>
<groupid> io.micrometer </ groupid>
<artifactid> micrometer-segistry-prometheus </ artifactid>
<version> 1.1.3 </ version>
</fendency>
Next, in the web.xml file, I create a servlet for export metrics:
<!-- Servlet for export metrics -->
<servlet>
<servlet-name> prometheus </ servlet-name>
<servlet-class> io.prometheus.client.exporter.MetricsServlet </sevlet-class>
<load-on-startup> 1 </laad-on-startup>
</ servlet>
<!-- Path in which the metrics will be available -->
<servlet-mapping>
<servlet-name> prometheus </ servlet-name>
<url-pattern>/metrics </rl-pattern>
</ servlet-mapping>
Then I create a class, in which I register the necessary metric collectors:
@WebListener
public class PrometheusInitListener implements ServletContextListener {
/*
* Register the necessary metric collectors
*/
@Override
public void contextInitialized(ServletContextEvent ServletContextEvent) {
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 contextInitialized(ServletContextEvent servletContextEvent) {
}
}
At the end I start the project. I open the address:
http://localhost:8080/core/metrics
and I see standard jvm metrics:
# HELP JVM_GC_MEMORY_PROMOTED_BYTES_TOTAL COUNT of POSITISE InCreases in the SIZE of the Old Generation Memory Pool Before Gc TOM TOM
# Type JVM_GC_MEMERY_PROMOTED_BYTES_TOTAL COUNTER
JVM_GC_MEMORY_PROMOTED_BYTES_TOTAL 2.6146872E7
# HELP JVM_BUFFER_COUNT_BUFFERS ASTEMATE Of the Number of Buffers in the Pool
# Type jvm_buffer_count_buffers gauge
JVM_BUFFER_COUNT_BUFFERS {ID = "DIRECT",} 14.0
jvm_buffer_count_buffers {id = "mapped",} 0.0
. . . . .
Now I have the following question: I want to configure custom metrics for some business logic in 2 other modules: requests and employees . But I do not understand one moment: how can I transfer these metrics to the ui-module? Can you tell me, please :-).
Now I read in the Micrometer documentation this thing: "Micrometer is the instrumentation library underpinning Spring Boot 2's or later metrics collection.". Maybe Micrometer doesn't work with old Spring 3 projects? Maybe I should use another library, instead of Micrometer?
Spring WebMVC 3.2.9.RELEASE was released more than 9 years ago (2014-05-20) so you should upgrade. Micrometer is younger than that so there is no Instrumentation In Spring Framework 3.x.
You need to instrument your components on your own, you can look at the instrumentation in Spring Framework 6 but given that you are using a more than 9 years old version, you might not be able to copy the instrumentation as-is.
So I think your best course of action is upgrading the app and you can enjoy the instrumentation that is given to you by Spring Framework 6.