2

I have implemented Dropwizard metrics in my application. I am sending metrics to Graphite using below code.

final Graphite graphite = new Graphite(new InetSocketAddress("xxx.xxx.xxx.xxx", xxxx));
final GraphiteReporter graphiteReporter = GraphiteReporter.forRegistry(metricRegistry)
                .prefixedWith(getReporterRootTagName())
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .filter(MetricFilter.ALL)
                .build(graphite);

        graphiteReporter.start(Integer.parseInt(getTimePeriod()), timeUnit);

I want to add custom MetricFilter so that instead of sending all metrics to Graphite only few specific metrics will be sent.

eg. max,mean,min,mean only.

Please post MetricFilter usage.

Mayank Singh
  • 109
  • 1
  • 11

2 Answers2

3

To achieve this you could implement a metric filter:

class WhitelistMetricFilter implements MetricFilter {
    private final Set<String> whitelist;

    public WhitelistMetricFilter(Set<String> whitelist) {
        this.whitelist = whitelist;
    }

    @Override
    public boolean matches(String name, Metric metric) {
        for (String whitelisted: whitelist) {
            if (whitelisted.endsWith(name))
                return true;
        }
        return false;
    }
}

I suggest checking the names using String#endsWith function as the name you get there is not the full metric name usually (for example it may not contain your prefix). With this filter you can instantiate your reporter:

final MetricFilter whitelistFilter = new WhitelistMetricFilter(whitelist);
final GraphiteReporter reporter = GraphiteReporter
    .forRegistry(metricRegistry)
    .prefixedWith(getReporterRootTagName())
    .filter(whiltelistFilter)
    .build(graphite);

This should do the trick. If you need more refined filtering on the metrics - for example if you need to disable specific metrics reported automatically by timers, then 3.2.0 version introduced this. You can use the disabledMetricAttributes argument to provide a set of attributes that you want to be disabled.

final Set<MetricAttribute> disabled = new HashSet<MetricAttribute>();
disabled.add(MetricAttribute.MAX);

final GraphiteReporter reporter = GraphiteReporter
    .forRegistry(metricRegistry)
    .disabledMetricAttributes(disabled)
    .build(graphite)

I hope this helps you.

divyum
  • 1,286
  • 13
  • 20
Andrei T.
  • 2,455
  • 1
  • 13
  • 28
  • Thanks, I think I didn't post my question correctly. Actually i want to filter the Timer metrics **count mean rate 1-minute rate 5-minute rate 15-minute rate min max mean stddev median 75% 95% 98% 99% 99.9%** I want some way to send only selected ones to Graphite. – Mayank Singh Mar 09 '17 at 04:42
  • Hmm, so you're looking to filter all these out / in? The ones you listed are reported by timers / meters. If you want to filter all these out it should be easy. If you want only a part of them there is a nasty workaround but it should work - so which one is it? – Andrei T. Mar 09 '17 at 09:06
  • Yes you got right, i want only **count, mean, min, max** and moreover is it feasible to have X mins duration in Timer metrics apart from default 1,5,15mins? – Mayank Singh Mar 09 '17 at 09:38
  • What version of codahale metrics are you using? The 3.2.0 GraphiteReporter has an argument `disabledMetricAttributes`. You can use that one :) Let me know if that works for you and I'll edit the answer – Andrei T. Mar 09 '17 at 10:04
  • Thanks, 3.2.0 works :) You seem to have worked a lot in codehale metrics. – Mayank Singh Mar 09 '17 at 10:27
  • I'm glad to hear that. Actually not that much - I just had some nasty issues to solve as well. I'll edit the answer to provide further details :) – Andrei T. Mar 09 '17 at 10:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137654/discussion-between-andrei-t-and-mayank-singh). – Andrei T. Mar 09 '17 at 10:39
0

I think your issue can be solved using disabledMetricAttributes.

In the accepted answer, this would work better:

if (name.endsWith(whitelisted))

hal9000
  • 201
  • 5
  • 25