Search code examples
dropwizardmetricsgraphitecodahale-metrics

Get the metrics printed on a file instead of sending to the graphite server


I am using dropwizard codahale metrics library to send the metrics data to the graphite server. However, now i have a requirement that these metrics data are to be written to a file instead of being pushed to the graphite server. The data in these files can be later pushed to the graphite server when required.

Is there any way to achieve this? Especially the part where the metrics are to be written to an intermediate file instead of being sent to the server, in such a format, that it can be pushed to the graphite server when necessary?


Solution

  • yes - there is. Codhale comes out of the box with a class called CsvReporter. This one will collect your metrics and write them to disk in a csv format.

    Have a look here: https://dropwizard.github.io/metrics/3.1.0/apidocs/com/codahale/metrics/CsvReporter.html

    You can initialise it like that:

            File file = new File(expandPath(conf.getMetricCsvLocation()));
            if(!file.exists()) {
                log.warn("CSV Metrics location does not exist. Metrics will not be written. Change the file location to an existing location.");
                return;
            }
            reporter = CsvReporter.forRegistry(env.metrics()).build(file);