Search code examples
spring-bootmetricsactivemq-artemismicrometeractuator

How can I expose the Metrics of ActiveMQ Artemis running in embedded mode via Spring Boot 3.x actuators Micrometer metrics API?


How can I configure ActiveMQ Artemis and Spring Boot to expose the metrics of the embedded ActiveMQ Artemis via the actuator API?

My goal is to expose it via the Prometheus endpoint so I can scrape it.

FYI: I am using Spring Boot 3.2

I added the following dependencies

implementation 'org.apache.activemq:artemis-jakarta-server'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.micrometer:micrometer-tracing-bridge-otel'

implementation 'org.springframework.boot:spring-boot-starter-artemis'
runtimeOnly 'io.micrometer:micrometer-registry-prometheus'

My application.properties contains the following settings:

spring.artemis.mode=embedded
spring.artemis.password=test
spring.artemis.user=test
spring.artemis.embedded.enabled=true
spring.artemis.embedded.persistent=true
spring.artemis.embedded.data-directory=./mq-data
spring.artemis.embedded.cluster-password=test
spring.artemis.embedded.topics=module1.out.topic

management.endpoint.health.show-components=always
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=prometheus,health,hawtio
management.server.port=8090

But still the Prometheus endpoint did not provide the metrics of ActiveMQ Artemis.


Solution

  • I posted the question to answer it myself so that someone else can find the solution faster than I did.

    ActiveMQ Artemis provides an interface for a metrics plugin that you can implement. Afterwards you need to configure your embedded ActiveMQ Artemis instance with the plugin.

    How did I do it?

    1. Provide an Implementation of the ActiveMQMetricsPlugin

      Here is a simple implementation of the class that allows you to pass in the MeterRegistry that Spring Boot Micrometer autoconfiguration provides:

    class MicrometerActiveMQMetricsPlugin implements ActiveMQMetricsPlugin {
    
      private transient MeterRegistry meterRegistry;
    
      private Map<String, String> options;
    
      private ActiveMQServer server;
    
      public MicrometerActiveMQMetricsPlugin(MeterRegistry meterRegistry) {
        if(meterRegistry == null) {
          throw new IllegalArgumentException("Provided meter registry is null.");
        }
        this.meterRegistry = meterRegistry;
      }
    
      @Override
      public ActiveMQMetricsPlugin init(Map<String, String> options) {
        this.meterRegistry = new SimpleMeterRegistry();
        this.options = options;
        return this;
      }
    
      @Override
      public MeterRegistry getRegistry() {
        return meterRegistry;
      }
    
      @Override
      public void registered(ActiveMQServer server) {
        this.server = server;
      }
    
      public Map<String, String> getOptions() {
        return options;
      }
    
      public ActiveMQServer getServer() {
        return server;
      }
    }
    
    1. Configure ActiveMQ Artemis

      Spring Boot provides an configuration for its auto-configuration that you can use to configure the embedded instance:

    @Configuration
    @Slf4j
    class ArtemisConfigurationCustomizerImpl implements ArtemisConfigurationCustomizer {
    
      @Autowired private MeterRegistry meterRegistry;
      
      @Override
      public void customize(org.apache.activemq.artemis.core.config.Configuration configuration) {
        log.info("############ CONFIGURING METRICS ############");
        MetricsConfiguration metricsConfiguration = new MetricsConfiguration();
        MicrometerActiveMQMetricsPlugin plugin = new MicrometerActiveMQMetricsPlugin(meterRegistry);
    
        metricsConfiguration.setPlugin(plugin);
        configuration.setMetricsConfiguration(metricsConfiguration);
      }
    }
    
    1. Query the Prometheus endpoint
    curl -s localhost:8090/actuator/prometheus | grep -v "#" | grep artemis |sort