Skip to content

Commit 0803e4c

Browse files
bprotopopovBoris Protopopovevg-tso
authored
Add default dimensions for OTEL metrics (#19)
* Add default dimensions for OTEL metrics * Add default dimensions for OTEL metrics * Remove empty line added accidentally when merging * Update version and CHANGELOG.md --------- Co-authored-by: Boris Protopopov <pboris@amazon.com> Co-authored-by: Yevgeni Tsodikov <evg.tso@gmail.com>
1 parent a3615b3 commit 0803e4c

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.1.0] - 2025-02-10
9+
10+
### Added
11+
12+
- Add default dimensions for OTEL metrics.
13+
814
## [1.0.1] - 2024-09-19
915

1016
### Changed
@@ -18,6 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1824

1925
- Initial release of the AWS SDK Java OpenTelemetry Metrics library.
2026

27+
[1.1.0]: https://github.com/AppsFlyer/aws-sdk-java-opentelemetry-metrics/compare/aws-sdk-java-opentelemetry-metrics-1.0.1...aws-sdk-java-opentelemetry-metrics-1.1.0
28+
2129
[1.0.1]: https://github.com/AppsFlyer/aws-sdk-java-opentelemetry-metrics/compare/aws-sdk-java-opentelemetry-metrics-1.0.0...aws-sdk-java-opentelemetry-metrics-1.0.1
2230

2331
[1.0.0]: https://github.com/AppsFlyer/aws-sdk-java-opentelemetry-metrics/releases/tag/aws-sdk-java-opentelemetry-metrics-1.0.0

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.appsflyer</groupId>
66
<artifactId>aws-sdk-java-opentelemetry-metrics</artifactId>
7-
<version>1.0.2-SNAPSHOT</version>
7+
<version>1.1.0-SNAPSHOT</version>
88
<name>AWS SDK Java OpenTelemetry Metrics</name>
99
<description>OpenTelemetry Metric Publisher for AWS SDK for Java</description>
1010
<url>https://github.com/AppsFlyer/aws-sdk-java-opentelemetry-metrics</url>

src/main/java/com/appsflyer/otelawsmetrics/OtelMetricPublisher.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
public class OtelMetricPublisher implements MetricPublisher {
2727
private static final Logger log = LoggerFactory.getLogger(OtelMetricPublisher.class);
2828
private static final String DEFAULT_METRIC_PREFIX = "aws.sdk";
29+
private final Attributes baseAttributes;
2930

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

4041
public OtelMetricPublisher(OpenTelemetry openTelemetry) {
41-
this(openTelemetry, DEFAULT_METRIC_PREFIX);
42+
this(openTelemetry, DEFAULT_METRIC_PREFIX, ForkJoinPool.commonPool(), Attributes.empty());
4243
}
4344

4445
public OtelMetricPublisher(OpenTelemetry openTelemetry, String metricPrefix) {
45-
this(openTelemetry, metricPrefix, ForkJoinPool.commonPool());
46+
this(openTelemetry, metricPrefix, ForkJoinPool.commonPool(), Attributes.empty());
4647
}
4748

4849
public OtelMetricPublisher(OpenTelemetry openTelemetry, String metricPrefix, Executor executor) {
50+
this(openTelemetry, metricPrefix, executor, Attributes.empty());
51+
}
52+
53+
public OtelMetricPublisher(OpenTelemetry openTelemetry, String metricPrefix,
54+
Executor executor, Attributes baseAttributes) {
4955
Objects.requireNonNull(metricPrefix, "metricPrefix must not be null");
5056
Objects.requireNonNull(openTelemetry, "openTelemetry must not be null");
57+
Objects.requireNonNull(baseAttributes, "baseAttributes must not be null");
5158

5259
if (executor == null) {
5360
log.warn("An executor is not provided. The metrics will be published synchronously on the calling thread.");
5461
}
55-
5662
this.metricPrefix = metricPrefix + ".";
5763
this.executor = executor;
64+
this.baseAttributes = baseAttributes;
5865

59-
Meter meter = openTelemetry.getMeter("aws.sdk");
66+
Meter meter = openTelemetry.getMeter(this.metricPrefix);
6067

6168
perRequestMetrics = initializePerRequestStrategies(meter);
6269
perAttemptMetrics = initializeCoreStrategies(meter);
@@ -267,6 +274,7 @@ private Attributes toPerRequestAttributes(String operationName, boolean isSucces
267274
.put("request_operation_name", nullSafeOperationName)
268275
.put("request_is_success", isSuccess)
269276
.put("request_retry_count", retryCount)
277+
.putAll(this.baseAttributes)
270278
.build());
271279
}
272280

@@ -291,4 +299,3 @@ private Attributes toHttpAttributes(Attributes parentAttributes, int httpStatusC
291299
.build());
292300
}
293301
}
294-

src/test/java/com/appsflyer/otelawsmetrics/OtelMetricPublisherTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.opentelemetry.api.GlobalOpenTelemetry;
44
import io.opentelemetry.api.common.AttributeKey;
5+
import io.opentelemetry.api.common.Attributes;
56
import io.opentelemetry.sdk.OpenTelemetrySdk;
67
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
78
import io.opentelemetry.sdk.metrics.data.HistogramData;
@@ -30,6 +31,8 @@ class OtelMetricPublisherTest {
3031
private ExecutorService executor;
3132
private InMemoryMetricReader metricReader;
3233
private MetricPublisher metricPublisher;
34+
private Attributes baseAttributes;
35+
private static String basePrefix = "custom.prefix";
3336

3437
@BeforeEach
3538
void setUp() {
@@ -39,6 +42,10 @@ void setUp() {
3942
// Set up an InMemoryMetricReader to capture metrics
4043
metricReader = InMemoryMetricReader.create();
4144

45+
// Setup some base attributes
46+
baseAttributes = Attributes.of(AttributeKey.stringKey("custom.dimension.key.1"), "CustomDimensionValue.1",
47+
AttributeKey.stringKey("custom.dimension.key.2"), "CustomDimensionValue.2");
48+
4249
// Set up the SdkMeterProvider with the metric reader
4350
SdkMeterProvider sdkMeterProvider = SdkMeterProvider.builder()
4451
.registerMetricReader(metricReader)
@@ -53,7 +60,7 @@ void setUp() {
5360
GlobalOpenTelemetry.set(openTelemetrySdk);
5461

5562
// Create an instance of OtelMetricPublisher
56-
metricPublisher = new OtelMetricPublisher(GlobalOpenTelemetry.get(), "aws.sdk", executor);
63+
metricPublisher = new OtelMetricPublisher(GlobalOpenTelemetry.get(), basePrefix, executor, baseAttributes);
5764
}
5865

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

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

@@ -90,6 +97,10 @@ public void testPublishMetrics() throws InterruptedException {
9097
assertEquals("GetItem", point.getAttributes().get(AttributeKey.stringKey("request_operation_name")));
9198
assertEquals(true, point.getAttributes().get(AttributeKey.booleanKey("request_is_success")));
9299
assertEquals(0L, point.getAttributes().get(AttributeKey.longKey("request_retry_count")));
100+
assertEquals("CustomDimensionValue.1", point.getAttributes()
101+
.get(AttributeKey.stringKey("custom.dimension.key.1")));
102+
assertEquals("CustomDimensionValue.2", point.getAttributes()
103+
.get(AttributeKey.stringKey("custom.dimension.key.2")));
93104
}
94105

95106
private MetricCollection createMockMetricCollection() {

0 commit comments

Comments
 (0)