Search code examples
prometheusprometheus-java

Inject custom label into default jmx Prometheus metrics for Java client


Our system consists of multiple JAVA based microservices, each one reports its RT status leveraging io.prometheus simpleclient

The integration code looks pretty straightforward

// Initialize default JVM metrics for Prometheus
    DefaultExports.initialize();

    DeploymentInfo monitoringServletInfo = Servlets.deployment()
            .setClassLoader(MonitoringServer.class.getClassLoader())
            .setContextPath("/")
            .setDeploymentName("monitoring")
            .addServlet(Servlets.servlet(MetricsServlet.class).addMapping("/*"));

    DeploymentManager monitoringDeployment = Servlets.defaultContainer().addDeployment(monitoringServletInfo);
    monitoringDeployment.deploy();
    HttpHandler monitoringHandler = monitoringDeployment.start();

    server = Undertow.builder()
            .addHttpListener(port, "0.0.0.0")
            .setHandler(
                    Handlers.path().addExactPath(HEALTH_PATH, new HealthHandler())
                            .addExactPath(MONITORING_PATH, monitoringHandler))
            .build();

    server.start();

Since all microservices expose the same metrics name we are looking for a way to distinguish them by providing a custom label for each microservice

for example, this metrics has the following definition

jvm_buffer_pool_used_bytes{pool="direct",} 116776.0

our purpose is to add a custom label like, then it will be super easy to separate than in the Grafana.

jvm_buffer_pool_used_bytes{pool="direct",service="foo"} 116776.0

I want to believe the lib has to have such capability, unfortunately googling it for a while was not such successful

Any idea will be appreciated.


Solution

  • I would stronly suggest to use Micrometer for that job. Which is

    a) easier to integrate and

    b) you can can also add so called "common tags" which is exactly what you are looking for (Micromether uses the term "tag" instead of "label" but the concept is the same).

    registry.config().commonTags("service", "someService");
    

    Especially for the "default" metrics that the JVM provides also via JMX it has an easy integration:

    new JvmMemoryMetrics().bindTo(registry);
    

    and you got all the memory metrics in your prometheus metrics. (see here for more information)