|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package software.amazon.lambda.durable.examples.otel; |
| 4 | + |
| 5 | +import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter; |
| 6 | +import io.opentelemetry.sdk.trace.SdkTracerProvider; |
| 7 | +import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; |
| 8 | +import java.time.Duration; |
| 9 | +import software.amazon.lambda.durable.DurableConfig; |
| 10 | +import software.amazon.lambda.durable.DurableContext; |
| 11 | +import software.amazon.lambda.durable.DurableHandler; |
| 12 | +import software.amazon.lambda.durable.examples.types.GreetingRequest; |
| 13 | +import software.amazon.lambda.durable.otel.OpenTelemetryDurablePlugin; |
| 14 | + |
| 15 | +/** |
| 16 | + * OTel + X-Ray example: step → wait → step pattern that forces multiple Lambda invocations. |
| 17 | + * |
| 18 | + * <p>This handler exercises the critical multi-invocation tracing scenario: |
| 19 | + * |
| 20 | + * <ol> |
| 21 | + * <li>Invocation 1: "before-wait" step completes → wait suspends execution |
| 22 | + * <li>Invocation 2: replays "before-wait" (no-op) → wait completes → "after-wait" step runs |
| 23 | + * </ol> |
| 24 | + * |
| 25 | + * <p>Exports spans via OTLP gRPC to the ADOT collector extension (Lambda layer), which forwards to X-Ray. |
| 26 | + * |
| 27 | + * <p>Used by {@code OtelXRayIntegrationTest} to verify that deterministic trace IDs correctly stitch spans from |
| 28 | + * multiple invocations into a single X-Ray trace. |
| 29 | + * |
| 30 | + * <p>Expected trace structure in X-Ray: |
| 31 | + * |
| 32 | + * <pre> |
| 33 | + * Trace (single trace ID across both invocations) |
| 34 | + * ├── durable.invocation (invocation 1) |
| 35 | + * │ ├── durable.step:before-wait |
| 36 | + * │ │ └── durable.step:before-wait [attempt 1] |
| 37 | + * │ └── durable.wait:pause (ended as PENDING) |
| 38 | + * └── durable.invocation (invocation 2) |
| 39 | + * ├── durable.wait:pause (completed) |
| 40 | + * └── durable.step:after-wait |
| 41 | + * └── durable.step:after-wait [attempt 1] |
| 42 | + * </pre> |
| 43 | + * |
| 44 | + * <p>All spans share the same deterministic trace ID derived from the execution ARN. |
| 45 | + */ |
| 46 | +public class OtelXRayWaitExample extends DurableHandler<GreetingRequest, String> { |
| 47 | + |
| 48 | + @Override |
| 49 | + protected DurableConfig createConfiguration() { |
| 50 | + var otlpExporter = OtlpGrpcSpanExporter.builder() |
| 51 | + .setEndpoint("http://localhost:4317") |
| 52 | + .build(); |
| 53 | + |
| 54 | + var otelPlugin = new OpenTelemetryDurablePlugin( |
| 55 | + SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(otlpExporter))); |
| 56 | + |
| 57 | + return DurableConfig.builder().withPlugins(otelPlugin).build(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String handleRequest(GreetingRequest input, DurableContext context) { |
| 62 | + context.getLogger().info("Starting OTel X-Ray wait example for {}", input.getName()); |
| 63 | + |
| 64 | + var before = context.step("before-wait", String.class, stepCtx -> "Prepared: " + input.getName()); |
| 65 | + |
| 66 | + // This wait forces Lambda to suspend and re-invoke after the duration |
| 67 | + context.wait("pause", Duration.ofSeconds(5)); |
| 68 | + |
| 69 | + var after = context.step("after-wait", String.class, stepCtx -> before + " | Resumed and completed"); |
| 70 | + |
| 71 | + context.getLogger().info("OTel X-Ray wait example complete: {}", after); |
| 72 | + return after; |
| 73 | + } |
| 74 | +} |
0 commit comments