@Test public void testBasicGauge() { BasicGauge<Double> c = new BasicGauge<>(CONFIG, () -> 42.0); register(c); PolledMeter.update(registry); assertEquals(42.0, registry.gauge(ID).value(), 1e-12); }
@Test public void testNumberGauge() { Number n = 42.0; NumberGauge c = new NumberGauge(CONFIG, n); register(c); PolledMeter.update(registry); assertEquals(42.0, registry.gauge(ID).value(), 1e-12); }
@Test public void testCustomCounter() { CustomCounter c = new CustomCounter(); register(c); c.increment(); PolledMeter.update(registry); assertEquals(1, registry.counter(ID).count()); }
@Test public void testPeakRateCounterRemove() { PeakRateCounter c = new PeakRateCounter(CONFIG); DefaultMonitorRegistry.getInstance().register(c); DefaultMonitorRegistry.getInstance().unregister(c); c.increment(); PolledMeter.update(registry); assertEquals(0, registry.stream().count()); }
@Test public void testMinGaugeRemove() { MinGauge g = new MinGauge(CONFIG); DefaultMonitorRegistry.getInstance().register(g); DefaultMonitorRegistry.getInstance().unregister(g); g.update(42); PolledMeter.update(registry); assertEquals(0, registry.stream().count()); }
@Test public void testPeakRateCounter() { PeakRateCounter c = new PeakRateCounter(CONFIG); DefaultMonitorRegistry.getInstance().register(c); c.increment(); PolledMeter.update(registry); assertEquals(1.0, registry.gauge(ID).value()); }
@Test public void testAnnotatedGauge() { AnnotateExample ex = new AnnotateExample("foo"); PolledMeter.update(registry); Id id = registry.createId("gauge") .withTag("class", "AnnotateExample") .withTag("level", "INFO") .withTag("id", "foo"); assertEquals(42.0, registry.gauge(id).value(), 1e-12); }
@Test public void testAnnotatedCounter() { AnnotateExample ex = new AnnotateExample("foo"); PolledMeter.update(registry); Id id = registry.createId("counter") .withTag("class", "AnnotateExample") .withTag("level", "INFO") .withTag("id", "foo"); assertEquals(1, registry.counter(id).count()); }
@Test public void testMinGauge() { ManualClock clock = new ManualClock(0); MinGauge g = new MinGauge(CONFIG, clock); DefaultMonitorRegistry.getInstance().register(g); g.update(42); clock.set(60000); PolledMeter.update(registry); assertEquals(42.0, registry.gauge(ID).value()); }
@Test public void testGuage() { DefaultRegistry registry = new DefaultRegistry(); SpectatorMetricRegistry metricRegistry = new SpectatorMetricRegistry(registry, registry.createId("foo")); metricRegistry.registerGauge("bar", () -> 10); PolledMeter.update(registry); Assert.assertEquals(10.0, registry.gauge(registry.createId("foo.bar")).value(), 0); }
@Test public void testUnregister() { DefaultRegistry registry = new DefaultRegistry(); SpectatorMetricRegistry metricRegistry = new SpectatorMetricRegistry(registry, registry.createId("foo")); metricRegistry.registerGauge("bar", () -> 10); metricRegistry.registerGauge("bar", () -> 20); PolledMeter.update(registry); Assert.assertEquals(20.0, registry.gauge(registry.createId("foo.bar")).value(), 0); } }
private void assertGaugeValue( MetricsRegistry r, MetricRegistry codaRegistry, String name, double expected) { PolledMeter.update(r); Assertions.assertEquals(expected, (Double) codaRegistry.getGauges().get(name).getValue(), 1e-12); }
private static List<Measurement> getAllMeasurements(Registry registry) { PolledMeter.update(registry); final List<Measurement> result = new ArrayList<>(); registry.stream() .filter(meter -> !meter.hasExpired()) .forEach(meter -> meter.measure().forEach(result::add)); return result; }
private Meter getMeter(String meterName, String threadPoolName) { ThreadPoolMonitor.attach(registry, latchedExecutor, threadPoolName); PolledMeter.update(registry); final Id id = registry.createId(meterName).withTag(ThreadPoolMonitor.ID_TAG_NAME, (threadPoolName == null || threadPoolName.isEmpty()) ? ThreadPoolMonitor.DEFAULT_ID : threadPoolName); return registry.get(id); }
private void assertGaugeValue(Registry r, Id id, double expected) { PolledMeter.update(r); Assertions.assertEquals(expected, r.gauge(id).value(), 1e-12); }
@Test public void uncaughtExceptionFromGaugeFunction() { Assertions.assertThrows(RuntimeException.class, () -> { Registry registry = new DefaultRegistry(); PolledMeter.using(registry) .withName("test") .monitorValue(new RuntimeException("failure"), value -> { throw value; }); PolledMeter.update(registry); }); } }
private void assertLongTaskTimer(Registry r, Id id, long timestamp, int activeTasks, double duration) { PolledMeter.update(r); Gauge g = r.gauge(id.withTag(Statistic.activeTasks)); Assertions.assertEquals(timestamp, g.measure().iterator().next().timestamp()); Assertions.assertEquals(activeTasks, g.value(), 1.0e-12); g = r.gauge(id.withTag(Statistic.duration)); Assertions.assertEquals(timestamp, g.measure().iterator().next().timestamp()); Assertions.assertEquals(duration, g.value(), 1.0e-12); }
@Test public void testIgnoreNan() { Id id = idB.withTag("tagA", "Z"); Measurement measure = new Measurement(id, 100, Double.NaN); Meter meter = new TestMeter("ignoreZ", measure); DefaultRegistry registry = new DefaultRegistry(clock); registry.register(meter); HashMap<String, MetricValues> expect = new HashMap<>(); PolledMeter.update(registry); Assertions.assertEquals(expect, controller.encodeRegistry(registry, allowAll)); }
@Test public void maxThreadsUpdatesWhenRegistryIsUpdated() { final Gauge gauge = getGauge(ThreadPoolMonitor.MAX_THREADS); Assertions.assertEquals(10.0, gauge.value(), 1e-12); latchedExecutor.setMaximumPoolSize(42); PolledMeter.update(registry); Assertions.assertEquals(42.0, gauge.value(), 1e-12); }
@Test public void corePoolSizeUpdatesWhenRegistryIsUpdated() { final Gauge gauge = getGauge(ThreadPoolMonitor.CORE_POOL_SIZE); Assertions.assertEquals(3.0, gauge.value(), 1e-12); // Must be <= 10 because that is the max pool size used in the test. Starting with // jdk9 the it will validate and fail if trying to set the pool size larger than // the max latchedExecutor.setCorePoolSize(7); PolledMeter.update(registry); Assertions.assertEquals(7.0, gauge.value(), 1e-12); }