如何在GraphiteReporter中添加自定义MetricFilter以仅发送选定的指标 [英] How to add custom MetricFilter in GraphiteReporter to send only selected metricd
问题描述
我已经在我的应用程序中实现了Dropwizard指标。我正在使用下面的代码向Graphite发送指标。
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);
我想添加自定义MetricFilter,以便将所有度量标准发送到Graphite而不是将所有度量标准发送给Graphite
I want to add custom MetricFilter so that instead of sending all metrics to Graphite only few specific metrics will be sent.
例如。
请发布MetricFilter用法。
Please post MetricFilter usage.
推荐答案
要实现此目标,您可以实施指标过滤器:
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;
}
}
我建议使用 String#endsWith
函数用作您获得的名称,通常没有完整的度量标准名称(例如,它可能不包含您的前缀)。使用此过滤器,您可以实例化您的报告程序:
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);
这应该可以解决问题。如果您需要对指标进行更精细的过滤-例如,如果您需要禁用计时器自动报告的特定指标,则 3.2.0 版本对此进行了介绍。您可以使用 disabledMetricAttributes
参数提供一组要禁用的属性。
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)
希望对您有所帮助。
这篇关于如何在GraphiteReporter中添加自定义MetricFilter以仅发送选定的指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!