private Object map(final Metric metric) { if (Counter.class.isInstance(metric)) { return Counter.class.cast(metric).getCount(); } if (Gauge.class.isInstance(metric)) { return Gauge.class.cast(metric).getValue(); } return metric; }
private <T> Optional<T> getGaugeValue(String name, Class<T> resultType) { return getMetric(name, Gauge.class) .map((g) -> resultType.cast(g.getValue())); }
public static void buildGauge(StringBuilder builder, String name, Gauge<?> gauge, String description, Double conversionFactor, String tags, String appendUnit) { // Skip non number values Number gaugeValNumber; Object gaugeValue; gaugeValue = gauge.getValue(); if (!Number.class.isInstance(gaugeValue)) { log.info("Skipping Prometheus output for Gauge: " + name + " of type " + gauge.getValue().getClass()); return; } gaugeValNumber = (Number) gaugeValue; if (!(Double.isNaN(conversionFactor))) { gaugeValNumber = gaugeValNumber.doubleValue() * conversionFactor; } getPromTypeLine(builder, name, "gauge", appendUnit); getPromHelpLine(builder, name, description, appendUnit); getPromValueLine(builder, name, gaugeValNumber, tags, appendUnit); }
@Test @InSequence(1) public void gaugeCalledWithDefaultValue() { // Make sure that the gauge has the expected value assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(0L))); }
@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( @Metric(absolute = true, name = "io.astefanutti.metrics.cdi.se.GaugeInjectionBean.gaugeInjection") 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(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)); }
@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 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())); }
@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)); } }
@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 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(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 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())))); }
@Override public void serialize(Metric metric, JsonGenerator json, SerializerProvider provider) throws IOException { if(metric instanceof Gauge) { json.writeObject(((Gauge) metric).getValue()); } else if(metric instanceof Counter) { json.writeObject(((Counter) metric).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())))); }
final Object val = Gauge.class.cast(entry.metric).getValue(); if (Number.class.isInstance(val)) { final String key = toPrometheusKey(entry.metadata);