Frage

We are using dropwizard that includes the codehale's Metrics library. I want to use this library for reporting some metrics from a Java application. We have a custom metrics reporting engine that processes metrics emitted by our internal applications but expects the Metrics to have a certain JSON format. What is the best way of making the Metrics library to report metrics in a custom JSON format? Will this require making a code change to the Metrics library? Or do I have to write a custom translator application?

I could not find anything about custom reporting in Metrics manual or on google. This also brings me to the question "is this the right thing to do in the first place?"

Any suggestions/ideas are welcome. Thanks.

War es hilfreich?

Lösung

Metrics comes with metrics-json, which features two reusable modules for Jackson.

This allows for the serialization of all metric types and health checks to a standard, easily-parsable JSON format.

Here is the package on GitHub: https://github.com/dropwizard/metrics/tree/master/metrics-json

You can also build your own JSON data with the data in the metrics instances.

You can send your JSON through HTTP or wathever you are using to send data to your metrics engine.

Andere Tipps

Here is an example of JsonSl4jReporter

public class JsonSl4jReporter extends ScheduledReporter {

    private static final Logger logger = LoggerFactory.getLogger(JsonSl4jReporter.class);

    private final ObjectMapper  mapper;

    public static JsonSl4jReporter create(MetricRegistry registry) {
        return new JsonSl4jReporter(registry, MetricFilter.ALL, TimeUnit.SECONDS, TimeUnit.MILLISECONDS);
    }

    private JsonSl4jReporter(MetricRegistry registry, MetricFilter filter, TimeUnit rateUnit, TimeUnit durationUnit) {
        super(registry, "json-sl4j-reporter", filter, rateUnit, durationUnit);
        // from metrics-json
        MetricsModule metricsModule = new MetricsModule(rateUnit, durationUnit, false, filter);
        // jackson mapper
        mapper = new ObjectMapper().registerModule(metricsModule);
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
    }

    @Override
    public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters, SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters, SortedMap<String, Timer> timers) {
        log(gauges);
        log(counters);
        log(histograms);
        log(meters);
        log(timers);
    }

    private void log(Map<String, ?> data){

        if(data.isEmpty()) return;

        try {
            String json = mapper.writeValueAsString(data);
            logger.debug(json);
        } catch (JsonProcessingException e) {
            logger.error(e);
        }
    }

}

As you can see, you just have to configure the ObjectMapper using MetricModul from metrics-json

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top