void add(Cache<?, ?> cache, Ticker ticker) { synchronized (cacheRefs) { for (CacheReference ref : cacheRefs) { if (ref.get() == cache) { // Do not aggregate more than once for the same instance. return; } } cacheRefs.add(new CacheReference(cache, ticker)); } if (cache instanceof LoadingCache && hasLoadingCache.compareAndSet(false, true)) { // Add the following meters only for LoadingCache and only once. final String loads = idPrefix.name("loads"); parent.more().counter(loads, idPrefix.tags("result", "success"), this, func(LOAD_SUCCESS_COUNT, ref -> ref.cacheStats.loadSuccessCount())); parent.more().counter(loads, idPrefix.tags("result", "failure"), this, func(LOAD_FAILURE_COUNT, ref -> ref.cacheStats.loadFailureCount())); parent.more().counter(idPrefix.name("loadDuration"), idPrefix.tags(), this, func(TOTAL_LOAD_TIME, ref -> ref.cacheStats.totalLoadTime())); } }
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)); }
private AtomicLong getPartitionLatencyHolder(int partition) { return partitionLatencies.computeIfAbsent(partition, k -> { Tags tags = roadStreamTags.and("partition", Integer.toString(k)); AtomicLong latencyHolder = new AtomicLong(); registry.more().timeGauge(OFFRAMP + LATENCY, tags, latencyHolder, MILLISECONDS, AtomicLong::doubleValue); return latencyHolder; }); }
@Test @DisplayName("original and convention names are preserved for custom meter types") void aTaleOfTwoNames(MeterRegistry registry) { AtomicInteger n = new AtomicInteger(1); registry.more().counter("my.counter", Collections.emptyList(), n); registry.get("my.counter").functionCounter(); }
@Test @DisplayName("function timers respect the base unit of an underlying registry") void functionTimerUnits(MeterRegistry registry) { Object o = new Object(); registry.more().timer("function.timer", emptyList(), o, o2 -> 1, o2 -> 1, TimeUnit.MILLISECONDS); FunctionTimer ft = registry.get("function.timer").functionTimer(); clock(registry).add(step()); assertThat(ft.measure()) .anySatisfy(ms -> { TimeUnit baseUnit = TimeUnit.valueOf(requireNonNull(ft.getId().getBaseUnit()).toUpperCase()); assertThat(ms.getStatistic()).isEqualTo(Statistic.TOTAL_TIME); assertThat(TimeUtils.convert(ms.getValue(), baseUnit, TimeUnit.MILLISECONDS)).isEqualTo(1); }); }
@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); } }
@Test public void unknownGet() { meterRegistry.more().longTaskTimer("foo").record(() -> clock.addSeconds(60)); Assertions.assertNull(registry.get(registry.createId("foo"))); }
@Test @DisplayName("total time is preserved for a single timing") default void record(MeterRegistry registry) { LongTaskTimer t = registry.more().longTaskTimer("myTimer"); LongTaskTimer.Sample sample = t.start(); clock(registry).add(10, TimeUnit.NANOSECONDS); assertAll(() -> assertEquals(10, t.duration(TimeUnit.NANOSECONDS)), () -> assertEquals(0.01, t.duration(TimeUnit.MICROSECONDS)), () -> assertEquals(10, sample.duration(TimeUnit.NANOSECONDS)), () -> assertEquals(0.01, sample.duration(TimeUnit.MICROSECONDS)), () -> assertEquals(1, t.activeTasks())); clock(registry).add(10, TimeUnit.NANOSECONDS); sample.stop(); assertAll(() -> assertEquals(0, t.duration(TimeUnit.NANOSECONDS)), () -> assertEquals(-1, sample.duration(TimeUnit.NANOSECONDS)), () -> assertEquals(0, t.activeTasks())); } }