@Setup public void setup() { registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); timer = registry.timer("timer"); }
@Benchmark public int sumTimedWithRegistryLookup() { return registry.timer("timer").record(this::sum); }
/** * Constructs a new Micrometer metrics listener that adds metrics to the given registry with the given list of tags. * * @param meterRegistry the registry to which to add metrics * @param tags an optional collection of tags to attach to metrics; may be empty */ public MicrometerApnsClientMetricsListener(final MeterRegistry meterRegistry, final Iterable<Tag> tags) { this.notificationStartTimes = new ConcurrentHashMap<>(); this.notificationTimer = meterRegistry.timer(NOTIFICATION_TIMER_NAME, tags); this.writeFailures = meterRegistry.counter(WRITE_FAILURES_COUNTER_NAME, tags); this.sentNotifications = meterRegistry.counter(SENT_NOTIFICATIONS_COUNTER_NAME, tags); this.acceptedNotifications = meterRegistry.counter(ACCEPTED_NOTIFICATIONS_COUNTER_NAME, tags); this.rejectedNotifications = meterRegistry.counter(REJECTED_NOTIFICATIONS_COUNTER_NAME, tags); this.connectionFailures = meterRegistry.counter(CONNECTION_FAILURES_COUNTER_NAME, tags); meterRegistry.gauge(OPEN_CONNECTIONS_GAUGE_NAME, tags, openConnections); }
private void endTimerInner(ServerWebExchange exchange, Sample sample) { String outcome = "CUSTOM"; String status = "CUSTOM"; HttpStatus statusCode = exchange.getResponse().getStatusCode(); if (statusCode != null) { outcome = statusCode.series().name(); status = statusCode.name(); } else { // a non standard HTTPS status could be used. Let's be defensive here if (exchange.getResponse() instanceof AbstractServerHttpResponse) { Integer statusInt = ((AbstractServerHttpResponse) exchange.getResponse()) .getStatusCodeValue(); if (statusInt != null) { status = String.valueOf(statusInt); } else { status = "NA"; } } } Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR); Tags tags = Tags.of("outcome", outcome, "status", status, "routeId", route.getId(), "routeUri", route.getUri().toString()); if (log.isTraceEnabled()) { log.trace("Stopping timer 'gateway.requests' with tags " + tags); } sample.stop(meterRegistry.timer("gateway.requests", tags)); } }
private void success(ServerWebExchange exchange, long start) { Iterable<Tag> tags = this.tagsProvider.httpRequestTags(exchange, null); this.registry.timer(this.metricName, tags).record(System.nanoTime() - start, TimeUnit.NANOSECONDS); }
private void error(ServerWebExchange exchange, long start, Throwable cause) { Iterable<Tag> tags = this.tagsProvider.httpRequestTags(exchange, cause); this.registry.timer(this.metricName, tags).record(System.nanoTime() - start, TimeUnit.NANOSECONDS); }
private static Timer timer( MeterRegistry meterRegistry, String interactionModel, SignalType signalType, Tag... tags) { return meterRegistry.timer( "rsocket." + interactionModel, Tags.of(tags).and("signal.type", signalType.name())); } }
public TimedThreadPoolTaskExecutor(MeterRegistry registry, String name, Iterable<Tag> tags) { this.registry = registry; this.name = name; this.tags = tags; this.timer = registry.timer(name, tags); }
@Override public Timer timer(String name, Iterable<Tag> tags) { return meterRegistry.timer(getName(name), tags); }
private void on(final State to) { final State from = state.getAndSet(to); final Sample last = sample.getAndSet(start(registry)); if (from != CLOSED) { last.stop(registry.timer(metricName, tags(from))); } }
protected Object profileMethod(ProceedingJoinPoint pjp) throws Throwable { Timer.Sample sample = Timer.start(meterRegistry); try { return pjp.proceed(); } finally { sample.stop(meterRegistry.timer(MetricsListener.METRIC_NAME, Arrays.asList(// new ImmutableTag("context", getStepIdentifier()), // new ImmutableTag("method", ClassUtils.getShortName(pjp.getTarget().getClass()) + "." + pjp.getSignature().getName())))); } }
void record(final ClientHttpResponse response, final Throwable throwable) throws IOException { final long endTime = clock.monotonicTime(); final Iterable<Tag> tags = concat(defaultTags, generator.tags(arguments, response, throwable)); final Timer timer = registry.timer(metricName, tags); final long duration = endTime - startTime; timer.record(duration, NANOSECONDS); }
@DisplayName("record throwables") @Test default void recordThrowable() { MeterRegistry registry = new SimpleMeterRegistry(); Supplier<String> timed = () -> registry.timer("timer").record(() -> ""); timed.get(); }
@Test default void recordMax(MeterRegistry registry) { Timer timer = registry.timer("my.timer"); timer.record(10, TimeUnit.MILLISECONDS); timer.record(1, TimeUnit.SECONDS); clock(registry).add(step()); // for Atlas, which is step rather than ring-buffer based assertThat(timer.max(TimeUnit.SECONDS)).isEqualTo(1); assertThat(timer.max(TimeUnit.MILLISECONDS)).isEqualTo(1000); //noinspection ConstantConditions clock(registry).add(Duration.ofMillis(step().toMillis() * DistributionStatisticConfig.DEFAULT.getBufferLength())); assertThat(timer.max(TimeUnit.SECONDS)).isEqualTo(0); }
@Test @DisplayName("find meters by name and value") void findMetersByValue(MeterRegistry registry) { Counter c = registry.counter("counter"); c.increment(); Timer t = registry.timer("timer"); t.record(10, TimeUnit.NANOSECONDS); clock(registry).add(step()); assertThat(registry.get("counter").counter().count()).isEqualTo(1.0); assertThat(registry.get("timer").timer().count()).isEqualTo(1L); assertThat(registry.get("timer").timer().totalTime(TimeUnit.NANOSECONDS)).isEqualTo(10.0); }
@Test @DisplayName("record durations") default void recordDuration(MeterRegistry registry) { Timer t = registry.timer("myTimer"); t.record(Duration.ofMillis(42)); clock(registry).add(step()); assertAll(() -> assertEquals(1L, t.count()), () -> assertEquals(42, t.totalTime(TimeUnit.MILLISECONDS), 1.0e-12)); }
protected void handleSentEvent(ExchangeSentEvent sentEvent) { String name = namingStrategy.getName(sentEvent.getExchange(), sentEvent.getEndpoint()); Tags tags = namingStrategy.getTags(sentEvent, sentEvent.getEndpoint()); getMeterRegistry().timer(name, tags).record(sentEvent.getTimeTaken(), TimeUnit.MILLISECONDS); }
@Test @DisplayName("zero times contribute to the count of overall events but do not add to total time") default void recordZero(MeterRegistry registry) { Timer t = registry.timer("myTimer"); t.record(0, TimeUnit.MILLISECONDS); clock(registry).add(step()); assertAll(() -> assertEquals(1L, t.count()), () -> assertEquals(0L, t.totalTime(TimeUnit.NANOSECONDS))); }
@Test @DisplayName("total time and count are preserved for a single timing") default void record(MeterRegistry registry) { Timer t = registry.timer("myTimer"); t.record(42, TimeUnit.MILLISECONDS); clock(registry).add(step()); assertAll(() -> assertEquals(1L, t.count()), () -> assertEquals(42, t.totalTime(TimeUnit.MILLISECONDS), 1.0e-12)); }
@Test @DisplayName("record a runnable task") default void recordWithRunnable(MeterRegistry registry) { Timer t = registry.timer("myTimer"); try { t.record(() -> clock(registry).add(10, TimeUnit.NANOSECONDS)); clock(registry).add(step()); } finally { assertAll(() -> assertEquals(1L, t.count()), () -> assertEquals(10, t.totalTime(TimeUnit.NANOSECONDS), 1.0e-12)); } }