Search code examples
spring-bootgrailsgrails-3.3micrometer

Register bean with Grails for MeterRegistryCustomizer


Having followed Spring Boot Metrics documentation, I was able to set metrics logging for datadog easily. The only remaining stuff is to set custom tags for my instances. With Spring Boot, you can do it by registering a new bean:

@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
    return registry -> registry.config().commonTags("region", "us-east-1");
}

However, I'm not able to register it in Grails 3. Not in resource.groovy nor in application main class Application.groovy. Is there any way how to set this in Grails 3?


Solution

  • In Grails 3, You should put the below code to grails-app/conf/spring/resources.groovy:

    import io.micrometer.core.instrument.MeterRegistry
    import io.micrometer.spring.autoconfigure.MeterRegistryCustomizer
    
    class CommonTagCustomizer implements MeterRegistryCustomizer<MeterRegistry> {
    
        void customize(MeterRegistry registry) {
            registry.config().commonTags("host", "myapp-dev")
        }
    }
    
    beans = {
        commonTags(CommonTagCustomizer) {}
    }