我该如何添加自定义指标到Dropwizard提供的默认内容中,以便在管理端口(8081)上查看?除了添加健康检查之外,文档中似乎没有其他相关信息。我想将一些从MongoDB检索的统计数据整合进来,并且更愿意将其与8081上的管理资源集成,而不是在8080端口上创建自定义指标页面。
我该如何添加自定义指标到Dropwizard提供的默认内容中,以便在管理端口(8081)上查看?除了添加健康检查之外,文档中似乎没有其他相关信息。我想将一些从MongoDB检索的统计数据整合进来,并且更愿意将其与8081上的管理资源集成,而不是在8080端口上创建自定义指标页面。
这里有一个例子。在该JVM实例中,每个指标都通过JMX公开。您还可以注册度量报告器,这些报告器将在间隔期间执行操作,例如将所有指标转储到日志中或定时发送到Graphite。
//this creates or returns the metrics, basically every metric is only created once and registered in a registry
private final Timer timerCanMakeHold =
Metrics.newTimer(MyClass.class, "METRICNAME", TimeUnit.MILLISECONDS, TimeUnit.SECONDS);
final TimerContext timerContex = timerCanMakeHold.time();
try{
doSomeWork()//this is what you are timing
}finally{
timerContex.stop();
}
final Graphite graphite = new Graphite(new InetSocketAddress("graphite.url.example", 2003));
MetricRegistry metrics = new MetricRegistry();
GraphiteReporter reporter = GraphiteReporter.forRegistry(metrics)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build(graphite);
reporter.start(1, TimeUnit.SECONDS);
Counter counter = metrics.counter("nameOfCounter");
counter.inc();
对于ivy,您需要将以下内容添加到您的ivy.xml文件中:
<dependency org="io.dropwizard" name="dropwizard-metrics" rev="0.7.1"/>
<dependency org="io.dropwizard" name="dropwizard-metrics-graphite" rev="0.7.1"/>
metrics:
reporters:
- type: console
timeZone: UTC
output: stdout
durationUnit: milliseconds
rateUnit: seconds
frequency: 120 seconds
- type: graphite
host: localhost
port: 9090
prefix: test.prefix
MetricRegistry metrics = new MetricRegistry();
MetricsFactory mfac = configuration.getMetricsFactory();
mfac.configure(environment.lifecycle(), metrics);
Counter counter = metrics.counter("nameOfCounter");
counter.inc();
counter.inc();
counter.inc();
counter.inc();
counter.inc();
test.prefix.nameOfCounter.count 5 1411562372
run
方法中的度量注册表的方法是environment.metrics()
。 - Geraint Ballinger