/** * 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); }
/** * Configures the specified {@link MeterRegistry} to use the {@link NamingConvention}s provided by this * class. {@link DropwizardMeterRegistry} and {@link PrometheusMeterRegistry} will be configured to use * {@link #dropwizard()} and {@link #prometheus()} respectively. A {@link CompositeMeterRegistry} will be * configured recursively. */ public static void configure(MeterRegistry registry) { requireNonNull(registry, "registry"); if (registry instanceof CompositeMeterRegistry) { ((CompositeMeterRegistry) registry).getRegistries().forEach(MoreNamingConventions::configure); } if (registryTypeMatches(registry, "io.micrometer.core.instrument.dropwizard.DropwizardMeterRegistry")) { registry.config().namingConvention(dropwizard()); } else if (registryTypeMatches(registry, "io.micrometer.prometheus.PrometheusMeterRegistry")) { registry.config().namingConvention(prometheus()); } else { // Probably OK to use the default. } }
@TearDown(Level.Trial) public void tearDown(Blackhole hole) { for (io.micrometer.core.instrument.Meter m : registry.getMeters()) { if (m instanceof io.micrometer.core.instrument.Counter) { hole.consume(((io.micrometer.core.instrument.Counter) m).count()); } } } }
CircuitBreakerMetrics(MeterRegistry parent, MeterIdPrefix idPrefix) { requireNonNull(parent, "parent"); requireNonNull(idPrefix, "idPrefix"); final String requests = idPrefix.name("requests"); parent.gauge(requests, idPrefix.tags("result", "success"), latestEventCount, lec -> lec.get().success()); parent.gauge(requests, idPrefix.tags("result", "failure"), latestEventCount, lec -> lec.get().failure()); final String transitions = idPrefix.name("transitions"); transitionsToClosed = parent.counter(transitions, idPrefix.tags("state", CLOSED.name())); transitionsToOpen = parent.counter(transitions, idPrefix.tags("state", OPEN.name())); transitionsToHalfOpen = parent.counter(transitions, idPrefix.tags("state", HALF_OPEN.name())); rejectedRequests = parent.counter(idPrefix.name("rejectedRequests"), idPrefix.tags()); }
CaffeineMetrics(MeterRegistry parent, MeterIdPrefix idPrefix) { this.parent = requireNonNull(parent, "parent"); this.idPrefix = requireNonNull(idPrefix, "idPrefix"); final String requests = idPrefix.name("requests"); parent.more().counter(requests, idPrefix.tags("result", "hit"), this, func(HIT_COUNT, ref -> ref.cacheStats.hitCount())); parent.more().counter(requests, idPrefix.tags("result", "miss"), this, func(MISS_COUNT, ref -> ref.cacheStats.missCount())); parent.more().counter(idPrefix.name("evictions"), idPrefix.tags(), this, func(EVICTION_COUNT, ref -> ref.cacheStats.evictionCount())); parent.more().counter(idPrefix.name("evictionWeight"), idPrefix.tags(), this, func(EVICTION_WEIGHT, ref -> ref.cacheStats.evictionWeight())); parent.gauge(idPrefix.name("estimatedSize"), idPrefix.tags(), this, func(null, ref -> ref.estimatedSize)); }
@Setup public void setup() { registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); timer = registry.timer("timer"); }
private void setupServerMetrics() { final MeterRegistry meterRegistry = config().meterRegistry(); final GracefulShutdownSupport gracefulShutdownSupport = this.gracefulShutdownSupport; assert gracefulShutdownSupport != null; meterRegistry.gauge("armeria.server.pendingResponses", gracefulShutdownSupport, GracefulShutdownSupport::pendingResponses); meterRegistry.gauge("armeria.server.connections", connectionLimitingHandler, ConnectionLimitingHandler::numConnections); }
@Test @DisplayName("common tags are added to every measurement") void addCommonTags(MeterRegistry registry) { registry.config().commonTags("k", "v"); Counter c = registry.counter("foo"); assertThat(registry.get("foo").tags("k", "v").counter()).isSameAs(c); assertThat(c.getId().getTagsAsIterable()).hasSize(1); }
@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("gauges attached to an object are updated when their values are observed") default void objectGauge(MeterRegistry registry) { List<String> list = registry.gauge("my.gauge", emptyList(), new ArrayList<>(), List::size); list.addAll(Arrays.asList("a", "b")); Gauge g = registry.get("my.gauge").gauge(); assertThat(g.value()).isEqualTo(2); }
@Test public void noOnNextTimerFuseable() { Mono<Integer> source = Mono.just(123); new MonoMetricsFuseable<>(source, registry) .block(); Timer nextMeter = registry .find(METER_ON_NEXT_DELAY) .timer(); assertThat(nextMeter).isNull(); }
@Test public void testClientCustomization() { log.info("--- Starting tests with client customization ---"); final MeterRegistry meterRegistry = new SimpleMeterRegistry(); assertEquals(0, meterRegistry.getMeters().size()); final MetricCollectingClientInterceptor mcci = new MetricCollectingClientInterceptor(meterRegistry, counter -> counter.tag("type", "counter"), timer -> timer.tag("type", "timer").publishPercentiles(0.5, 0.9, 0.99), OK, UNKNOWN); mcci.preregisterService(TestServiceGrpc.getServiceDescriptor()); MetricTestHelper.logMeters(meterRegistry.getMeters()); assertEquals(METHOD_COUNT * 10, meterRegistry.getMeters().size()); final Counter counter = meterRegistry.find(METRIC_NAME_CLIENT_REQUESTS_SENT).counter(); assertNotNull(counter); assertEquals("counter", counter.getId().getTag("type")); final Timer timer = meterRegistry.find(METRIC_NAME_CLIENT_PROCESSING_DURATION).timer(); assertNotNull(timer); assertEquals("timer", timer.getId().getTag("type")); log.info("--- Test completed ---"); }
private void assertMetricsContainsTag(String tagKey, String tagValue) { List<Meter.Id> meterIds = null; try { meterIds = this.meterRegistry.getMeters().stream() .map(Meter::getId) .collect(Collectors.toList()); Collection<Timer> timers = this.meterRegistry.get(REQUEST_METRICS_NAME).timers(); System.err.println("Looking for gateway.requests: tag: " + tagKey + ", value: "+ tagValue); timers.forEach(timer -> System.err.println(timer.getId()+timer.getClass().getSimpleName())); long count = getCount(tagKey, tagValue); assertThat(count).isEqualTo(1); } catch (MeterNotFoundException e) { System.err.println("\n\n\nError finding gatway.requests meter: tag: " + tagKey + ", value: "+ tagValue); System.err.println("\n\n\nMeter ids prior to search: "+meterIds + "\n\n\n and after:"); this.meterRegistry.forEachMeter(meter -> System.err.println(meter.getId() + meter.getClass().getSimpleName())); // try again? long count = getCount(tagKey, tagValue); if (count != 1) { throw e; } } }
@Test @DisplayName("function-tracking counter increments by change in a monotonically increasing function when observed") default void functionTrackingCounter(MeterRegistry registry) { AtomicLong n = new AtomicLong(0); registry.more().counter("tracking", emptyList(), n); n.incrementAndGet(); clock(registry).add(step()); registry.forEachMeter(Meter::measure); assertThat(registry.get("tracking").functionCounter().count()).isEqualTo(1.0); } }
private long getCount(String tagKey, String tagValue) { return this.meterRegistry.get(REQUEST_METRICS_NAME).tag(tagKey, tagValue) .timer().count(); }
@Test @DisplayName("gauges can be directly associated with collection size") default void collectionSizeGauge(MeterRegistry registry) { List<String> list = registry.gaugeCollectionSize("my.gauge", emptyList(), new ArrayList<>()); list.addAll(Arrays.asList("a", "b")); Gauge g = registry.get("my.gauge").gauge(); assertThat(g.value()).isEqualTo(2); }
protected void increment(String key) { this.meterRegistry.counter(key).increment(); }