@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()))); }
@Inject public MeterResource( Meter unnamedMeter, @Metric(name = "com.metric") Meter metricMeter, @Metric(name = "com.absoluteMetric", absolute = true) Meter absoluteMetricMeter ) { unnamedMeter.mark(123); metricMeter.mark(456); absoluteMetricMeter.mark(789); }
@Inject public HistogramResource( Histogram unnamedHistogram, @Metric(name = "com.metric") Histogram metricHistogram, @Metric(name = "com.absoluteMetric", absolute = true) Histogram absoluteMetricHistogram ) { unnamedHistogram.update(123); metricHistogram.update(456); absoluteMetricHistogram.update(789); }
@Inject @Timed public TimerResource( Timer unnamedTimer, @Metric(name = "com.metric") Timer metricTimer, @Metric(name = "com.absoluteMetric", absolute = true) Timer absoluteMetricTimer ) { unnamedTimer.update(123, TimeUnit.DAYS); metricTimer.update(123, TimeUnit.MICROSECONDS); absoluteMetricTimer.update(123, TimeUnit.MILLISECONDS); }
@Inject public CounterResource( Counter unnamedCounter, @Metric(name = "com.metric") Counter metricCounter, @Metric(name = "com.absoluteMetric", absolute = true) Counter absoluteMetricCounter ) { unnamedCounter.inc(); metricCounter.inc(2); absoluteMetricCounter.inc(3); }
@Produces @Metric(name = "cache-hits") Gauge<Double> cacheHitRatioGauge(final @Metric(name = "hits") Meter hits, final @Metric(name = "calls") Timer calls) { return new RatioGauge() { @Override protected Ratio getRatio() { return Ratio.of(hits.getCount(), calls.getCount()); } }; }
@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() | parameters.contains(useAbsoluteName) ? name : MetricRegistry.name(member.getJavaMember().getDeclaringClass(), name); } else { return parameters.contains(useAbsoluteName) ? 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() | parameters.contains(useAbsoluteName) ? name : MetricRegistry.name(parameter.getDeclaringCallable().getJavaMember().getDeclaringClass(), name); } else { return parameters.contains(useAbsoluteName) ? getParameterName(parameter) : MetricRegistry.name(parameter.getDeclaringCallable().getJavaMember().getDeclaringClass(), getParameterName(parameter)); } }
@Produces @Metric(name = "success-ratio") // Register a custom gauge that's the ratio of the 'success' meter on the 'generated' meter Gauge<Double> successRatio(Meter success, Meter generated) { return () -> Ratio.of(success.getOneMinuteRate(), generated.getOneMinuteRate()).getValue(); }
@Produces @Metric(name = "success-ratio") Gauge<Double> successRatio(Meter generated, Meter success) { return () -> Ratio.of(success.getOneMinuteRate(), generated.getOneMinuteRate()).getValue(); }
@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"); }