/** * Returns the {@link Gauge} matching the criteria from the injection point. * * @param <T> type of the {@code Gauge} * @param registry metric registry * @param ip injection point being resolved * @return requested gauge */ @Produces @VendorDefined @SuppressWarnings("unchecked") private <T> Gauge<T> produceGauge(MetricRegistry registry, InjectionPoint ip) { Metric metric = ip.getAnnotated().getAnnotation(Metric.class); return (Gauge<T>) registry.getGauges().entrySet().stream() .filter(entry -> entry.getKey().equals(metric.name())) .findFirst() .orElseThrow(() -> new IllegalArgumentException("Could not produce Gauge for injection point " + ip.toString())) .getValue(); }
/** * Register a gauge with nanos as unit. Checks if gauge is already registered * using synchronization. * * @param metricName Name of metric. * @param description Description of gauge. * @return The gauge created or existing if already created. */ @SuppressWarnings("unchecked") static synchronized <T> Gauge<T> registerGauge(Method method, String metricName, String description, Gauge<T> gauge) { String name = String.format(METRIC_NAME_TEMPLATE, method.getDeclaringClass().getName(), method.getName(), metricName); Gauge<T> existing = getMetricRegistry().getGauges().get(name); if (existing == null) { getMetricRegistry().register(new Metadata(name, name, description, MetricType.GAUGE, MetricUnits.NANOSECONDS), gauge); } return existing; } }
public void gaugeMe() { @SuppressWarnings("unchecked") Gauge<Integer> gaugeManual = metrics.getGauges().get("tck.gaugetest.gaugemanual"); if (gaugeManual == null) { gaugeManual = value::getAndIncrement; metrics.register("tck.gaugetest.gaugemanual", gaugeManual); } }
public void gaugeMe() { @SuppressWarnings("unchecked") Gauge<Long> gauge = metrics.getGauges().get("metricTest.test1.gauge"); if (gauge == null) { gauge = () -> { return 19L; }; Metadata gaugeMetadata = new Metadata("metricTest.test1.gauge",MetricType.GAUGE, MetricUnits.GIGABYTES); metrics.register("metricTest.test1.gauge", gauge, gaugeMetadata); } }
@Test @InSequence(1) public void gaugesCalledWithDefaultValues() { assertThat("Gauges are not registered correctly", registry.getGauges(), allOf(hasKey(PARENT_GAUGE_NAME), hasKey(CHILD_GAUGE_NAME))); @SuppressWarnings("unchecked") Gauge<Long> parentGauge = registry.getGauges().get(PARENT_GAUGE_NAME); @SuppressWarnings("unchecked") Gauge<Long> childGauge = registry.getGauges().get(CHILD_GAUGE_NAME); // Make sure that the gauge has the expected value assertThat("Gauge values are incorrect", Arrays.asList(parentGauge.getValue(), childGauge.getValue()), contains(0L, 0L)); }
@Test public void testManualGauge() { Assert.assertNull(metrics.getGauges().get("tck.gaugetest.gaugemanual")); gaugeMe(); Assert.assertEquals(0, (metrics.getGauges().get("tck.gaugetest.gaugemanual").getValue())); Assert.assertEquals(1, (metrics.getGauges().get("tck.gaugetest.gaugemanual").getValue())); }
/** * Returns the {@link Gauge} matching the criteria from the injection point. * * @param <T> type of the {@code Gauge} * @param registry metric registry * @param ip injection point being resolved * @return requested gauge */ @Produces @VendorDefined @SuppressWarnings("unchecked") private <T> Gauge<T> produceGauge(MetricRegistry registry, InjectionPoint ip) { Metric metric = ip.getAnnotated().getAnnotation(Metric.class); return (Gauge<T>) registry.getGauges().entrySet().stream() .filter(entry -> entry.getKey().equals(metric.name())) .findFirst() .orElseThrow(() -> new IllegalArgumentException("Could not produce Gauge for injection point " + ip.toString())) .getValue(); }
@SuppressWarnings("unchecked") @Produces public <T> Gauge<T> produceGauge(InjectionPoint injectionPoint) { Metadata m = AnnotationMetadata.buildProducerMetadata(injectionPoint); return () -> (T) applicationRegistry.getGauges().get(m.getName()).getValue(); } }
@Test @InSequence(2) public void callGaugeAfterSetterCall() { assertThat("Gauge is not registered correctly", registry.getGauges(), hasKey(GAUGE_NAME)); @SuppressWarnings("unchecked") Gauge<Long> gauge = registry.getGauges().get(GAUGE_NAME); // Call the setter method and assert the gauge is up-to-date long value = Math.round(Math.random() * Long.MAX_VALUE); bean.setGauge(value); assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(value))); } }
@Test @InSequence(1) public void gaugeCalledWithDefaultValue() { assertThat("Gauge is not registered correctly", registry.getGauges(), hasKey(GAUGE_NAME)); @SuppressWarnings("unchecked") Gauge<Long> gauge = registry.getGauges().get(GAUGE_NAME); // Make sure that the gauge has the expected value assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(0L))); }
@Test @InSequence(2) public void callGaugesAfterSetterCalls() { assertThat("Gauges are not registered correctly", registry.getGauges(), allOf(hasKey(PARENT_GAUGE_NAME), hasKey(CHILD_GAUGE_NAME))); @SuppressWarnings("unchecked") Gauge<Long> parentGauge = registry.getGauges().get(PARENT_GAUGE_NAME); @SuppressWarnings("unchecked") Gauge<Long> childGauge = registry.getGauges().get(CHILD_GAUGE_NAME); // Call the setter methods and assert the gauges are up-to-date long parentValue = Math.round(Math.random() * Long.MAX_VALUE); pBean.setGauge(parentValue); long childValue = Math.round(Math.random() * Long.MAX_VALUE); bean.setChildGauge(childValue); assertThat("Gauge values are incorrect", Arrays.asList(parentGauge.getValue(), childGauge.getValue()), contains(parentValue, childValue)); } }
@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()))); } }
@AroundConstruct private Object gaugeBeanConstructor(InvocationContext context) throws Exception { Object target = context.proceed(); Class<?> type = context.getConstructor().getDeclaringClass(); do { for (Method method : type.getDeclaredMethods()) { if(method.isAnnotationPresent(org.eclipse.microprofile.metrics.annotation.Gauge.class)) { Metadata metadata = AnnotationMetadata.buildMetadata(type, method, org.eclipse.microprofile.metrics.annotation.Gauge.class); Gauge gauge = applicationRegistry.getGauges().get(metadata.getName()); if (gauge == null) { applicationRegistry.register(metadata, new ForwardingGauge(method, context.getTarget())); } } } type = type.getSuperclass(); } while (!Object.class.equals(type)); return target; } }
@Test @InSequence(2) public void incrementCountersFromRegistry() { assertThat("Counters are not registered correctly", registry.getCounters(), allOf( hasKey("counter1"), hasKey("counter2"), not(hasKey("not_registered_counter")) ) ); Counter counter1 = registry.getCounters().get("counter1"); Counter counter2 = registry.getCounters().get("counter2"); assertThat("Gauge is not registered correctly", registry.getGauges(), hasKey("ratioGauge")); @SuppressWarnings("unchecked") Gauge<Double> gauge = (Gauge<Double>) registry.getGauges().get("ratioGauge"); 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())))); }
@Test @InSequence(1) public void countersNotIncrementedYet() { assertThat("Counters are not registered correctly", registry.getCounters(), allOf( hasKey("counter1"), hasKey("counter2"), not(hasKey("not_registered_counter")) ) ); Counter counter1 = registry.getCounters().get("counter1"); Counter counter2 = registry.getCounters().get("counter2"); assertThat("Gauge is not registered correctly", registry.getGauges(), hasKey("ratioGauge")); @SuppressWarnings("unchecked") Gauge<Double> gauge = (Gauge<Double>) registry.getGauges().get("ratioGauge"); assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(((double) counter1.getCount()) / ((double) counter2.getCount())))); }
@Test @InSequence(2) public void callMetricsMethodOnce() { assertThat("Metrics are not registered correctly", registry.getMetrics().keySet(), is(equalTo(absoluteMetricNames()))); // Call the monitored method and assert it's been instrumented bean.metricsMethod(); // Make sure that the metrics have been called assertThat("Counter count is incorrect", registry.getCounters().get(absoluteMetricName("counter")).getCount(), is(equalTo(1L))); assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("meter")).getCount(), is(equalTo(1L))); assertThat("Timer count is incorrect", registry.getTimers().get(absoluteMetricName("timer")).getCount(), is(equalTo(1L))); // Let's call the gauge at the end as Weld is intercepting the gauge // invocation while OWB not assertThat("Gauge value is incorrect", registry.getGauges().get(absoluteMetricName("gauge")).getValue(), hasToString((equalTo("value")))); } }
@Test @InSequence(2) public void callCachedMethodMultipleTimes() { assertThat("Metrics are not registered correctly", registry.getMetrics(), allOf( hasKey(CALLS_METRIC), hasKey(HITS_METRIC), hasKey(CACHE_HITS_METRIC) ) ); Timer calls = registry.getTimers().get(CALLS_METRIC); Meter hits = registry.getMeters().get(HITS_METRIC); @SuppressWarnings("unchecked") Gauge<Double> gauge = (Gauge<Double>) registry.getGauges().get(CACHE_HITS_METRIC); long count = 10 + Math.round(Math.random() * 10); for (int i = 0; i < count; i++) { bean.cachedMethod((Math.random() < 0.5)); } assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo((double) hits.getCount() / (double) calls.getCount()))); } }
@Test @InSequence(1) public void cachedMethodNotCalledYet() { assertThat("Metrics are not registered correctly", registry.getMetrics(), allOf( hasKey(CALLS_METRIC), hasKey(HITS_METRIC), hasKey(CACHE_HITS_METRIC) ) ); Timer calls = registry.getTimers().get(CALLS_METRIC); Meter hits = registry.getMeters().get(HITS_METRIC); @SuppressWarnings("unchecked") Gauge<Double> gauge = (Gauge<Double>) registry.getGauges().get(CACHE_HITS_METRIC); assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(((double) hits.getCount() / (double) calls.getCount())))); }