Search code examples
javaspringmetricsstatsdmicrometer

Supporting multiple metrics via prometheus (statsd and prometheus)


I am in the middle of moving the metrics collection system from statsd to Prometheus. It is a spring boot app and based on a micrometer. I am wondering if there is a way to start supporting both hierarchical statsd metrics and also dimensional Prometheus metrics at the same time.

Currently, the way I create a timer is

Metrics.timer("apiName.endpointName.latency").record(latency)

This is fine for statsd but does not work for Prometheus. I need to support a separate metric with tags such as

service_latency {api="apiName", endpoint="endpointName"}

Is it possible to support both at the same time?


Solution

  • You can support both at the same time. The recommended way is

    Metrics.timer("latency", Tags.of("api", apiName, "endpoint", endpointName.)).record(latency)
    

    Then use a 'hierarchical naming convention'

    That way the statsd tags are placed in the hierarchy as you need them, and the Prometheus metrics are already tagged and ready to be used.

    Alternative (not recommended)

    An alternative (and less appealing approach) would be to duplicate the meter between the 2 meter repositories.

    Currently when you use Metrics.timer("myMeter") you are using the global composite meter registry, which delegates the meters to the underlying registries.

    Instead if you call each registry individually:

    statsdRegistry.timer("myMeter")...
    prometheusRegistry.timer("myMeter")...
    

    Unfortunately that will require more manual changes on your part and a duplication of code between the registries. But, you DO have that as an option.