2

我已经在我的应用程序中实现了 Dropwizard 指标。我正在使用以下代码向 Graphite 发送指标。

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,而只会发送几个特定的​​指标。

例如。最大值、平均值、最小值、平均值。

请发布 MetricFilter 使用情况。

4

2 回答 2

3

为此,您可以实现一个指标过滤器:

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)

我希望这可以帮助你。

于 2017-03-08T17:14:58.710 回答
0

我认为您的问题可以使用 disabledMetricAttributes 解决。

在接受的答案中,这会更好:

if (name.endsWith(whitelisted))

于 2018-09-25T16:11:14.147 回答