Skip to content

AWS SDK v2 -> OpenTelemetry metrics bridge #13831

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

evg-tso
Copy link

@evg-tso evg-tso commented May 8, 2025

AWS SDK v2 -> OpenTelemetry metrics bridge

This PR adds an OpenTelemetryMetricPublisher that implements
software.amazon.awssdk.metrics.MetricPublisher and converts the internal AWS SDK v2 metric stream into OpenTelemetry instruments.

Why?

  • Today the AWS SDK instrumentation ships traces only.
    Metrics such as ApiCallDuration, RetryCount, or HttpStatusCode (see the official metric list) are available inside the SDK but cannot be exported to OTel without custom plumbing.
  • Bridging these metrics lets clients correlate latency/error-rate spikes in CloudWatch/Prometheus/OTLP-backed dashboards (DataDog in my company's case) with the traces they already get from the OTel Java agent.

Key features

  • Full metric coverage: supports request/attempt/HTTP-level metrics defined by the AWS SDK Developer Guide.
  • Semantic mapping: MetricSpec maps every AWS metric to the correct OpenTelemetry instrument type (histograms, mostly) with unit, description, and attribute set.
  • Low-GC attribute caching: frequently repeated attribute sets (service + operation + status, etc.) are cached to minimize allocations.
  • Configurable: metric-name prefix (aws.sdk by default), executor for asynchronous publishing, and optional static attributes such as env, tenant, and more.
  • Opt-in and independent: works with or without AWS tracing; users simply register the publisher in ClientOverrideConfiguration, with no agent flag required.

Usage

// Minimal – common ForkJoinPool, prefix "aws.sdk"
MetricPublisher publisher = OpenTelemetryMetricPublisher.create(openTelemetry);

S3Client s3 = S3Client.builder()
    .overrideConfiguration(cfg -> cfg.addMetricPublisher(publisher))
    .build();

Optional customization:

Executor executor = Executors.newFixedThreadPool(4);
Attributes staticAttrs = Attributes.of(AttributeKey.stringKey("env"), "prod");

MetricPublisher publisher =
        new OpenTelemetryMetricPublisher(openTelemetry,
                                         "mycompany.aws.sdk",
                                         executor,
                                         staticAttrs);

Open questions (these should be cleared by the maintainers):

Closes #12259.

@evg-tso evg-tso requested a review from a team as a code owner May 8, 2025 12:03
Copy link

linux-foundation-easycla bot commented May 8, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@evg-tso evg-tso changed the title Add OpenTelemetry metrics publisher for AWS SDK instrumentation DRAFT: Add OpenTelemetry metrics publisher for AWS SDK instrumentation May 8, 2025
evg-tso added 3 commits May 8, 2025 17:08
- Added missing AWS SDK metric dependencies to test configuration
- Fixed Javadoc, static method, and error-prone warnings in main and test sources
- Replaced wildcard and unused imports in test code
- Ensured all tests in OpenTelemetryMetricPublisherTest pass successfully
@evg-tso evg-tso force-pushed the aws-sdk-metric-publisher branch from 22b0fa9 to 41e5e3e Compare May 8, 2025 14:08
evg-tso added 4 commits May 8, 2025 20:18
- Move metric publisher classes from internal to metrics package
- Create dedicated test suite for metric publisher tests
- Remove AWS SDK metric dependencies from main test scope
- Add AWS SDK metric dependencies specifically to metric publisher test suite
- Add the main source set as dependency to metric publisher test suite
@evg-tso evg-tso force-pushed the aws-sdk-metric-publisher branch from 9023b71 to 7942f2b Compare May 8, 2025 19:11
@evg-tso evg-tso changed the title DRAFT: Add OpenTelemetry metrics publisher for AWS SDK instrumentation DRAFT: Add OpenTelemetry metrics publisher for AWS SDK v2 May 12, 2025
@evg-tso evg-tso changed the title DRAFT: Add OpenTelemetry metrics publisher for AWS SDK v2 DRAFT: AWS SDK v2 -> OpenTelemetry metrics bridge May 12, 2025
@evg-tso evg-tso changed the title DRAFT: AWS SDK v2 -> OpenTelemetry metrics bridge AWS SDK v2 -> OpenTelemetry metrics bridge May 12, 2025
@evg-tso
Copy link
Author

evg-tso commented May 14, 2025

@breedx-splk , @trask - sorry for the mention, but may I request a code review?

.build();

// For metrics (can be used independently of tracing)
MetricPublisher metricPublisher = new OpenTelemetryMetricPublisher(openTelemetry);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you consider building the metric publisher through AwsSdkTelemetry?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it, and it seems the AwsSdkTelemetry is more focused on tracing and propagation.
It could be as simple as the attached patch.
The main pros are the unified access point, but the con is that a person who only requires metrics will have to initiate an AwsSdkTelemetry via the builder, which contains many irrelevant fields.

Subject: Metrics in `AwsSdkTelemetry`
---
Index: instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/AwsSdkTelemetry.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/AwsSdkTelemetry.java b/instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/AwsSdkTelemetry.java
--- a/instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/AwsSdkTelemetry.java	(revision 542fdbf96ace578a0f4eb74a497b66a180adf069)
+++ b/instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/AwsSdkTelemetry.java	(date 1746692159838)
@@ -6,6 +6,7 @@
 package io.opentelemetry.instrumentation.awssdk.v2_2;
 
 import io.opentelemetry.api.OpenTelemetry;
+import io.opentelemetry.api.common.Attributes;
 import io.opentelemetry.api.logs.Logger;
 import io.opentelemetry.context.propagation.TextMapPropagator;
 import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
@@ -16,12 +17,15 @@
 import io.opentelemetry.instrumentation.awssdk.v2_2.internal.SqsProcessRequest;
 import io.opentelemetry.instrumentation.awssdk.v2_2.internal.SqsReceiveRequest;
 import io.opentelemetry.instrumentation.awssdk.v2_2.internal.TracingExecutionInterceptor;
+import io.opentelemetry.instrumentation.awssdk.v2_2.metrics.OpenTelemetryMetricPublisher;
 import io.opentelemetry.javaagent.tooling.muzzle.NoMuzzle;
 import java.util.List;
+import java.util.concurrent.Executor;
 import javax.annotation.Nullable;
 import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
 import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
 import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
+import software.amazon.awssdk.metrics.MetricPublisher;
 import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeAsyncClient;
 import software.amazon.awssdk.services.sqs.SqsAsyncClient;
 import software.amazon.awssdk.services.sqs.SqsClient;
@@ -61,6 +65,7 @@
     return new AwsSdkTelemetryBuilder(openTelemetry);
   }
 
