Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
publish/
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk">

<!-- Handlers for the shared OpenTelemetry conformance suites (otel-invocation, otel-execution).
One executable serves every case: CONFORMANCE_SCENARIO picks the scenario and OTEL_PLUGIN_MODE
picks the view, both set by template.yaml. -->
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<OutputType>Exe</OutputType>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AssemblyName>bootstrap</AssemblyName>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<None Include="collector-xray.yaml" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../../src/Amazon.Lambda.DurableExecution/Amazon.Lambda.DurableExecution.csproj" />
<ProjectReference Include="../../src/Amazon.Lambda.DurableExecution.OpenTelemetry/Amazon.Lambda.DurableExecution.OpenTelemetry.csproj" />
<ProjectReference Include="../../src/Amazon.Lambda.RuntimeSupport/Amazon.Lambda.RuntimeSupport.csproj" />
<ProjectReference Include="../../src/Amazon.Lambda.Serialization.SystemTextJson/Amazon.Lambda.Serialization.SystemTextJson.csproj" />
</ItemGroup>

<ItemGroup>
<!-- The instrumentation package depends on Amazon.Lambda.Core 2.x, which NuGet prefers over a project reference. -->
<PackageReference Include="Amazon.Lambda.Core" Version="3.4.0" />
<!-- Not transitive into a framework-dependent publish; the deployed bundle needs them. -->
<PackageReference Include="AWSSDK.Lambda" Version="4.0.110.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="11.0.0" />
<!-- The plugin only emits Activity objects. The handler needs an SDK to export them to the collector layer. -->
<PackageReference Include="OpenTelemetry" Version="1.21.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.21.0" />
<!-- Starts the server span for each Lambda invocation, the .NET counterpart of the ADOT wrapper the other SDKs run under. -->
<PackageReference Include="OpenTelemetry.Instrumentation.AWSLambda" Version="1.21.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using System.Text.Json;
using Amazon.Lambda.Core;
using Amazon.Lambda.DurableExecution.OpenTelemetry;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using OpenTelemetry;
using OpenTelemetry.Instrumentation.AWSLambda;
using OpenTelemetry.Trace;

namespace Amazon.Lambda.DurableExecution.ConformanceTests.Otel;

