com.codahale.metrics.annotation.Metric Java Examples

The following examples show how to use com.codahale.metrics.annotation.Metric. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MetricProducerFieldBeanTest.java    From metrics-cdi with Apache License 2.0 6 votes vote down vote up
@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())));
}
 
Example #2
Source File: MonotonicCountedMethodBeanTest.java    From metrics-cdi with Apache License 2.0 5 votes vote down vote up
@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)));
}
 
Example #3
Source File: GaugeInjectionBeanTest.java    From metrics-cdi with Apache License 2.0 5 votes vote down vote up
@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)));
}
 
Example #4
Source File: CountedMethodBeanTest.java    From metrics-cdi with Apache License 2.0 5 votes vote down vote up
@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)));
}
 
Example #5
Source File: SeMetricName.java    From metrics-cdi with Apache License 2.0 5 votes vote down vote up
@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());
    }
}
 
Example #6
Source File: SeMetricName.java    From metrics-cdi with Apache License 2.0 5 votes vote down vote up
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));
    }
}
 
Example #7
Source File: MetricProducerMethodBean.java    From metrics-cdi with Apache License 2.0 4 votes vote down vote up
@Produces
@Metric(name = "cache-hits")
Gauge<Double> cacheHitRatioGauge(Meter hits, Timer calls) {
    return () -> Ratio.of(hits.getCount(), calls.getCount()).getValue();
}
 
Example #8
Source File: MetricProducerMethodBean.java    From metrics-cdi with Apache License 2.0 4 votes vote down vote up
@Produces
@Metric(name = "not_registered_metric")
Counter not_registered_metric(MetricRegistry registry, InjectionPoint ip) {
    return registry.counter("not_registered_metric");
}