Skip to content

[Draft, stacked on #2611] Add the OpenTelemetry tracing plugin (Execution and Invocation views) - #2612

Draft
normj wants to merge 9 commits into
normj/durable-otel-plugin-apifrom
normj/durable-otel-package
Draft

normj wants to merge 9 commits into
normj/durable-otel-plugin-apifrom
normj/durable-otel-package

Conversation

@normj

@normj normj commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Issue #, if available:

Description of changes:

Draft, stacked on #2611. The base branch is the plugin API branch of #2611, so this diff shows only the OpenTelemetry package. It should be retargeted to master once #2611 merges.

What this adds

A new package, Amazon.Lambda.DurableExecution.OpenTelemetry, that is a plugin for the instrumentation plugin API of #2611. It has both views: Execution (the default) and Invocation.

It emits Workflow, Invocation, operation and attempt spans through System.Diagnostics.ActivitySource, so the package has no OpenTelemetry NuGet dependency. The OpenTelemetry SDK, the ADOT layer and OpenTelemetry.Instrumentation.AWSLambda listen to the source once the application adds OtelPlugin.ActivitySourceName.

static readonly DurableExecutionOptions Options = new DurableExecutionOptions()
    .AddOpenTelemetry(OtelView.Execution, new OtelPluginOptions
    {
        FlushAsync = (timeout, _) => { Tracer.ForceFlush((int)timeout.TotalMilliseconds); return Task.CompletedTask; },
    });

See the package README for the trace structure, attributes, sampling and options. It follows the JavaScript, Python and Java OTel plugins: one trace per execution anchored at the propagated remote parent or a synthetic root, derived ids for the Workflow span, the synthetic root and the first segment of each operation, upstream sampling decisions honored, and the chained-invoke trace header supplied to the SDK.

The two views

View Hierarchy Operations link
Execution (default) Workflow -> operation -> attempt Invocation
Invocation Invocation -> operation -> attempt Workflow

Both exist in the JavaScript, Python and Java SDKs, with the same shape, and the shared conformance suite has an otel-execution and an otel-invocation suite.

In the Execution view an operation has one span, with a derived id, exported by the invocation where it finishes using the start and end times the service recorded. While it is open only its identity exists, so nothing is left unended when an invocation suspends. Replaying a finished operation exports nothing. Attempts are real spans in the invocation that ran them, parented on the operation's derived id before its own span exists. Application spans started inside the workflow, a child context or a step parent onto the Workflow, the child context or the attempt through a stand-in Activity that carries the derived id and is never exported (it is not created by an ActivitySource).

Both views share one base class: trace context, Invocation span, Workflow span, synthetic root, sampling, propagation, log scope and flush.

The part to review closely: deterministic span ids

An execution runs as several invocations and nothing is stored between them, so a span exported by one invocation must name spans exported by another (the Workflow span, the link from a retried step's later segments to its first segment). That needs spans with chosen ids. Activity has no public way to set its own span id.

The plugin writes the private _spanId and _id fields through [UnsafeAccessor] (ActivityIdOverride), only for those spans. It is generated at compile time and passes the AOT publish check. The fields are not a contract, so:

  • if a runtime changes them, the override switches itself off and spans get random ids. Parenting inside one invocation still works; references across invocations then dangle;
  • a test fails when the fields are missing, so a new runtime is noticed;
  • a listener notified when a span starts sees the random id. Export and child spans see the derived one.

I checked it on net8.0 and net10.0 with the real OpenTelemetry SDK in a scratch project (not in this PR), including parent/child ids and a sampler following the parent flags. If you would rather not depend on private fields, the alternative is random ids plus a spec exception for cross-invocation references.

Found by the conformance tests (#2615)

In the Invocation view, a later invocation that only replayed a finished step or child context exported it again as a new segment. Now only an operation that had not finished when the invocation reached it (a retried step, a suspended context) gets a new segment; a finished one is exported once. This matches the Python SDK, and shared conformance cases 10 and 17 require it. The test that covered the old behavior now asserts the new one, and fails with the fix reverted.

W3C traceparent

ContextExtractors.W3C anchors the execution on a W3C traceparent instead of an X-Ray header. The value comes from ILambdaContext.W3C (clientContext.w3c), which the Lambda team confirmed is where it is carried; #2611 exposes it as InvocationInfo.TraceParent. The default extractor is still X-Ray. Unit tests cover the parser; it has not been exercised against a deployed function.

Not in this PR

Testing

68 tests on net8.0 and net10.0, using a plain ActivityListener (no OpenTelemetry SDK) and the in-memory test runner from #2611, plus direct hook tests for cases the in-memory service cannot produce. They cover the span tree and parenting, derived ids, links and replay, a synthetic root, sampling, errors and statuses, completions while suspended, propagation, log scope, flush, Activity.Current handling, the exclusive group, and the Activity field override. I broke the code on purpose in several places (no derived ids, Activity.Current leaking, re-exported completions, override disabled, Workflow exported on every invocation) and the tests caught each one. The package is part of the Native AOT publish smoke test, which publishes with no IL warnings.

Checked against a real durable execution (us-west-2)

I ran both views in a deployed function (a retried step, a wait, a child context, a parallel, a chained invoke, and a callback) with an in-process listener that prints each exported span, and active tracing on.

Invocation view, five invocations: one trace of 27 spans (this run predates the replay fix above, so some of those were re-exported finished steps). No duplicate span ids, every link resolves, each Invocation span's parent is the X-Ray function segment of its own invocation, and Workflow is exported once. The retried step has three segments with distinct ids, and replayed ones link the first segment. The chained-invoke header lands on the right span: in X-Ray the invoked function's AWS::Lambda segments have parent_id equal to the id of the call_child span. With Sampled=0 upstream nothing is exported and the workflow succeeds.

Execution view, four invocations: one trace of 20 spans, one span per operation:

Workflow
+-- flaky  (one span; attempt 1 and 2 are its children, each exported by the invocation that ran it)
+-- pause, outer -> inner -> inner attempt 1, fan -> 0 -> branch-a ..., 1 -> branch-b ..., call_child
Invocation x4   (siblings of Workflow; every operation links the Invocation that finished it)

No duplicate ids, every link resolves, replayed operations were not exported again, and the wait, the child context, the parallel and the invoke each have the service's start and end (the wait is 2000 ms, as configured).

Two real findings:

  • A step resumed after a retry was reported as a fresh start, so two spans in one trace had the same derived id. Fixed in Add instrumentation plugin API to Amazon.Lambda.DurableExecution (groundwork for OpenTelemetry) #2611 (a resumed operation is a replay at start) and covered by tests here.
  • The service resets a step's start time when it schedules a retry, and a later invocation cannot see an earlier attempt. So a retried step's operation span in the Execution view starts after its first attempts (the retried step above starts about 2 s after its first attempt). This is cosmetic and is noted in the README.

Not checked here: Lambda Managed Instances, or running the Native AOT binary. #2615 runs the plugin with the OpenTelemetry SDK's OTLP exporter and the ADOT collector layer.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@normj normj changed the title [Draft, stacked on #2611] Add the OpenTelemetry tracing plugin (Invocation view) [Draft, stacked on #2611] Add the OpenTelemetry tracing plugin (Execution and Invocation views) Nov 18, 2026
@normj
normj added this pull request to stack #2616 November 18, 2026 23:03
@normj
normj force-pushed the normj/durable-otel-package branch from 86cefdb to af349e8 Compare November 18, 2026 23:42
New package Amazon.Lambda.DurableExecution.OpenTelemetry. It is a plugin for
the instrumentation plugin API and emits Workflow, Invocation, operation and
attempt spans through System.Diagnostics.ActivitySource, so it takes no
OpenTelemetry NuGet dependency.

Every invocation of an execution joins one trace, anchored at the propagated
remote parent or, without one, at a synthetic root derived from the execution
ARN. The Workflow span, the synthetic root and the first segment of each
operation have ids derived from the ARN, as in the JavaScript, Python and Java
SDKs. Activity cannot set its own span id through its public API, so the ids
are written to its private fields with UnsafeAccessor; that switches itself
off if a runtime changes the fields, and a test notices.

Also: sampling follows the upstream decision, waits/invokes/callbacks that
finished while suspended are exported once, replayed steps link their first
segment, the plugin supplies the chained-invoke trace header, adds log scope
fields, and calls an application-supplied flush at the end of each invocation.
The Execution view is not implemented yet. The package is part of the AOT
publish check.
Adds the project to autover.json and a change file for its first release.
Covers the bug found by tracing a real execution: a step resumed after a retry
was reported as a fresh start and got the derived id of its first segment a
second time. Merged in the core fix (a resumed operation is a replay at start).
The new tests run a step through three invocations and call the plugin hooks
for a resumed step.
In this view operations are children of the Workflow span and link the
Invocation. An operation has one span, with a derived id, exported by the
invocation where it finishes, using the start and end times the service
recorded. While it is open only its identity exists, so nothing is left
unended when an invocation suspends. Reaching a finished operation by replay
exports nothing, and a wait, invoke or callback that completed while suspended
is exported once.

Attempts are real spans in the invocation that ran them, parented on the
operation's derived id before its own span exists. A parent starts no later
than its earliest child. Application spans started inside the workflow, a
child context or a step parent onto the Workflow, the child context or the
attempt: the first two have no span yet, so a stand-in activity that carries
their derived id is current. It is not created by an ActivitySource, so no
listener sees it.

The state both views share (trace context, Invocation span, Workflow span,
synthetic root, sampling, propagation, log scope, flush) moved into a base
class. OtelView.Execution no longer throws.
…st attempts

Observed on a deployed function: the service resets a step's start time when
it schedules a retry, and a later invocation cannot see an earlier attempt, so
the Execution view's operation span can start after the attempts it parents.
…it is only replayed

The shared conformance case 10 (and 17) expects one span for a completed
submitter step that later invocations replay. The Python SDK gives finished
operations no start hook on replay; this matches it.
ContextExtractors.W3C reads InvocationInfo.TraceParent, which comes from
ILambdaContext.W3C (clientContext.w3c).
@normj
normj force-pushed the normj/durable-otel-package branch from af349e8 to 4865380 Compare November 19, 2026 00:07
Only XRay, W3C and the tests call them. The extractors stay public.
Previously each span went through the application's sampler, so a sampler
that ignores the parent (AlwaysOn) exported every operation span of an
unsampled execution but not its Workflow span. Now the plugin starts no span
when the decision is not to sample: an upstream decision is authoritative,
and without one the Invocation span's sampling result decides for the rest.
This matches the JavaScript, Python and Java SDKs, which wrap the sampler.

This branch has not been deployed

No deployments
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.

1 participant