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

Add default dimensions for OTEL metrics #19

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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 @@ -26,6 +26,7 @@
public class OtelMetricPublisher implements MetricPublisher {
private static final Logger log = LoggerFactory.getLogger(OtelMetricPublisher.class);
private static final String DEFAULT_METRIC_PREFIX = "aws.sdk";
private final Attributes baseAttributes;

private final Map<String, Map<Boolean, Map<Integer, Attributes>>> perRequestAttributesCache = new ConcurrentHashMap<>();
private final Map<Attributes, Map<String, Attributes>> perAttemptAttributesCache = new ConcurrentHashMap<>();
Expand All @@ -38,25 +39,31 @@ public class OtelMetricPublisher implements MetricPublisher {
private final Map<String, MetricStrategy> httpMetrics;

public OtelMetricPublisher(OpenTelemetry openTelemetry) {
this(openTelemetry, DEFAULT_METRIC_PREFIX);
this(openTelemetry, DEFAULT_METRIC_PREFIX, ForkJoinPool.commonPool(), Attributes.empty());
}

public OtelMetricPublisher(OpenTelemetry openTelemetry, String metricPrefix) {
this(openTelemetry, metricPrefix, ForkJoinPool.commonPool());
this(openTelemetry, metricPrefix, ForkJoinPool.commonPool(), Attributes.empty());
}

public OtelMetricPublisher(OpenTelemetry openTelemetry, String metricPrefix, Executor executor) {
this(openTelemetry, metricPrefix, executor, Attributes.empty());
}

public OtelMetricPublisher(OpenTelemetry openTelemetry, String metricPrefix,
Executor executor, Attributes baseAttributes) {
Objects.requireNonNull(metricPrefix, "metricPrefix must not be null");
Objects.requireNonNull(openTelemetry, "openTelemetry must not be null");
Objects.requireNonNull(baseAttributes, "baseAttributes must not be null");

if (executor == null) {
log.warn("An executor is not provided. The metrics will be published synchronously on the calling thread.");
}

this.metricPrefix = metricPrefix + ".";
this.executor = executor;
this.baseAttributes = baseAttributes;

Meter meter = openTelemetry.getMeter("aws.sdk");
Meter meter = openTelemetry.getMeter(this.metricPrefix);

perRequestMetrics = initializePerRequestStrategies(meter);
perAttemptMetrics = initializeCoreStrategies(meter);
Expand Down Expand Up @@ -267,6 +274,7 @@ private Attributes toPerRequestAttributes(String operationName, boolean isSucces
.put("request_operation_name", nullSafeOperationName)
.put("request_is_success", isSuccess)
.put("request_retry_count", retryCount)
.putAll(this.baseAttributes)
.build());
}

Expand All @@ -291,4 +299,3 @@ private Attributes toHttpAttributes(Attributes parentAttributes, int httpStatusC
.build());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.data.HistogramData;
Expand Down Expand Up @@ -30,6 +31,8 @@ class OtelMetricPublisherTest {
private ExecutorService executor;
private InMemoryMetricReader metricReader;
private MetricPublisher metricPublisher;
private Attributes baseAttributes;
private static String basePrefix = "custom.prefix";

@BeforeEach
void setUp() {
Expand All @@ -39,6 +42,10 @@ void setUp() {
// Set up an InMemoryMetricReader to capture metrics
metricReader = InMemoryMetricReader.create();

// Setup some base attributes
baseAttributes = Attributes.of(AttributeKey.stringKey("custom.dimension.key.1"), "CustomDimensionValue.1",
AttributeKey.stringKey("custom.dimension.key.2"), "CustomDimensionValue.2");

// Set up the SdkMeterProvider with the metric reader
SdkMeterProvider sdkMeterProvider = SdkMeterProvider.builder()
.registerMetricReader(metricReader)
Expand All @@ -53,7 +60,7 @@ void setUp() {
GlobalOpenTelemetry.set(openTelemetrySdk);

// Create an instance of OtelMetricPublisher
metricPublisher = new OtelMetricPublisher(GlobalOpenTelemetry.get(), "aws.sdk", executor);
metricPublisher = new OtelMetricPublisher(GlobalOpenTelemetry.get(), basePrefix, executor, baseAttributes);
}

@Test
Expand All @@ -75,7 +82,7 @@ public void testPublishMetrics() throws InterruptedException {
assertEquals(1, exportedMetrics.size(), "Expected one metric to be exported");

MetricData metricData = exportedMetrics.get(0);
assertEquals("aws.sdk.api_call_duration", metricData.getName());
assertEquals(basePrefix + ".api_call_duration", metricData.getName());
assertEquals("The total time taken to finish a request (inclusive of all retries)", metricData.getDescription());
assertEquals("ns", metricData.getUnit());

Expand All @@ -90,6 +97,10 @@ public void testPublishMetrics() throws InterruptedException {
assertEquals("GetItem", point.getAttributes().get(AttributeKey.stringKey("request_operation_name")));
assertEquals(true, point.getAttributes().get(AttributeKey.booleanKey("request_is_success")));
assertEquals(0L, point.getAttributes().get(AttributeKey.longKey("request_retry_count")));
assertEquals("CustomDimensionValue.1", point.getAttributes()
.get(AttributeKey.stringKey("custom.dimension.key.1")));
assertEquals("CustomDimensionValue.2", point.getAttributes()
.get(AttributeKey.stringKey("custom.dimension.key.2")));
}

private MetricCollection createMockMetricCollection() {
Expand Down
Loading