获取打印在文件上的指标而不是发送到石墨服务器

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

我正在使用 dropwizard codahale 指标库将指标数据发送到 Graphite 服务器。但是,现在我要求将这些指标数据写入文件而不是推送到石墨服务器。这些文件中的数据可以稍后在需要时推送到石墨服务器。

有什么办法可以实现吗? 特别是指标要写入中间文件而不是发送到服务器的部分,以这样的格式,以便在必要时可以将其推送到石墨服务器?

是的 - 有。 Codhale 开箱即用,带有一个名为 CsvReporter 的 class。这个将收集您的指标并将它们以 csv 格式写入磁盘。

看这里:https://dropwizard.github.io/metrics/3.1.0/apidocs/com/codahale/metrics/CsvReporter.html

你可以这样初始化它:

        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);