Search code examples
spring-bootdropwizardgraphitecodahale-metrics

Dropwizard Counter Not Retaining value in Spring Boot App


I am trying to register the # of spring boot apps I've in my pvt cloud environment. Logic is to use Counter metric to increment during startUp and decrement during shut down. All the different deployments will publish to the same metricPreFix(--assumption).
Following is the graph I get in Graphite:Counter vs Time

#application.properties
spring.metrics.export.delay-millis=100

Why do I see the value to come down to 0 even when the app is running? I have tried with 2 different implementations with same result. Can someone please point out the gap in my understanding? PFB the code

@Component
public class AppStartupBean implements CommandLineRunner {

private static final String appMetricName = "MyApp.currentCount.GraphOne";
private static final String metricName = "MyApp.currentCount.GraphTwo";

@Autowired
DropwizardMetricServices dwMetricService;

@Autowired
private MetricRegistry registry;

@Override
public void run(String... arg0) throws Exception {
    dwMetricService.increment(appMetricName);

    Counter counter = registry.counter(metricName);
    counter.inc();
}
}

Solution

  • The configuration for DropwizardMetricServices was wrong. I was using

    @Bean
    public DropwizardMetricServices dwMetricService(MetricRegistry registry) {
         return new DropwizardMetricServices(registry);
    }
    

    Instead we should just @Autowire DropwizardMetricServices as needed. PFB

    When Dropwizard metrics are in use, the default CounterService and GaugeService are replaced with a DropwizardMetricServices, which is a wrapper around the MetricRegistry (so you can @Autowired one of those services and use it as normal).