'Publish Spring MVC Metrics To Multiple Monitoring System Simultaneously Using Micrometer
I have a use case wherein I want to publish my spring boot API metrics to Datadog & CloudWatch simultaneously.
I have added the below dependencies to my pom.
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-statsd</artifactId>
<version>${micrometer.version}</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-cloudwatch</artifactId>
<version>${micrometer.version}</version>
</dependency>
Main Application class
@SpringBootApplication
public class MyApplication {
@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
return registry -> registry.config().commonTags("my-tag", "my-common-tag");
}
}
I have added all required properties in the application. properties as well.
I can see metrics are being published to both Datadog & CloudWatch with default metrics name http.server.request
But I want the metrics name for Datadog to be different & for this, I have added the below property.
management.metrics.web.server.requests-metric-name = i.want.to.be.different
But this is changing the name for both CloudWatch & Datadog.
My question is, how can I change the default metrics name for Datadog only or keep words different for both.
Solution 1:[1]
Micrometer uses MeterFilters registered with a MeterRegistry to modified the meters that are registered. The modifications include the ability to map a meter's ID to something different.
In Spring Boot, you can use a MeterRegistryCustomizer bean to add a MeterFilter to a registry. You can use generics to work with a registry of a specific type, for example MeterRegistryCustomizer<DatadogMeterRegistry> for a customizer that is only interested in customizing the Datadog registry.
Putting this together, you can map the ID of the http.server.request meter to i.want.to.be.different using the following bean:
@Bean
MeterRegistryCustomizer<DatadogMeterRegistry> datadogMeterIdCustomizer() {
return (registry) -> registry.config().meterFilter(new MeterFilter() {
@Override
public Id map(Id id) {
if ("http.server.request".equals(id.getName())) {
return id.withName("i.want.to.be.different");
}
return id;
}
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andy Wilkinson |