public static class Program
{
// The collector layer is a Lambda extension, so spans must be handed to it before the environment freezes.
private static readonly TracerProvider Tracer = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations()
.AddSource(OtelPlugin.ActivitySourceName)
.AddSource(Probes.SourceName)
.AddOtlpExporter()
.Build()!;

private static readonly DurableExecutionOptions Options = new DurableExecutionOptions()
.AddOpenTelemetry(
Environment.GetEnvironmentVariable("OTEL_PLUGIN_MODE") == "execution" ? OtelView.Execution : OtelView.Invocation,
new OtelPluginOptions
{
FlushAsync = (timeout, _) =>
{
Tracer.ForceFlush((int)timeout.TotalMilliseconds);
return Task.CompletedTask;
},
FlushTimeout = TimeSpan.FromSeconds(5),
});

private static readonly Scenario Selected = Scenarios.Find(
Environment.GetEnvironmentVariable("CONFORMANCE_SCENARIO")
?? throw new InvalidOperationException("CONFORMANCE_SCENARIO is not set"));

public static async Task Main()
{
using var handlerWrapper = HandlerWrapper.GetHandlerWrapper<DurableExecutionInvocationInput, DurableExecutionInvocationOutput>(
Handler, new DefaultLambdaJsonSerializer());
using var bootstrap = new LambdaBootstrap(handlerWrapper);
await bootstrap.RunAsync();
}

// The wrapper starts the server span the plugin's Invocation and Workflow spans hang from, and flushes after it.
public static Task<DurableExecutionInvocationOutput> Handler(DurableExecutionInvocationInput input, ILambdaContext context)
=> AWSLambdaWrapper.TraceAsync(Tracer, RunAsync, input, context);

private static Task<DurableExecutionInvocationOutput> RunAsync(DurableExecutionInvocationInput input, ILambdaContext context)
=> DurableFunction.WrapAsync<JsonElement, object?>(
(payload, durable) =>
{
Scenarios.RequireScenario(payload, Selected.PayloadScenario);
return Selected.Run(payload, durable);
},
input, context, Options);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# OpenTelemetry conformance handlers (.NET)

Handlers and a SAM template for the `otel-invocation` and `otel-execution` suites of
[aws-durable-execution-conformance-tests](https://github.com/aws/aws-durable-execution-conformance-tests).
The requirements, validators and runner live there. This directory only holds what the SDK owns.

One executable serves every case. `template.yaml` gives each function a `CONFORMANCE_SCENARIO`
(a class in `Scenarios/`) and an `OTEL_PLUGIN_MODE` (`invocation` or `execution`).
`template.yaml` is generated: edit the case list in `scripts/generate_template.py` and rerun it.

Spans leave the function in-process. `Program.cs` builds an OpenTelemetry `TracerProvider` that listens to the
plugin's `ActivitySource` and sends OTLP to the collector Lambda layer. `AWSLambdaWrapper` (from
`OpenTelemetry.Instrumentation.AWSLambda`) starts the server span the plugin's Invocation span hangs from. It plays the
role the ADOT wrapper plays for the Java, JavaScript and Python handlers.

## Run

```bash
./scripts/build_examples.sh
durable-execution-conformance \
--template template.yaml --language dotnet --region us-west-2 --name dotnet-xray-inv \
--suite otel-invocation --otel-service-name durable-execution-conformance \
--otel-exporter adot --otel-backend xray \
--otel-layer-arn arn:aws:lambda:us-west-2:901920570463:layer:aws-otel-collector-amd64-ver-0-156-0:4 \
--parameter-overrides LambdaExecutionRoleArn=<role> OtelServiceName=durable-execution-conformance OtelSuite=otel-invocation
```

`--language dotnet` needs the `dotnet` ADOT profile in the OTel conformance package. It uses the ADOT collector layer
and no exec wrapper.

## Export path

`collector-xray.yaml` configures the collector layer to send spans to X-Ray's OTLP endpoint with SigV4. That endpoint
keeps the span kind and resource service name the validators check; the layer's default `awsxray` exporter drops them.
It requires X-Ray Transaction Search in the account, with the indexing rule at 100% so every trace is queryable.

## Coverage

Both suites, against X-Ray in us-west-2: 25 passed, 2 not implemented.

| Case | Status |
|---|---|
| 1 to 22, 25 to 27 | pass |
| 23 | not implemented: needs a with-retry helper for child contexts, which the .NET SDK does not have |
| 24 | not implemented: needs a public way to request an invocation retry; a workflow exception fails the execution |

Child contexts set `SubType = "RunInChildContext"`, which other SDKs send by default and the requirements expect.
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

namespace Amazon.Lambda.DurableExecution.ConformanceTests.Otel;

// The conformance driver completes or fails these callbacks, so the submitters do nothing.

internal static class Otel10WaitForCallback
{
public static readonly Scenario Scenario = new(nameof(Otel10WaitForCallback), "wait-for-callback", async (_, context) =>
await context.WaitForCallbackAsync<string>((_, _, _) => Task.CompletedTask, name: "otel-callback"));
}

internal static class Otel12ChildContextFailure
{
public static readonly Scenario Scenario = new(nameof(Otel12ChildContextFailure), "child-context-failure", async (_, context) =>
{
await context.RunInChildContextAsync<string>(
(_, _) => throw new InvalidOperationException("Intentional child-context failure"),
name: "otel-failed-child-context",
config: new ChildContextConfig { SubType = "RunInChildContext" });
return null;
});
}

internal static class Otel13ParallelFailure
{
public static readonly Scenario Scenario = new(nameof(Otel13ParallelFailure), "parallel-failure", async (_, context) =>
{
var result = await context.ParallelAsync(
new[]
{
new DurableBranch<string>("otel-failed-parallel-branch", (_, _) =>
throw new InvalidOperationException("Intentional parallel branch failure")),
},
name: "otel-failed-parallel",
config: new ParallelConfig { MaxConcurrency = 1 });
result.ThrowIfError();
return null;
});
}

internal static class Otel14MapFailure
{
public static readonly Scenario Scenario = new(nameof(Otel14MapFailure), "map-failure", async (_, context) =>
{
var result = await context.MapAsync<int, string>(
new[] { 1 },
(_, _, _, _, _) => throw new InvalidOperationException("Intentional map iteration failure"),
name: "otel-failed-map",
config: new MapConfig<int> { MaxConcurrency = 1, ItemNamer = (_, index) => $"otel-failed-map-iteration-{index}" });
result.ThrowIfError();
return null;
});
}

internal static class Otel15WaitInterrupted
{
public static readonly Scenario Scenario = new(nameof(Otel15WaitInterrupted), "wait-interrupted", async (_, context) =>
{
await context.WaitAsync(TimeSpan.FromSeconds(30), name: "otel-interrupted-wait");
return null;
});
}

internal static class Otel16WaitForConditionFailure
{
public static readonly Scenario Scenario = new(nameof(Otel16WaitForConditionFailure), "wait-for-condition-failure", async (_, context) =>
{
await context.WaitForConditionAsync<int>(
(_, _, _) => throw new InvalidOperationException("Intentional condition check failure"),
new WaitForConditionConfig<int>
{
InitialState = 0,
WaitStrategy = WaitStrategy.FromDelegate<int>((_, _) => WaitDecision.ContinueAfter(TimeSpan.FromSeconds(1))),
},
name: "otel-failed-condition");
return null;
});
}

internal static class Otel17WaitForCallbackFailure
{
public static readonly Scenario Scenario = new(nameof(Otel17WaitForCallbackFailure), "wait-for-callback-failure", async (_, context) =>
{
await context.WaitForCallbackAsync<string>((_, _, _) => Task.CompletedTask, name: "otel-failed-callback");
return null;
});
}

internal static class Otel19ExecutionFailure
{
public static readonly Scenario Scenario = new(nameof(Otel19ExecutionFailure), "execution-failure",
(_, _) => throw new InvalidOperationException("Intentional execution failure"));
}

internal static class Otel20VirtualContext
{
public static readonly Scenario Scenario = new(nameof(Otel20VirtualContext), "virtual-context", async (_, context) =>
await context.RunInChildContextAsync(
(_, _) => Task.FromResult("virtual-complete"),
name: "otel-virtual-context",
config: new ChildContextConfig { NestingType = NestingType.Flat, SubType = "RunInChildContext" }));
}

internal static class Otel11ChainedInvoke
{
public static readonly Scenario Scenario = new(nameof(Otel11ChainedInvoke), "chained-invoke", async (payload, context) =>
{
var result = await context.InvokeAsync<System.Text.Json.JsonElement, System.Text.Json.JsonElement>(
Scenarios.InvokeTarget, payload, name: "otel-invoke");
// The driver completes this callback after the invocation that created it ends, forcing a replay of the invoke.
await context.WaitForCallbackAsync<System.Text.Json.JsonElement>(
(_, _, _) => Task.CompletedTask, name: "otel-invoke-replay-barrier");
return result;
});

public static readonly Scenario Target = new("Otel11InvokeTarget", "chained-invoke", (payload, _) =>
Task.FromResult<object?>(payload));
}

internal static class Otel18ChainedInvokeFailure
{
public static readonly Scenario Scenario = new(nameof(Otel18ChainedInvokeFailure), "chained-invoke-failure", async (payload, context) =>
{
await context.InvokeAsync<System.Text.Json.JsonElement, System.Text.Json.JsonElement>(
Scenarios.InvokeTarget, payload, name: "otel-failed-invoke");
return null;
});

public static readonly Scenario Target = new("Otel18InvokeTarget", "chained-invoke-failure", (_, _) =>
throw new InvalidOperationException("Intentional chained-invoke target failure"));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

namespace Amazon.Lambda.DurableExecution.ConformanceTests.Otel;

internal static class Otel1Success
{
public static readonly Scenario Scenario = new(nameof(Otel1Success), "success", async (_, context) =>
await context.StepAsync((_, _) => Task.FromResult("success"), name: "otel-success"));
}

internal static class Otel2WaitResume
{
public static readonly Scenario Scenario = new(nameof(Otel2WaitResume), "wait-resume", async (_, context) =>
{
await context.WaitAsync(TimeSpan.FromSeconds(1), name: "otel-wait");
return await context.StepAsync((_, _) => Task.FromResult("resumed"), name: "otel-after-resume");
});
}

internal static class Otel3Retry
{
public static readonly Scenario Scenario = new(nameof(Otel3Retry), "retry", async (_, context) =>
await context.StepAsync(
(step, _) => step.AttemptNumber == 1
? throw new InvalidOperationException("Intentional first-attempt failure")
: Task.FromResult("retried"),
name: "otel-retry",
config: new StepConfig
{
RetryStrategy = RetryStrategy.FromDelegate((_, attempt) =>
attempt >= 2 ? RetryDecision.DoNotRetry() : RetryDecision.RetryAfter(TimeSpan.FromSeconds(1))),
}));
}

internal static class Otel4TerminalFailure
{
public static readonly Scenario Scenario = new(nameof(Otel4TerminalFailure), "terminal-failure", async (_, context) =>
{
await context.StepAsync<string>(
(_, _) => throw new InvalidOperationException("Intentional terminal failure"),
name: "otel-terminal-failure",
config: new StepConfig { RetryStrategy = RetryStrategy.None });
return null;
});
}
Loading
Loading