2017-03-03 35 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分鐘的持續時間是否可行? –

相關問題