Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve the duplicate metrics definition issue in Micrometer shim #4457

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a tiny worry that someone will be creating dynamically named meters and making this grow without bound, but I think that's a small worry that probably means they're doing sometime wrong with micrometer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone does that other areas of the metrics SDK will cause memory pressure at a faster pace than this.


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