我正在尝试为DropWizard Metrics创建自定义报告器,但找不到任何显示如何通过扩展ScheduledReporter
来创建一个的资源或示例。我可以通过跟踪现有的记者(CSV,SLF4J等)扩展这个类,但是我在哪里注册它?
以下是我尝试注册的自定义Reporter的摘录:
public class DbReporter extends ScheduledReporter {
private final Connection connection;
private final Clock clock;
public static DbReporter.Builder forRegistry(MetricRegistry registry) {
return new DbReporter.Builder(registry);
}
private DbReporter(MetricRegistry registry, Connection connection, Clock clock, MetricFilter filter, TimeUnit rateUnit, TimeUnit durationUnit) {
super(registry, "db-reporter", filter, rateUnit, durationUnit);
this.connection = connection;
this.clock = clock;
}
//...
public static class Builder {
private final MetricRegistry registry;
private Connection connection;
private TimeUnit rateUnit;
private TimeUnit durationUnit;
private MetricFilter filter;
private Clock clock;
private Builder(MetricRegistry registry) {
this.registry = registry;
this.connection = null;
this.rateUnit = TimeUnit.SECONDS;
this.durationUnit = TimeUnit.MILLISECONDS;
this.filter = MetricFilter.ALL;
this.clock = Clock.defaultClock();
}
public DbReporter.Builder outputTo(Connection connection) {
this.connection = connection;
return this;
}
public DbReporter.Builder convertRatesTo(TimeUnit rateUnit) {
this.rateUnit = rateUnit;
return this;
}
public DbReporter.Builder convertDurationsTo(TimeUnit durationUnit) {
this.durationUnit = durationUnit;
return this;
}
public DbReporter.Builder filter(MetricFilter filter) {
this.filter = filter;
return this;
}
public DbReporter build() {
return new DbReporter(this.registry, this.connection, this.clock, this.filter, this.rateUnit, this.durationUnit);
}
}
}
而且,这是创建自定义Reporter的正确方法还是有其他方式?
提前致谢.-
答案 0 :(得分:0)
是的,你可以用同样的方式实现它们。至于注册,通常在DW实例化度量标准后在主应用程序中完成。
例如,在你的run方法中:
@Override
public void run(final Configuration configuration,
final Environment environment) {
log.info(String.format("Starting %s server with version: %s", getName(), configuration.getVersion()));
CsvReporter.forRegistry(environment.metrics()).build(new File("path/to/csv"));
}
希望有所帮助,
阿图尔