+  private final OpenTelemetry openTelemetry;
   private final Instrumenter<ExecutionAttributes, Response> requestInstrumenter;
   private final Instrumenter<SqsReceiveRequest, Response> consumerReceiveInstrumenter;
   private final Instrumenter<SqsProcessRequest, Response> consumerProcessInstrumenter;
@@ -96,6 +101,7 @@
             messagingReceiveInstrumentationEnabled,
             useXrayPropagator);
 
+    this.openTelemetry = openTelemetry;
     this.requestInstrumenter = instrumenterFactory.requestInstrumenter();
     this.consumerReceiveInstrumenter = instrumenterFactory.consumerReceiveInstrumenter();
     this.consumerProcessInstrumenter = instrumenterFactory.consumerProcessInstrumenter();
@@ -154,4 +160,20 @@
       BedrockRuntimeAsyncClient bedrockClient) {
     return BedrockRuntimeImpl.wrap(bedrockClient, eventLogger, genAiCaptureMessageContent);
   }
+  
+  public MetricPublisher createMetricPublisher() {
+    return new OpenTelemetryMetricPublisher(openTelemetry);
+  }
+
+  public MetricPublisher createMetricPublisher(String metricPrefix) {
+    return new OpenTelemetryMetricPublisher(openTelemetry, metricPrefix);
+  }
+
+  public MetricPublisher createMetricPublisher(String metricPrefix, Executor executor) {
+    return new OpenTelemetryMetricPublisher(openTelemetry, metricPrefix, executor);
+  }
+
+  public MetricPublisher createMetricPublisher(String metricPrefix, Executor executor, Attributes baseAttributes) {
+    return new OpenTelemetryMetricPublisher(openTelemetry, metricPrefix, executor, baseAttributes);
+  }
 }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I don't like these additions to the AwsSdkTelemetry. They muddy that class and force some odd fields into its builder that are unrelated to its traces aspect.
Example: the Executor executor and Attributesn't baseAttributes are used by the metrics but not by the traces.
@laurit, what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding it that the intention was that end users will interact Telemetry classes to apply the manual instrumentation libraries. If you disagree with that approach you are welcome to raise this at the SIG meeting. Java SIG meeting is on Thursday, you can find the time in the OTEL community calendar. @open-telemetry/java-instrumentation-approvers do you have any suggestions?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't disagree with that approach, it makes plenty of sense from the end-user perspective.
I'll need to refactor the AwsSdkTelemetry class and expand the scope of this PR to address that, though.
Let me think if I can find a clean approach here.
Any specific suggestions are welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for AWS's v2 SDK's MetricPublisher
2 participants