下面列出了怎么用com.codahale.metrics.Reporter的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void activateService() {
metricRegistry = new MetricRegistry();
healthCheckRegistry = new HealthCheckRegistry();
CodahaleMetricsDeclaration declaration = descriptor.metaInfo( CodahaleMetricsDeclaration.class );
prefix = declaration.prefix() != null ? declaration.prefix() : app.name();
fqcn = declaration.fqcn();
if( declaration.jmx() )
{
JmxReporter jmxReporter = JmxReporter.forRegistry( metricRegistry ).build();
jmxReporter.start();
reporters.add( jmxReporter );
}
for( Function<MetricRegistry, Reporter> reporterFactory : declaration.reportersFactories())
{
reporters.add( reporterFactory.apply( metricRegistry ) );
}
}
@Override
public synchronized Reporter getReporter(MetricRegistry registry) {
if (null == reporter) {
Graphite graphite = new Graphite(new InetSocketAddress(graphiteURL, graphitePort));
reporter = GraphiteReporter.forRegistry(registry)
.prefixedWith(prefix)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.filter(MetricFilter.ALL)
.build(graphite);
}
return reporter;
}
@Override
public synchronized Reporter getReporter(MetricRegistry registry) {
if (null == reporter) {
reporter = Slf4jReporter.forRegistry(registry)
.outputTo(LoggerFactory.getLogger(prefix))
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
}
return reporter;
}
@Override
public synchronized Reporter getReporter(MetricRegistry registry) {
if (null == reporter) {
reporter = JmxReporter.forRegistry(registry).build();
}
return reporter;
}
@Override
public synchronized Reporter getReporter(MetricRegistry registry) {
if (null == reporter) {
reporter = ConsoleReporter.forRegistry(registry)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
}
return reporter;
}
@Test
public void test() {
MetricRegistry registry = new MetricRegistry();
Reporter r = new DefaultConsoleReporterFactory().getReporter(registry);
assertNotNull(r);
r = new DefaultJMXReporterFactory().getReporter(registry);
assertNotNull(r);
r = new DefaultSLF4jReporterFactory().getReporter(registry);
assertNotNull(r);
r = new DefaultGraphiteReporterFactory("test", "fakeurl.com", 4242).getReporter(registry);
assertNotNull(r);
r = new RiposteGraphiteReporterFactory("test", "fakeurl.com", 4242).getReporter(registry);
}
@Override
public Reporter getReporter(MetricRegistry registry) {
return this;
}
@Override
public Reporter getReporter(MetricRegistry registry) {
return this;
}
@Override
public Reporter getReporter(MetricRegistry registry) {
return this;
}
@Override
public Reporter getReporter(MetricRegistry registry) {
return this;
}
public List<Function<MetricRegistry, Reporter>> reportersFactories()
{
return reportersFactories;
}
public CodahaleMetricsAssembler withReporter( Function<MetricRegistry, Reporter> factory )
{
declaration.reportersFactories.add( factory );
return this;
}
/**
* Get the singleton instance of the Reporter wrapped by this ReporterFactory
*
* @param registry
* The MetricRegistry that the Reporter will be reporting on
*/
Reporter getReporter(MetricRegistry registry);