Spring Boot allows the declarative configuration of common tags:
Commons tags are applied to all meters and can be configured, as the following example shows:
management.metrics.tags.region=us-east-1
Thus, in Spring Boot, with management.metrics.tags.application=myapp
in place, all metrics include that tag, e.g.:
jvm_memory_max_bytes{application="myapp",area="heap",id="G1 Survivor Space"} -1.0
system_cpu_count{application="myapp"} 16.0
I want to achieve the same in a Micronaut application. There doesn't seem to be a declarative way to do it and instead, we need to programatically customize it using a MeterRegistryConfigurer
.
I created the following:
@Singleton
@RequiresMetrics
class PrometheusMeterRegistryConfigurer: MeterRegistryConfigurer<MeterRegistry> {
override fun configure(meterRegistry: MeterRegistry) {
meterRegistry.config().commonTags("application", "myapp")
}
override fun getType(): Class<MeterRegistry> {
return MeterRegistry::class.java
}
}
The problem is it isn't applying it to all metrics, some metrics are tagged (http_server_requests_seconds_max
) while other aren't (jvm_memory_max_bytes
, system_cpu_count
, etc.):
http_server_requests_seconds_max{application="myapp",exception="none",method="GET",status="200",uri="/actuator/prometheus",} 0.131942121
jvm_memory_max_bytes{area="nonheap",id="CodeHeap 'profiled nmethods'",} 1.22023936E8
system_cpu_count 16.0
How can I make the tag apply to all micrometer-prometheus metrics?
Solved adding two different MeterRegistryConfigurer
s:
@Singleton
@RequiresMetrics
class PrometheusMeterRegistryConfigurer: MeterRegistryConfigurer<PrometheusMeterRegistry> {
override fun configure(meterRegistry: PrometheusMeterRegistry) {
meterRegistry.config().commonTags("application", "myapp")
}
override fun getType(): Class<PrometheusMeterRegistry> {
return PrometheusMeterRegistry::class.java
}
}
@Singleton
@RequiresMetrics
class CompositeMeterRegistryConfigurer: MeterRegistryConfigurer<CompositeMeterRegistry> {
override fun configure(meterRegistry: CompositeMeterRegistry) {
meterRegistry.config().commonTags("application", "myapp")
}
override fun getType(): Class<CompositeMeterRegistry> {
return CompositeMeterRegistry::class.java
}
}