Interesting metrics in a message centric system come in several forms and flavors, like count, capacity and latency for example. Axon Framework allows you to retrieve such measurements through the use of the axon-metrics or axon-micrometer module. With these modules you can register a number of MessageMonitor implementations to your messaging components, like the CommandBus, EventBus, QueryBus and EventProcessors.
axon-metrics module uses Dropwizard Metrics for registering the measurements correctly. That means that MessageMonitors are registered against the Dropwizard MetricRegistry.
axon-micrometer module uses Micrometer which is a dimensional-first metrics collection facade whose aim is to allow you to time, count, and gauge your code with a vendor neutral API. That means that MessageMonitors are registered against the Micrometer MeterRegistry.
The following monitor implementations are currently provided:
CapacityMonitor - Measures message capacity by keeping track of the total time spent on message handling compared to total time it is active. This returns a number between 0 and n number of threads. Thus, if there are 4 threads working, the maximum capacity is 4 if every thread is active 100% of the time.
EventProcessorLatencyMonitor - Measures the difference between an event's timestamp and the current time, showing how far behind an event processor is. Note that triggering a reset will impact this metric!
MessageCountingMonitor - Counts the number of ingested, successful, failed, ignored and processed messages.
MessageTimerMonitor - Keeps a timer for all successful, failed and ignored messages, as well as an overall timer for all three combined.
PayloadTypeMessageMonitorWrapper - A special MessageMonitor implementation which allows setting a monitor per message type instead of per message publishing/handling component.
You are free to configure any combination of MessageMonitors through constructors on your messaging components, simply by using the Configuration API. The GlobalMetricRegistry contained in the axon-metrics and axon-micrometer modules provides a set of sensible defaults per type of messaging component. The following example shows you how to configure default metrics for your message handling components:
Dropwizard
publicclassMetricsConfiguration {publicConfigurerbuildConfigurer() {returnDefaultConfigurer.defaultConfiguration(); }// The MetricRegistry is a class from the Dropwizard Metrics frameworkpublicvoidconfigureDefaultMetrics(Configurer configurer,MetricRegistry metricRegistry) {GlobalMetricRegistry globalMetricRegistry =newGlobalMetricRegistry(metricRegistry);// We register the default monitors to our messaging components by doing the followingglobalMetricRegistry.registerWithConfigurer(configurer); }}
# The default value is `true`. Thus you will have Metrics configured if `axon-metrics` and `io.dropwizard.metrics` are on your classpath.
axon.metrics.auto-configuration.enabled=true
Micrometer
publicclassMetricsConfiguration {publicConfigurerbuildConfigurer() {returnDefaultConfigurer.defaultConfiguration(); }// The MeterRegistry is a class from the Micrometer librarypublicvoidconfigureDefaultMetrics(Configurer configurer,MeterRegistry meterRegistry) {GlobalMetricRegistry globalMetricRegistry =newGlobalMetricRegistry(meterRegistry);globalMetricRegistry.registerWithConfigurer(configurer); }}
publicclassMetricsConfiguration {publicConfigurerbuildConfigurer() {returnDefaultConfigurer.defaultConfiguration(); }// The MeterRegistry is a class from the Micrometer librarypublicvoidconfigureDefaultMetrics(Configurer configurer,MeterRegistry meterRegistry) {GlobalMetricRegistry globalMetricRegistry =newGlobalMetricRegistry(meterRegistry);globalMetricRegistry.registerWithConfigurerWithDefaultTags(configurer); }}
# The default value is `true`.
# Thus you will have Metrics configured if `axon-micrometer` and
# appropriate metric implementation (for example: `micrometer-registry-prometheus`) are on your classpath.
axon.metrics.auto-configuration.enabled=true
# Spring Boot metrics enabled
management.endpoint.metrics.enabled=true
# Spring Boot (Prometheus) endpoint (`/actuator/prometheus`) enabled and exposed
management.metrics.export.prometheus.enabled=true
management.endpoint.prometheus.enabled=true
# The default value is `true`.
# Thus you will have Metrics configured if `axon-micrometer` and
# appropriate metric implementation (for example: `micrometer-registry-prometheus`) are on your classpath.
axon.metrics.auto-configuration.enabled=true
# The default value is `false`.
# By enabling this property you will have message (event, command, query)
# payload type set as a micrometer tag/dimension by default.
# Additionally, the processor name will be a tag/dimension instead of it being part of the metric name.
axon.metrics.micrometer.dimensional=true
# Spring Boot metrics enabled
management.endpoint.metrics.enabled=true
# Spring Boot (Prometheus) endpoint (`/actuator/prometheus`) enabled and exposed
management.metrics.export.prometheus.enabled=true
management.endpoint.prometheus.enabled=true
The scenario might occur that more fine-grained control over which MessageMonitor instance are defined is necessary. The following snippet provides as sample if you want to have more specific metrics on any of the message handling components:
// Java (Spring Boot Configuration) - Micrometer example@ConfigurationpublicclassMetricsConfig { @BeanpublicConfigurerModulemetricConfigurer(MeterRegistry meterRegistry) {return configurer -> {instrumentEventStore(meterRegistry, configurer);instrumentEventProcessors(meterRegistry, configurer);instrumentCommandBus(meterRegistry, configurer);instrumentQueryBus(meterRegistry, configurer); }; }privatevoidinstrumentEventStore(MeterRegistry meterRegistry,Configurer configurer) {MessageMonitorFactory messageMonitorFactory = (configuration, componentType, componentName) -> {MessageCountingMonitor messageCounter =MessageCountingMonitor.buildMonitor( componentName, meterRegistry, message ->Tags.of(TagsUtil.PAYLOAD_TYPE_TAG,message.getPayloadType().getSimpleName()).and(message.getMetaData().entrySet().stream().map(s ->Tag.of(s.getKey(),s.getValue().toString())).collect(Collectors.toList())) );// Naming the Timer monitor/meter with the name of the component (eventStore)// Registering the Timer with custom tags: payloadType.MessageTimerMonitor messageTimer =MessageTimerMonitor.buildMonitor( componentName, meterRegistry, message ->Tags.of(TagsUtil.PAYLOAD_TYPE_TAG,message.getPayloadType().getSimpleName()) );returnnewMultiMessageMonitor<>(messageCounter, messageTimer); };configurer.configureMessageMonitor(EventStore.class, messageMonitorFactory); }privatevoidinstrumentEventProcessors(MeterRegistry meterRegistry,Configurer configurer) {MessageMonitorFactory messageMonitorFactory = (configuration, componentType, componentName) -> {// Naming the Counter monitor/meter with the fixed name `eventProcessor`.// Registering the Counter with custom tags: payloadType and processorName.MessageCountingMonitor messageCounter =MessageCountingMonitor.buildMonitor("eventProcessor", meterRegistry, message ->Tags.of(TagsUtil.PAYLOAD_TYPE_TAG,message.getPayloadType().getSimpleName(),TagsUtil.PROCESSOR_NAME_TAG, componentName ) );// Naming the Timer monitor/meter with the fixed name `eventProcessor`.// Registering the Timer with custom tags: payloadType and processorName.MessageTimerMonitor messageTimer =MessageTimerMonitor.buildMonitor("eventProcessor", meterRegistry, message ->Tags.of(TagsUtil.PAYLOAD_TYPE_TAG,message.getPayloadType().getSimpleName(),TagsUtil.PROCESSOR_NAME_TAG, componentName ) );// Naming the Capacity/Gauge monitor/meter with the fixed name `eventProcessor`.// Registering the Capacity/Gauge with custom tags: payloadType and processorName.CapacityMonitor capacityMonitor1Minute =CapacityMonitor.buildMonitor("eventProcessor", meterRegistry, message ->Tags.of(TagsUtil.PAYLOAD_TYPE_TAG,message.getPayloadType().getSimpleName(),TagsUtil.PROCESSOR_NAME_TAG, componentName ) );returnnewMultiMessageMonitor<>(messageCounter, messageTimer, capacityMonitor1Minute); };configurer.configureMessageMonitor(TrackingEventProcessor.class, messageMonitorFactory); }privatevoidinstrumentCommandBus(MeterRegistry meterRegistry,Configurer configurer) {MessageMonitorFactory messageMonitorFactory = (configuration, componentType, componentName) -> {MessageCountingMonitor messageCounter =MessageCountingMonitor.buildMonitor( componentName, meterRegistry, message ->Tags.of(TagsUtil.PAYLOAD_TYPE_TAG,message.getPayloadType().getSimpleName(),"messageId",message.getIdentifier() ) );MessageTimerMonitor messageTimer =MessageTimerMonitor.buildMonitor( componentName, meterRegistry, message ->Tags.of(TagsUtil.PAYLOAD_TYPE_TAG,message.getPayloadType().getSimpleName()) );CapacityMonitor capacityMonitor1Minute =CapacityMonitor.buildMonitor( componentName, meterRegistry, message ->Tags.of(TagsUtil.PAYLOAD_TYPE_TAG,message.getPayloadType().getSimpleName()) );returnnewMultiMessageMonitor<>(messageCounter, messageTimer, capacityMonitor1Minute); };configurer.configureMessageMonitor(CommandBus.class, messageMonitorFactory); }privatevoidinstrumentQueryBus(MeterRegistry meterRegistry,Configurer configurer) {MessageMonitorFactory messageMonitorFactory = (configuration, componentType, componentName) -> {MessageCountingMonitor messageCounter =MessageCountingMonitor.buildMonitor( componentName, meterRegistry, message ->Tags.of(TagsUtil.PAYLOAD_TYPE_TAG,message.getPayloadType().getSimpleName(),"messageId",message.getIdentifier() ) );MessageTimerMonitor messageTimer =MessageTimerMonitor.buildMonitor( componentName, meterRegistry, message ->Tags.of(TagsUtil.PAYLOAD_TYPE_TAG,message.getPayloadType().getSimpleName()) );CapacityMonitor capacityMonitor1Minute =CapacityMonitor.buildMonitor( componentName, meterRegistry, message ->Tags.of(TagsUtil.PAYLOAD_TYPE_TAG,message.getPayloadType().getSimpleName()) );returnnewMultiMessageMonitor<>(messageCounter, messageTimer, capacityMonitor1Minute); };configurer.configureMessageMonitor(QueryBus.class, messageMonitorFactory); }}