下面列出了怎么用com.codahale.metrics.annotation.Metric的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
@InSequence(3)
public void incrementCountersFromInjection(@Metric(name = "ratioGauge", absolute = true) Gauge<Double> gauge,
@Metric(name = "counter1", absolute = true) Counter counter1,
@Metric(name = "counter2", absolute = true) Counter counter2) {
counter1.inc(Math.round(Math.random() * Integer.MAX_VALUE));
counter2.inc(Math.round(Math.random() * Integer.MAX_VALUE));
assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(((double) counter1.getCount()) / ((double) counter2.getCount()))));
assertThat("Gauge is not registered correctly", registry.getGauges(), hasKey("ratioGauge"));
@SuppressWarnings("unchecked")
Gauge<Double> gaugeFromRegistry = (Gauge<Double>) registry.getGauges().get("ratioGauge");
assertThat("Gauge values from registry and injection do not match", gauge.getValue(), is(equalTo(gaugeFromRegistry.getValue())));
}
@Test
@InSequence(2)
public void countedMethodNotCalledYet(@Metric(name = "monotonicCountedMethod", absolute = true) Counter instance) {
assertThat("Counter is not registered correctly", registry.getCounters(), hasKey(COUNTER_NAME));
Counter counter = registry.getCounters().get(COUNTER_NAME);
// Make sure that the counter registered and the bean instance are the same
assertThat("Counter and bean instance are not equal", instance, is(equalTo(counter)));
}
@Test
@InSequence(2)
public void callGaugeAfterSetterCall(@Metric(absolute = true, name = "io.astefanutti.metrics.cdi.se.GaugeMethodBean.gaugeMethod") Gauge<Long> gauge) {
// Call the setter method and assert the gauge is up-to-date
long value = 1L + Math.round(Math.random() * (Long.MAX_VALUE - 1L));
bean.setGauge(value);
assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(value)));
}
@Test
@InSequence(2)
public void countedMethodNotCalledYet(@Metric(name = "countedMethod", absolute = true) Counter instance) {
assertThat("Counter is not registered correctly", registry.getCounters(), hasKey(COUNTER_NAME));
Counter counter = registry.getCounters().get(COUNTER_NAME);
// Make sure that the counter registered and the bean instance are the same
assertThat("Counter and bean instance are not equal", instance, is(equalTo(counter)));
}
@Override
public String of(AnnotatedMember<?> member) {
if (member.isAnnotationPresent(Metric.class)) {
Metric metric = member.getAnnotation(Metric.class);
String name = metric.name().isEmpty() ? member.getJavaMember().getName() : of(metric.name());
return metric.absolute() | extension.<Boolean>getParameter(UseAbsoluteName).orElse(false) ? name : MetricRegistry.name(member.getJavaMember().getDeclaringClass(), name);
} else {
return extension.<Boolean>getParameter(UseAbsoluteName).orElse(false) ? member.getJavaMember().getName() : MetricRegistry.name(member.getJavaMember().getDeclaringClass(), member.getJavaMember().getName());
}
}
private String of(AnnotatedParameter<?> parameter) {
if (parameter.isAnnotationPresent(Metric.class)) {
Metric metric = parameter.getAnnotation(Metric.class);
String name = metric.name().isEmpty() ? getParameterName(parameter) : of(metric.name());
return metric.absolute() | extension.<Boolean>getParameter(UseAbsoluteName).orElse(false) ? name : MetricRegistry.name(parameter.getDeclaringCallable().getJavaMember().getDeclaringClass(), name);
} else {
return extension.<Boolean>getParameter(UseAbsoluteName).orElse(false) ? getParameterName(parameter) : MetricRegistry.name(parameter.getDeclaringCallable().getJavaMember().getDeclaringClass(), getParameterName(parameter));
}
}
@Produces
@Metric(name = "cache-hits")
Gauge<Double> cacheHitRatioGauge(Meter hits, Timer calls) {
return () -> Ratio.of(hits.getCount(), calls.getCount()).getValue();
}
@Produces
@Metric(name = "not_registered_metric")
Counter not_registered_metric(MetricRegistry registry, InjectionPoint ip) {
return registry.counter("not_registered_metric");
}