-
Notifications
You must be signed in to change notification settings - Fork 10
feat(otel): Add OpenTelemetry plugin module with deterministic tracing #426
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
examples/src/main/java/software/amazon/lambda/durable/examples/general/OtelExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.lambda.durable.examples.general; | ||
|
|
||
| import io.opentelemetry.exporter.logging.LoggingSpanExporter; | ||
| import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
| import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; | ||
| import software.amazon.lambda.durable.DurableConfig; | ||
| import software.amazon.lambda.durable.DurableContext; | ||
| import software.amazon.lambda.durable.DurableHandler; | ||
| import software.amazon.lambda.durable.examples.types.GreetingRequest; | ||
| import software.amazon.lambda.durable.otel.DeterministicIdGenerator; | ||
| import software.amazon.lambda.durable.otel.OpenTelemetryDurablePlugin; | ||
|
|
||
| /** | ||
| * Example demonstrating OpenTelemetry instrumentation with the Durable Execution SDK. | ||
| * | ||
| * <p>This handler configures the OTel plugin with: | ||
| * | ||
| * <ul> | ||
| * <li>Deterministic trace/span IDs (all invocations of the same execution share one trace) | ||
| * <li>MDC log enrichment (trace_id and span_id in every log line) | ||
| * <li>Logging exporter (spans printed to stdout → CloudWatch Logs) | ||
| * </ul> | ||
| * | ||
| * <p>In production, replace {@code LoggingSpanExporter} with {@code OtlpGrpcSpanExporter} to send spans to an OTLP | ||
| * collector (X-Ray, Datadog, etc.). | ||
| * | ||
| * <p>Expected trace structure: | ||
| * | ||
| * <pre> | ||
| * durable.invocation | ||
| * ├── durable.step:create-greeting [attempt 1] | ||
| * ├── durable.step:create-greeting (operation, backfilled) | ||
| * ├── durable.step:transform [attempt 1] | ||
| * └── durable.step:transform (operation, backfilled) | ||
| * </pre> | ||
| */ | ||
| public class OtelExample extends DurableHandler<GreetingRequest, String> { | ||
|
|
||
| @Override | ||
| protected DurableConfig createConfiguration() { | ||
| var idGenerator = new DeterministicIdGenerator(); | ||
| var tracerProvider = SdkTracerProvider.builder() | ||
| .setIdGenerator(idGenerator) | ||
| .addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create())) | ||
| .build(); | ||
| var otelPlugin = new OpenTelemetryDurablePlugin(tracerProvider, idGenerator); | ||
|
|
||
| return DurableConfig.builder().withPlugins(otelPlugin).build(); | ||
| } | ||
|
|
||
| @Override | ||
| public String handleRequest(GreetingRequest input, DurableContext context) { | ||
| // Log with MDC — trace_id and span_id will be in the JSON output | ||
| context.getLogger().info("Starting OTel example for {}", input.getName()); | ||
|
|
||
| var greeting = context.step("create-greeting", String.class, stepCtx -> { | ||
| context.getLogger().info("Inside step — this log has trace context in MDC"); | ||
| return "Hello, " + input.getName(); | ||
| }); | ||
|
|
||
| var result = context.step("transform", String.class, stepCtx -> greeting.toUpperCase() + "!"); | ||
|
|
||
| context.getLogger().info("OTel example complete: {}", result); | ||
| return result; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
examples/src/test/java/software/amazon/lambda/durable/examples/general/OtelExampleTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.lambda.durable.examples.general; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import software.amazon.lambda.durable.examples.types.GreetingRequest; | ||
| import software.amazon.lambda.durable.model.ExecutionStatus; | ||
| import software.amazon.lambda.durable.testing.LocalDurableTestRunner; | ||
|
|
||
| class OtelExampleTest { | ||
|
|
||
| @Test | ||
| void testOtelExample_executesSuccessfully() { | ||
| var handler = new OtelExample(); | ||
| var runner = LocalDurableTestRunner.create(GreetingRequest.class, handler); | ||
|
|
||
| var result = runner.run(new GreetingRequest("World")); | ||
|
|
||
| assertEquals(ExecutionStatus.SUCCEEDED, result.getStatus()); | ||
| assertEquals("HELLO, WORLD!", result.getResult(String.class)); | ||
|
|
||
| assertNotNull(result.getOperation("create-greeting")); | ||
| assertNotNull(result.getOperation("transform")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <parent> | ||
| <groupId>software.amazon.lambda.durable</groupId> | ||
| <artifactId>aws-durable-execution-sdk-java-parent</artifactId> | ||
| <version>1.1.1-SNAPSHOT</version> | ||
| </parent> | ||
|
|
||
| <artifactId>aws-durable-execution-sdk-java-otel</artifactId> | ||
| <name>AWS Lambda Durable Execution SDK - OpenTelemetry Plugin</name> | ||
| <description>OpenTelemetry instrumentation plugin for AWS Lambda Durable Execution SDK</description> | ||
|
|
||
| <properties> | ||
| <opentelemetry.version>1.62.0</opentelemetry.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <!-- Durable Execution SDK (for plugin interface) --> | ||
| <dependency> | ||
| <groupId>software.amazon.lambda.durable</groupId> | ||
| <artifactId>aws-durable-execution-sdk-java</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
|
|
||
| <!-- OpenTelemetry API (compile dependency — users bring the SDK implementation) --> | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-api</artifactId> | ||
| <version>${opentelemetry.version}</version> | ||
| </dependency> | ||
|
|
||
| <!-- OpenTelemetry SDK (provided — users bring their own SDK configuration) --> | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-sdk</artifactId> | ||
| <version>${opentelemetry.version}</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
|
|
||
| <!-- OpenTelemetry Context (transitive from API, but explicit for clarity) --> | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-context</artifactId> | ||
| <version>${opentelemetry.version}</version> | ||
| </dependency> | ||
|
|
||
| <!-- AWS X-Ray Propagator (official OTel contrib) --> | ||
| <dependency> | ||
| <groupId>io.opentelemetry.contrib</groupId> | ||
| <artifactId>opentelemetry-aws-xray-propagator</artifactId> | ||
| <version>1.56.0-alpha</version> | ||
| </dependency> | ||
|
|
||
| <!-- SLF4J for logging --> | ||
| <dependency> | ||
| <groupId>org.slf4j</groupId> | ||
| <artifactId>slf4j-api</artifactId> | ||
| </dependency> | ||
|
|
||
| <!-- Test dependencies --> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-sdk-testing</artifactId> | ||
| <version>${opentelemetry.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.mockito</groupId> | ||
| <artifactId>mockito-core</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>ch.qos.logback</groupId> | ||
| <artifactId>logback-classic</artifactId> | ||
| <version>1.5.18</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>com.diffplug.spotless</groupId> | ||
| <artifactId>spotless-maven-plugin</artifactId> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
27 changes: 27 additions & 0 deletions
27
otel-plugin/src/main/java/software/amazon/lambda/durable/otel/ContextExtractor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.lambda.durable.otel; | ||
|
|
||
| import io.opentelemetry.context.Context; | ||
|
|
||
| /** | ||
| * Extracts OTel trace context from the Lambda runtime environment. | ||
| * | ||
| * <p>Implementations read trace context from various sources (X-Ray trace header, W3C traceparent, etc.) and return an | ||
| * OTel {@link Context} that can be used as the parent for invocation spans. | ||
| * | ||
| * <p>Called once per invocation in {@code onInvocationStart} to establish the parent trace context. | ||
| * | ||
| * @deprecated This is a preview API that is experimental and may be changed or removed in future releases. | ||
| */ | ||
| @Deprecated | ||
| @FunctionalInterface | ||
| public interface ContextExtractor { | ||
|
|
||
| /** | ||
| * Extracts trace context from the runtime environment. | ||
| * | ||
| * @return the extracted OTel context, or {@link Context#root()} if no context is available | ||
| */ | ||
| Context extract(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.