Skip to content

Commit

Permalink
Solve the duplicate metrics definition issue in Micrometer shim (open…
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek authored May 10, 2022
1 parent 6606020 commit a9968bc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> descriptionsCache = new ConcurrentHashMap<>();

static Attributes tagsAsAttributes(Meter.Id id, NamingConvention namingConvention) {
Iterable<Tag> tags = id.getTagsAsIterable();
if (!tags.iterator().hasNext()) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit a9968bc

Please sign in to comment.