Skip to content

Commit

Permalink
Avoid conflicts in Micrometer description mapping
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Stäber <[email protected]>
  • Loading branch information
fstab committed Feb 25, 2022
1 parent 938cd46 commit 1c29dfa
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
final class Bridging {

private static final Cache<String, AttributeKey<String>> tagsCache = Cache.bounded(1024);
private static final Cache<String, String> descriptionsCache = Cache.bounded(1024);

static Attributes tagsAsAttributes(Meter.Id id, NamingConvention namingConvention) {
Iterable<Tag> tags = id.getTagsAsIterable();
Expand All @@ -36,9 +37,12 @@ static String name(Meter.Id id, NamingConvention namingConvention) {
return namingConvention.name(id.getName(), id.getType(), id.getBaseUnit());
}

static String description(Meter.Id id) {
String description = id.getDescription();
return description == null ? "" : description;
static String description(String name, Meter.Id id) {
return descriptionsCache.computeIfAbsent(
name, 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 @@ -34,10 +34,11 @@ final class OpenTelemetryCounter implements Counter, RemovableMeter {
this.id = id;

this.attributes = tagsAsAttributes(id, namingConvention);
String conventionName = name(id, namingConvention);
this.otelCounter =
otelMeter
.counterBuilder(name(id, namingConvention))
.setDescription(description(id))
.counterBuilder(conventionName)
.setDescription(description(conventionName, id))
.setUnit(baseUnit(id))
.ofDoubles()
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ final class OpenTelemetryDistributionSummary extends AbstractDistributionSummary
this.otelHistogram =
otelMeter
.histogramBuilder(conventionName)
.setDescription(description(id))
.setDescription(description(conventionName, id))
.setUnit(baseUnit(id))
.build();
this.maxHandle =
asyncInstrumentRegistry.buildGauge(
conventionName + ".max",
description(id),
description(conventionName, id),
baseUnit(id),
attributes,
max,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

package io.opentelemetry.instrumentation.micrometer.v1_5;

import static io.opentelemetry.instrumentation.micrometer.v1_5.Bridging.baseUnit;
import static io.opentelemetry.instrumentation.micrometer.v1_5.Bridging.description;
import static io.opentelemetry.instrumentation.micrometer.v1_5.Bridging.name;
import static io.opentelemetry.instrumentation.micrometer.v1_5.Bridging.tagsAsAttributes;
import static io.opentelemetry.instrumentation.micrometer.v1_5.Bridging.*;

import io.micrometer.core.instrument.FunctionCounter;
import io.micrometer.core.instrument.Measurement;
Expand All @@ -34,10 +31,11 @@ final class OpenTelemetryFunctionCounter<T> implements FunctionCounter, Removabl
AsyncInstrumentRegistry asyncInstrumentRegistry) {
this.id = id;

String conventionName = name(id, namingConvention);
countMeasurementHandle =
asyncInstrumentRegistry.buildDoubleCounter(
name(id, namingConvention),
description(id),
conventionName,
description(conventionName, id),
baseUnit(id),
tagsAsAttributes(id, namingConvention),
obj,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,22 @@ final class OpenTelemetryFunctionTimer<T> implements FunctionTimer, RemovableMet
this.id = id;
this.baseTimeUnit = baseTimeUnit;

String countMeterName = name(id, namingConvention) + ".count";
String totalTimeMeterName = name(id, namingConvention) + ".sum";
String conventionName = name(id, namingConvention);
Attributes attributes = tagsAsAttributes(id, namingConvention);

countMeasurementHandle =
asyncInstrumentRegistry.buildLongCounter(
countMeterName, description(id), /* baseUnit = */ "1", attributes, obj, countFunction);
conventionName + ".count",
description(conventionName, id),
/* baseUnit = */ "1",
attributes,
obj,
countFunction);

totalTimeMeasurementHandle =
asyncInstrumentRegistry.buildDoubleCounter(
totalTimeMeterName,
description(id),
conventionName + ".sum",
description(conventionName, id),
getUnitString(baseTimeUnit),
attributes,
obj,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ final class OpenTelemetryGauge<T> implements Gauge, RemovableMeter {

this.id = id;

String conventionName = name(id, namingConvention);
gaugeMeasurementHandle =
asyncInstrumentRegistry.buildGauge(
name(id, namingConvention),
description(id),
conventionName,
description(conventionName, id),
baseUnit(id),
tagsAsAttributes(id, namingConvention),
obj,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ final class OpenTelemetryLongTaskTimer extends DefaultLongTaskTimer implements R
this.activeTasksHandle =
asyncInstrumentRegistry.buildUpDownLongCounter(
conventionName + ".active",
description(id),
description(conventionName, id),
"tasks",
attributes,
this,
DefaultLongTaskTimer::activeTasks);
this.durationHandle =
asyncInstrumentRegistry.buildUpDownDoubleCounter(
conventionName + ".duration",
description(id),
description(conventionName, id),
getUnitString(baseTimeUnit),
attributes,
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class OpenTelemetryMeter implements Meter, RemovableMeter {
List<AsyncMeasurementHandle> measurementHandles = new ArrayList<>();
for (Measurement measurement : measurements) {
String name = statisticInstrumentName(id, measurement.getStatistic(), namingConvention);
String description = description(id);
String description = description(name, id);
String baseUnit = baseUnit(id);

switch (measurement.getStatistic()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ final class OpenTelemetryTimer extends AbstractTimer implements RemovableMeter {
this.otelHistogram =
otelMeter
.histogramBuilder(conventionName)
.setDescription(description(id))
.setDescription(description(conventionName, id))
.setUnit(getUnitString(baseTimeUnit))
.build();
this.maxHandle =
asyncInstrumentRegistry.buildGauge(
conventionName + ".max",
description(id),
description(conventionName, id),
getUnitString(baseTimeUnit),
attributes,
max,
Expand Down

0 comments on commit 1c29dfa

Please sign in to comment.