From 91fb7aee4d4ced9a00bc6d1b8c5671d95de292f8 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Tue, 10 May 2022 16:11:34 +0200 Subject: [PATCH] Solve the duplicate metrics definition issue in Micrometer shim --- .../opentelemetry/micrometer1shim/Bridging.java | 12 ++++++++++-- .../micrometer1shim/FunctionCounterTest.java | 15 +++------------ .../opentelemetry/micrometer1shim/GaugeTest.java | 3 --- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/micrometer1-shim/src/main/java/io/opentelemetry/micrometer1shim/Bridging.java b/micrometer1-shim/src/main/java/io/opentelemetry/micrometer1shim/Bridging.java index 7a3bec635d2..1c632457ef8 100644 --- a/micrometer1-shim/src/main/java/io/opentelemetry/micrometer1shim/Bridging.java +++ b/micrometer1-shim/src/main/java/io/opentelemetry/micrometer1shim/Bridging.java @@ -11,9 +11,13 @@ import io.micrometer.core.instrument.config.NamingConvention; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.AttributesBuilder; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; final class Bridging { + private static final ConcurrentMap descriptionsCache = new ConcurrentHashMap<>(); + static Attributes tagsAsAttributes(Meter.Id id, NamingConvention namingConvention) { Iterable tags = id.getTagsAsIterable(); if (!tags.iterator().hasNext()) { @@ -33,8 +37,12 @@ static String name(Meter.Id id, NamingConvention namingConvention) { } static String description(Meter.Id id) { - String description = id.getDescription(); - return description != null ? description : ""; + return descriptionsCache.computeIfAbsent( + id.getName(), + n -> { + String description = id.getDescription(); + return description != null ? description : ""; + }); } static String baseUnit(Meter.Id id) { diff --git a/micrometer1-shim/src/test/java/io/opentelemetry/micrometer1shim/FunctionCounterTest.java b/micrometer1-shim/src/test/java/io/opentelemetry/micrometer1shim/FunctionCounterTest.java index 9ded1bdc39c..ec213a030c7 100644 --- a/micrometer1-shim/src/test/java/io/opentelemetry/micrometer1shim/FunctionCounterTest.java +++ b/micrometer1-shim/src/test/java/io/opentelemetry/micrometer1shim/FunctionCounterTest.java @@ -58,14 +58,14 @@ void testFunctionCounter() { @Test @SuppressLogger(MetricStorageRegistry.class) - void functionCountersWithSameNameAndDifferentDescriptions() { + void functionCountersWithSameNameAndDifferentTags() { FunctionCounter.builder("testFunctionCounterWithTags", num, AtomicLong::get) .description("First description") .tags("tag", "1") .baseUnit("items") .register(Metrics.globalRegistry); FunctionCounter.builder("testFunctionCounterWithTags", anotherNum, AtomicLong::get) - .description("Second description") + .description("ignored") .tags("tag", "2") .baseUnit("items") .register(Metrics.globalRegistry); @@ -84,16 +84,7 @@ void functionCountersWithSameNameAndDifferentDescriptions() { point -> point .hasValue(12) - .hasAttributes(attributeEntry("tag", "1")))), - metric -> - assertThat(metric) - .hasName("testFunctionCounterWithTags") - .hasDescription("Second description") - .hasUnit("items") - .hasDoubleSumSatisfying( - sum -> - sum.isMonotonic() - .hasPointsSatisfying( + .hasAttributes(attributeEntry("tag", "1")), point -> point .hasValue(13) diff --git a/micrometer1-shim/src/test/java/io/opentelemetry/micrometer1shim/GaugeTest.java b/micrometer1-shim/src/test/java/io/opentelemetry/micrometer1shim/GaugeTest.java index f358bd3aca6..b0fd7717d19 100644 --- a/micrometer1-shim/src/test/java/io/opentelemetry/micrometer1shim/GaugeTest.java +++ b/micrometer1-shim/src/test/java/io/opentelemetry/micrometer1shim/GaugeTest.java @@ -14,7 +14,6 @@ import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import java.lang.ref.WeakReference; import java.util.concurrent.atomic.AtomicLong; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -57,8 +56,6 @@ void testGauge() { } @Test - // TODO(anuraaga): Enable after https://github.com/open-telemetry/opentelemetry-java/pull/4222 - @Disabled void gaugesWithSameNameAndDifferentTags() { Gauge.builder("testGaugeWithTags", () -> 12) .description("First description wins")