问题
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.
回答1:
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.
回答2:
I think your issue can be solved using disabledMetricAttributes.
In the accepted answer, this would work better:
if (name.endsWith(whitelisted))
来源:https://stackoverflow.com/questions/42570419/how-to-add-custom-metricfilter-in-graphitereporter-to-send-only-selected-metricd