Skip to content

Commit

Permalink
Instrumentation scope as Micrometer tags in meter state
Browse files Browse the repository at this point in the history
  • Loading branch information
HaloFour committed May 20, 2022
1 parent 7258961 commit f5fe2d4
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.contrib.metrics.micrometer.internal;

import io.micrometer.core.instrument.Tag;

/**
* Constants for common Micrometer {@link Tag} names for the OpenTelemetry instrumentation scope.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public class Constants {
public static final String OTEL_INSTRUMENTATION_NAME = "otel.instrumentation.name";
public static final String OTEL_INSTRUMENTATION_VERSION = "otel.instrumentation.version";
public static final Tag UNKNOWN_INSTRUMENTATION_VERSION_TAG =
Tag.of(OTEL_INSTRUMENTATION_VERSION, "unknown");

private Constants() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@

package io.opentelemetry.contrib.metrics.micrometer.internal.instruments;

import static io.opentelemetry.contrib.metrics.micrometer.internal.instruments.Constants.INSTRUMENTATION_NAME;
import static io.opentelemetry.contrib.metrics.micrometer.internal.instruments.Constants.INSTRUMENTATION_VERSION;
import static io.opentelemetry.contrib.metrics.micrometer.internal.instruments.Constants.UNKNOWN;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.util.StringUtils;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.ObservableDoubleMeasurement;
import io.opentelemetry.api.metrics.ObservableLongMeasurement;
Expand All @@ -38,24 +33,11 @@ abstract class AbstractInstrument {
protected AbstractInstrument(InstrumentState instrumentState) {
this.instrumentState = instrumentState;
this.logger = Logger.getLogger(getClass().getName());
instrumentationNameTag =
instrumentationScopeNameTag(instrumentState.instrumentationScopeName());
instrumentationVersionTag =
instrumentationScopeVersionTag(instrumentState.instrumentationScopeVersion());
this.instrumentationNameTag = instrumentState.instrumentationScopeNameTag();
this.instrumentationVersionTag = instrumentState.instrumentationScopeVersionTag();
this.attributesTagsCache = new ConcurrentHashMap<>();
}

private static Tag instrumentationScopeNameTag(String instrumentationScopeName) {
return Tag.of(INSTRUMENTATION_NAME, instrumentationScopeName);
}

private static Tag instrumentationScopeVersionTag(@Nullable String instrumentationScopeVersion) {
if (StringUtils.isBlank(instrumentationScopeVersion)) {
instrumentationScopeVersion = UNKNOWN;
}
return Tag.of(INSTRUMENTATION_VERSION, instrumentationScopeVersion);
}

protected final MeterRegistry meterRegistry() {
return instrumentState.meterRegistry();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.contrib.metrics.micrometer.internal.state;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.opentelemetry.contrib.metrics.micrometer.CallbackRegistration;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -36,13 +37,12 @@ public MeterRegistry meterRegistry() {
return meterSharedState.meterRegistry();
}

public String instrumentationScopeName() {
return meterSharedState.instrumentationScopeName();
public Tag instrumentationScopeNameTag() {
return meterSharedState.instrumentationScopeNameTag();
}

@Nullable
public String instrumentationScopeVersion() {
return meterSharedState.instrumentationScopeVersion();
public Tag instrumentationScopeVersionTag() {
return meterSharedState.instrumentationScopeVersionTag();
}

public CallbackRegistration registerCallback(Runnable runnable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
package io.opentelemetry.contrib.metrics.micrometer.internal.state;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.util.StringUtils;
import io.opentelemetry.contrib.metrics.micrometer.CallbackRegistration;
import io.opentelemetry.contrib.metrics.micrometer.internal.Constants;
import javax.annotation.Nullable;

/**
Expand All @@ -17,32 +20,38 @@
*/
public final class MeterSharedState {
private final MeterProviderSharedState providerSharedState;
private final String instrumentationScopeName;
@Nullable private final String instrumentationScopeVersion;
private final Tag instrumentationScopeNameTag;
private final Tag instrumentationScopeVersionTag;
@Nullable private final String schemaUrl;

public MeterSharedState(
MeterProviderSharedState providerSharedState,
String instrumentationScopeName,
@Nullable String instrumentationScopeVersion,
@Nullable String schemaUrl) {

this.providerSharedState = providerSharedState;
this.instrumentationScopeName = instrumentationScopeName;
this.instrumentationScopeVersion = instrumentationScopeVersion;
this.instrumentationScopeNameTag =
Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, instrumentationScopeName);
if (StringUtils.isNotBlank(instrumentationScopeVersion)) {
this.instrumentationScopeVersionTag =
Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, instrumentationScopeVersion);
} else {
this.instrumentationScopeVersionTag = Constants.UNKNOWN_INSTRUMENTATION_VERSION_TAG;
}
this.schemaUrl = schemaUrl;
}

public MeterRegistry meterRegistry() {
return providerSharedState.meterRegistry();
}

