Skip to content

Commit

Permalink
Avoid conflicts in Micrometer description mapping (#5452)
Browse files Browse the repository at this point in the history
* Avoid conflicts in Micrometer description mapping

Signed-off-by: Fabian Stäber <[email protected]>

* fix formatting

* Update instrumentation/micrometer/micrometer-1.5/library/src/main/java/io/opentelemetry/instrumentation/micrometer/v1_5/Bridging.java

Co-authored-by: Mateusz Rzeszutek <[email protected]>

Co-authored-by: Mateusz Rzeszutek <[email protected]>
  • Loading branch information
fstab and Mateusz Rzeszutek authored Mar 8, 2022
1 parent 4a98dae commit b6a5f28
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 21 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,14 @@ 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;
// TODO: remove the cache usage once the SDK is able to handle different descriptions
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 @@ -34,10 +34,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 b6a5f28

Please sign in to comment.