/** * Creates call meter with the given name and registers it in * the specified collector registry * * @param name - metric name * @param help - metric help * @param registry - collector registry * @return the call meter */ static CallMeter ofCollectorRegistry(String name, String help, CollectorRegistry registry) { return of(name, help).register(registry); }
/** * Decorates and executes the decorated Callable. * * @param callable the original Callable * @param <T> the type of results supplied by this Callable * @return the result of the decorated Callable. */ default <T> T executeCallable(Callable<T> callable) throws Exception { return CallMeter.decorateCallable(this, callable).call(); }
/** * Decorates and executes the decorated CompletionStage Supplier. * * @param supplier the CompletionStage Supplier * @param <T> the type of results supplied by this supplier * @return the result of the decorated Supplier. */ default <T> CompletionStage<T> executeCompletionStageSupplier(Supplier<CompletionStage<T>> supplier){ return CallMeter.decorateCompletionStageSupplier(this, supplier).get(); }
final CallMeter timer = CallMeter.ofCollectorRegistry("some_call", "Some help", registry); timer.executeRunnable(() -> { try { Thread.sleep(50);
/** * Registers this call meter with the given registry. */ CallMeter register(CollectorRegistry registry);
/** * Creates call meter with the given name and help message * * @param name - metric name * @param help - metric help * @return the call meter */ static CallMeter of(String name, String help) { return CallMeter .builder() .name(name) .help(help) .build(); }
/** * Decorates and executes the decorated Runnable. * * @param runnable the original Callable */ default void executeRunnable(Runnable runnable) throws Exception { CallMeter.decorateRunnable(this, runnable).run(); }
/** * Decorates and executes the decorated Supplier. * * @param supplier the original Supplier * @param <T> the type of results supplied by this supplier * @return the result of the decorated Supplier. */ default <T> T executeSupplier(Supplier<T> supplier){ return CallMeter.decorateSupplier(this, supplier).get(); }
@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); }