public String instrumentationScopeName() {
return instrumentationScopeName;
public Tag instrumentationScopeNameTag() {
return instrumentationScopeNameTag;
}

@Nullable
public String instrumentationScopeVersion() {
return instrumentationScopeVersion;
public Tag instrumentationScopeVersionTag() {
return instrumentationScopeVersionTag;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.MeterProvider;
import io.opentelemetry.contrib.metrics.micrometer.internal.Constants;
import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -39,9 +41,10 @@ void createMeter() {
.isInstanceOfSatisfying(
MicrometerMeter.class,
micrometerMeter -> {
assertThat(micrometerMeter.meterSharedState.instrumentationScopeName())
.isEqualTo("name");
assertThat(micrometerMeter.meterSharedState.instrumentationScopeVersion()).isNull();
assertThat(micrometerMeter.meterSharedState.instrumentationScopeNameTag())
.isEqualTo(Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "name"));
assertThat(micrometerMeter.meterSharedState.instrumentationScopeVersionTag())
.isEqualTo(Constants.UNKNOWN_INSTRUMENTATION_VERSION_TAG);
assertThat(micrometerMeter.meterSharedState.schemaUrl()).isNull();
});
}
Expand All @@ -58,10 +61,10 @@ void createMeterWithNameAndVersion() {
.isInstanceOfSatisfying(
MicrometerMeter.class,
micrometerMeter -> {
assertThat(micrometerMeter.meterSharedState.instrumentationScopeName())
.isEqualTo("name");
assertThat(micrometerMeter.meterSharedState.instrumentationScopeVersion())
.isEqualTo("version");
assertThat(micrometerMeter.meterSharedState.instrumentationScopeNameTag())
.isEqualTo(Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "name"));
assertThat(micrometerMeter.meterSharedState.instrumentationScopeVersionTag())
.isEqualTo(Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "version"));
assertThat(micrometerMeter.meterSharedState.schemaUrl()).isNull();
});
}
Expand All @@ -78,9 +81,10 @@ void createMeterWithNameAndSchemaUrl() {
.isInstanceOfSatisfying(
MicrometerMeter.class,
micrometerMeter -> {
assertThat(micrometerMeter.meterSharedState.instrumentationScopeName())
.isEqualTo("name");
assertThat(micrometerMeter.meterSharedState.instrumentationScopeVersion()).isNull();
assertThat(micrometerMeter.meterSharedState.instrumentationScopeNameTag())
.isEqualTo(Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "name"));
assertThat(micrometerMeter.meterSharedState.instrumentationScopeVersionTag())
.isEqualTo(Constants.UNKNOWN_INSTRUMENTATION_VERSION_TAG);
assertThat(micrometerMeter.meterSharedState.schemaUrl()).isEqualTo("schemaUrl");
});
}
Expand All @@ -102,10 +106,10 @@ void createMeterWithNameVersionAndSchemaUrl() {
.isInstanceOfSatisfying(
MicrometerMeter.class,
micrometerMeter -> {
assertThat(micrometerMeter.meterSharedState.instrumentationScopeName())
.isEqualTo("name");
assertThat(micrometerMeter.meterSharedState.instrumentationScopeVersion())
.isEqualTo("version");
assertThat(micrometerMeter.meterSharedState.instrumentationScopeNameTag())
.isEqualTo(Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "name"));
assertThat(micrometerMeter.meterSharedState.instrumentationScopeVersionTag())
.isEqualTo(Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "version"));
assertThat(micrometerMeter.meterSharedState.schemaUrl()).isEqualTo("schemaUrl");
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.opentelemetry.api.metrics.ObservableDoubleCounter;
import io.opentelemetry.context.Context;
import io.opentelemetry.contrib.metrics.micrometer.TestCallbackRegistrar;
import io.opentelemetry.contrib.metrics.micrometer.internal.Constants;
import io.opentelemetry.contrib.metrics.micrometer.internal.state.MeterProviderSharedState;
import io.opentelemetry.contrib.metrics.micrometer.internal.state.MeterSharedState;
import java.util.ArrayList;
Expand Down Expand Up @@ -62,8 +63,8 @@ void add() {
assertThat(id.getName()).isEqualTo("counter");
assertThat(id.getTags())
.containsExactlyInAnyOrder(
Tag.of(Constants.INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.INSTRUMENTATION_VERSION, "1.0"));
Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0"));
assertThat(id.getDescription()).isEqualTo("description");
assertThat(id.getBaseUnit()).isEqualTo("unit");
assertThat(counter.count()).isEqualTo(10.0);
Expand All @@ -90,8 +91,8 @@ void addWithAttributes() {
assertThat(id.getTags())
.containsExactlyInAnyOrder(
Tag.of("key", "value"),
Tag.of(Constants.INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.INSTRUMENTATION_VERSION, "1.0"));
Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0"));
assertThat(id.getDescription()).isEqualTo("description");
assertThat(id.getBaseUnit()).isEqualTo("unit");
assertThat(counter.count()).isEqualTo(10.0);
Expand All @@ -118,8 +119,8 @@ void addWithAttributesAndContext() {
assertThat(id.getTags())
.containsExactlyInAnyOrder(
Tag.of("key", "value"),
Tag.of(Constants.INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.INSTRUMENTATION_VERSION, "1.0"));
Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0"));
assertThat(id.getDescription()).isEqualTo("description");
assertThat(id.getBaseUnit()).isEqualTo("unit");
assertThat(counter.count()).isEqualTo(10.0);
Expand All @@ -146,8 +147,8 @@ void observable() {
assertThat(id.getName()).isEqualTo("counter");
assertThat(id.getTags())
.containsExactlyInAnyOrder(
Tag.of(Constants.INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.INSTRUMENTATION_VERSION, "1.0"));
Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0"));
assertThat(id.getDescription()).isEqualTo("description");
assertThat(id.getBaseUnit()).isEqualTo("unit");
assertThat(counter.count()).isEqualTo(10.0);
Expand Down Expand Up @@ -184,8 +185,8 @@ void observableWithAttributes() {
assertThat(id.getTags())
.containsExactlyInAnyOrder(
Tag.of("key", "value"),
Tag.of(Constants.INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.INSTRUMENTATION_VERSION, "1.0"));
Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0"));
assertThat(id.getDescription()).isEqualTo("description");
assertThat(id.getBaseUnit()).isEqualTo("unit");
assertThat(counter.count()).isEqualTo(10.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.ObservableDoubleGauge;
import io.opentelemetry.contrib.metrics.micrometer.TestCallbackRegistrar;
import io.opentelemetry.contrib.metrics.micrometer.internal.Constants;
import io.opentelemetry.contrib.metrics.micrometer.internal.state.MeterProviderSharedState;
import io.opentelemetry.contrib.metrics.micrometer.internal.state.MeterSharedState;
import java.util.ArrayList;
Expand Down Expand Up @@ -58,8 +59,8 @@ void observable() {
assertThat(id.getName()).isEqualTo("gauge");
assertThat(id.getTags())
.containsExactlyInAnyOrder(
Tag.of(Constants.INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.INSTRUMENTATION_VERSION, "1.0"));
Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0"));
assertThat(id.getDescription()).isEqualTo("description");
assertThat(id.getBaseUnit()).isEqualTo("unit");
assertThat(gauge.value()).isEqualTo(10.0);
Expand Down Expand Up @@ -91,8 +92,8 @@ void observableWithAttributes() {
assertThat(id.getTags())
.containsExactlyInAnyOrder(
Tag.of("key", "value"),
Tag.of(Constants.INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.INSTRUMENTATION_VERSION, "1.0"));
Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0"));
assertThat(id.getDescription()).isEqualTo("description");
assertThat(id.getBaseUnit()).isEqualTo("unit");
assertThat(gauge.value()).isEqualTo(10.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.opentelemetry.api.metrics.DoubleHistogram;
import io.opentelemetry.context.Context;
import io.opentelemetry.contrib.metrics.micrometer.TestCallbackRegistrar;
import io.opentelemetry.contrib.metrics.micrometer.internal.Constants;
import io.opentelemetry.contrib.metrics.micrometer.internal.state.MeterProviderSharedState;
import io.opentelemetry.contrib.metrics.micrometer.internal.state.MeterSharedState;
import java.util.Collections;
Expand Down Expand Up @@ -55,8 +56,8 @@ void add() {
assertThat(id.getName()).isEqualTo("histogram");
assertThat(id.getTags())
.containsExactlyInAnyOrder(
Tag.of(Constants.INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.INSTRUMENTATION_VERSION, "1.0"));
Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0"));
assertThat(id.getDescription()).isEqualTo("description");
assertThat(id.getBaseUnit()).isEqualTo("unit");
assertThat(summary.count()).isEqualTo(1);
Expand Down Expand Up @@ -87,8 +88,8 @@ void addWithAttributes() {
assertThat(id.getTags())
.containsExactlyInAnyOrder(
Tag.of("key", "value"),
Tag.of(Constants.INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.INSTRUMENTATION_VERSION, "1.0"));
Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0"));
assertThat(id.getDescription()).isEqualTo("description");
assertThat(id.getBaseUnit()).isEqualTo("unit");
assertThat(summary.count()).isEqualTo(1);
Expand Down Expand Up @@ -119,8 +120,8 @@ void addWithAttributesAndContext() {
assertThat(id.getTags())
.containsExactlyInAnyOrder(
Tag.of("key", "value"),
Tag.of(Constants.INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.INSTRUMENTATION_VERSION, "1.0"));
Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0"));
assertThat(id.getDescription()).isEqualTo("description");
assertThat(id.getBaseUnit()).isEqualTo("unit");
assertThat(summary.count()).isEqualTo(1);
Expand Down
Loading

0 comments on commit f5fe2d4

Please sign in to comment.