Prometheus Metrics and JMX with Embedded Infinispan

What You Will Learn

How to enable statistics on an embedded Infinispan cache manager and cache, scrape metrics in Prometheus format using Micrometer, and access cache attributes via JMX.

Prerequisites

  • Java 17+

Step 1: Add Dependencies

Add Infinispan core and the Micrometer Prometheus registry to your pom.xml:

<dependency>
   <groupId>org.infinispan</groupId>
   <artifactId>infinispan-core</artifactId>
</dependency>
<dependency>
   <groupId>io.micrometer</groupId>
   <artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
   <groupId>io.micrometer</groupId>
   <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

Step 2: Configure Statistics and JMX

Configure the cache manager with statistics and JMX enabled, then create a distributed cache with statistics:

        GlobalConfigurationBuilder builder = GlobalConfigurationBuilder.defaultClusteredBuilder();

        // Toggle statistics for the cache manager.
        // Equivalent of the following in the XML:
        //   <cache-container statistics="true">
        builder.cacheContainer().statistics(true);

        // Also enable JMX to scrap metrics.
        // Equivalent of the following XML:
        //   <jmx enabled="true" domain="${JMX_DOMAIN}"/>
        builder.jmx().enabled(true).domain(JMX_DOMAIN);

        dcm = new DefaultCacheManager(builder.build());

Step 3: Scrape Prometheus Metrics

Use the MetricsRegistry to scrape metrics in Prometheus text format:

        MetricsRegistry registry = GlobalComponentRegistry.componentOf(dcm, MetricsRegistry.class);
        Objects.requireNonNull(registry, "Registry is null");

        if (!registry.supportScrape())
            throw new IllegalStateException("Scrape not supported");

        return registry.scrape(CONTENT_TYPE);

Step 4: Access JMX Attributes

Query cache manager attributes through JMX:

        // Retrieve the server to lookup the attributes.
        MBeanServerLookup server = dcm.getCacheManagerConfiguration().jmx().mbeanServerLookup();
        Objects.requireNonNull(server, "MBeanServerLookup is null");

        // Create the name to access the cache manager attributes.
        ObjectName name = new ObjectName(String.format("%s:component=CacheManager,name=\"DefaultCacheManager\",type=CacheManager", JMX_DOMAIN));

        // Retrieve the number of running caches.
        return server.getMBeanServer().getAttribute(name, "RunningCacheCount");

Step 5: Run the Tutorial

mvn package exec:java

The output displays Prometheus-formatted metrics followed by the running cache count from JMX.

What’s Next