@Test public void testTimer() throws IOException, InterruptedException { Timer t = metricRegistry.timer(name("timer").tag("foo", "bar").build()); Timer.Context time = t.time(); Thread.sleep(1L); time.stop(); // We slept for 1Ms so we ensure that all timers are above 1ms: assertThat(registry.getSampleValue("timer_seconds", new String[]{"foo", "quantile"}, new String[]{"bar", "0.99"})).isGreaterThan(1 / 1000); assertThat(registry.getSampleValue("timer_seconds_count", new String[]{"foo"}, new String[]{"bar"})).isEqualTo(1); assertThat(registry.getSampleValue("timer_seconds_sum", new String[]{"foo"}, new String[]{"bar"})).isEqualTo(-1); assertThat(registry.getSampleValue("timer_meter_m1", new String[]{"foo"}, new String[]{"bar"})).isNotNull(); assertThat(registry.getSampleValue("timer_meter_total", new String[]{"foo"}, new String[]{"bar"})).isNotNull(); assertEquals(Double.valueOf(1.0D), registry.getSampleValue("timer_seconds_count", new String[]{"foo"}, new String[]{"bar"})); assertNotNull("Metric timer_seconds_count should exist", registry.getSampleValue("timer_seconds_count", new String[]{"foo"}, new String[]{"bar"})); assertTypeHelpTimer("timer"); }
@Test public void testHistogram() throws IOException { Histogram hist = metricRegistry.histogram(name("hist").build()); int i = 0; while (i < 100) { hist.update(i); i += 1; } assertEquals(new Double(100), registry.getSampleValue("hist_count")); for (String s : Arrays.asList("0.75", "0.95", "0.98", "0.99")) { assertEquals(Double.valueOf((Double.valueOf(s) - 0.01) * 100), registry.getSampleValue("hist", new String[]{"quantile"}, new String[]{s})); } assertEquals(new Double(99), registry.getSampleValue("hist", new String[]{"quantile"}, new String[]{"0.999"})); }
@Test public void testMeter() throws IOException, InterruptedException { Meter meter = metricRegistry.meter(name("meter").build()); meter.mark(); meter.mark(); assertEquals(new Double(2), registry.getSampleValue("meter_total")); }
@Test public void testInstrumentsSuccessfulCall() throws Exception { // Given final CollectorRegistry registry = new CollectorRegistry(); final CallMeter timer = CallMeter.ofCollectorRegistry("some_call", "Some help", registry); // When timer.executeRunnable(() -> { try { Thread.sleep(50); } catch (InterruptedException e) { fail(); } }); // Then assertThat(registry.getSampleValue( "some_call_total", new String[]{}, new String[]{})) .isEqualTo(1.0); assertThat(registry.getSampleValue( "some_call_failures_total", new String[]{}, new String[]{})) .isEqualTo(0.0); assertThat(registry.getSampleValue( "some_call_latency_count", new String[]{}, new String[]{})) .isEqualTo(1.0); }
assertThat(registry.getSampleValue( "some_call_total", new String[]{}, .isEqualTo(1.0); assertThat(registry.getSampleValue( "some_call_failures_total", new String[]{}, .isEqualTo(1.0); assertThat(registry.getSampleValue( "some_call_latency_count", new String[]{},
@Test public void testCounter() { metricRegistry.counter(name("foo_bar").tag("baz", "qux").build()).inc(); assertEquals(new Double(1), registry.getSampleValue("foo_bar", new String[]{"baz"}, new String[]{"qux"}) ); }
@Test public void testInvalidGaugeType() { Gauge<String> invalidGauge = new Gauge<String>() { @Override public String getValue() { return "foobar"; } }; metricRegistry.register(name("invalid_gauge").build(), invalidGauge); assertThat(registry.getSampleValue("invalid_gauge")).isEqualTo(-1); }
/** * Returns the given value, or null if it doesn't exist. * <p> * This is inefficient, and intended only for use in unittests. */ public Double getSampleValue(String name) { return getSampleValue(name, new String[]{}, new String[]{}); }
assertThat(registry.getSampleValue( "some_call_total", new String[]{ "label_1" }, .isEqualTo(1.0); assertThat(registry.getSampleValue( "some_call_failures_total", new String[]{ "label_1" }, .isEqualTo(1.0); assertThat(registry.getSampleValue( "some_call_latency_count", new String[]{ "label_1" },
assertThat(registry.getSampleValue( "some_call_total", new String[]{ "label_1" }, .isEqualTo(1.0); assertThat(registry.getSampleValue( "some_call_failures_total", new String[]{ "label_1" }, .isEqualTo(0.0); assertThat(registry.getSampleValue( "some_call_latency_count", new String[]{ "label_1" },
@Test public void testExportsCircuitBreakerStates() { // Given final CollectorRegistry registry = new CollectorRegistry(); final Bulkhead bulkhead = Bulkhead.ofDefaults("foo"); BulkheadExports.ofIterable("boo_bulkhead", singletonList(bulkhead)).register(registry); final Supplier<Map<String, Double>> values = () -> HashSet .of("available_concurrent_calls") .map(param -> Tuple.of(param, registry.getSampleValue( "boo_bulkhead", new String[]{"name", "param"}, new String[]{"foo", param}))) .toMap(t -> t); // When final Map<String, Double> initialValues = values.get(); // Then assertThat(initialValues).isEqualTo(HashMap.of("available_concurrent_calls", 25.0)); }
@Test public void testExportsRateLimiterMetrics() { // Given final CollectorRegistry registry = new CollectorRegistry(); final RateLimiter rateLimiter = RateLimiter.ofDefaults("foo"); RateLimiterExports.ofIterable("boo_rate_limiter", singletonList(rateLimiter)).register(registry); final Supplier<Map<String, Double>> values = () -> HashSet .of("available_permissions", "waiting_threads") .map(param -> Tuple.of(param, registry.getSampleValue( "boo_rate_limiter", new String[]{"name", "param"}, new String[]{"foo", param}))) .toMap(t -> t); // When final Map<String, Double> initialValues = values.get(); // Then assertThat(initialValues).isEqualTo(HashMap.of( "available_permissions", 50.0, "waiting_threads", 0.0 )); }
.of("closed", "open", "half_open") .map(state -> Tuple.of(state, registry.getSampleValue( "boo_circuit_breaker_states", new String[]{ "name", "state" },
registry.getSampleValue("integer_gauge", new String[]{"foo"}, new String[]{"bar"})); assertEquals(new Double(1234), registry.getSampleValue("long_gauge", new String[]{"foo"}, new String[]{"bar"})); assertEquals(new Double(1.234), registry.getSampleValue("double_gauge", new String[]{"foo"}, new String[]{"bar"})); assertEquals(new Double(0.1234F), registry.getSampleValue("float_gauge", new String[]{"foo"}, new String[]{"bar"})); assertEquals(new Double(1), registry.getSampleValue("boolean_gauge", new String[]{"foo"}, new String[]{"bar"}));
.of("successful", "failed", "not_permitted", "buffered", "buffered_max") .map(callType -> Tuple.of(callType, registry.getSampleValue( "boo_circuit_breaker_calls", new String[]{ "name", "call_result" },
/** * Returns the given value, or null if it doesn't exist. * <p> * This is inefficient, and intended only for use in unittests. */ public Double getSampleValue(String name) { return getSampleValue(name, new String[]{}, new String[]{}); }
public double getDimensionSum() { return defaultRegistry.getSampleValue(dimensionSumLabel).doubleValue(); }
public double getDimensionCount() { return defaultRegistry.getSampleValue(dimensionCountLabel).doubleValue(); }
private double getQuantile(double q) { return defaultRegistry.getSampleValue(name, QUANTILE_LABEL, new String[] { Collector.doubleToGoString(q) }) .doubleValue(); }