2017-03-03 42 views
0

我在我的应用程序中实现了Dropwizard指标。我使用下面的代码向Graphite发送指标。如何在GraphiteReporter中添加自定义MetricFilter以仅发送选定的公制

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,这样,而不是发送所有指标在石墨只有少数特定的指标将被发送。

例如。最大,平均,最小,仅意味着。

请发布MetricFilter用法。

回答

1

要做到这一点,你可以实现一个度量滤波器:

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功能名称你没有完整的度量名称通常的名称(例如,它可能不包含您字首)。有了这个过滤器,你可以实例化你的记者:

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

这应该是诀窍。如果您需要对指标进行更精细的过滤 - 例如,如果您需要禁用定时器自动报告的特定指标,则3.2.0版本引入了此指标。您可以使用disabledMetricAttributes参数来提供一组您希望禁用的属性。

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

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

我希望这可以帮助你。

+0

谢谢,我想我没有正确发布我的问题。实际上我要筛选的定时器指标**计数 平均速率 1分钟速率 5分钟的速率 15分钟的速率 分钟 最大 意味着 STDDEV 位数 75% 95% 98% 99 % 99.9%**我想要某种方式只将选定的人发送给Graphite。 –

+0

嗯,所以你想要过滤所有这些出/入?你列出的是按计时器/米报告的。如果你想过滤所有这些应该很容易。如果你只想要其中的一部分,这是一个令人讨厌的解决方法,但它应该工作 - 那它是哪一个? –

+0

是的,你是对的,我只想**计数,平均数,最小值,最大值**,并且除了默认的1,5,15分钟之外,在计时器指标中有X分钟的持续时间是否可行? –

相关问题