Skip to content

Commit 5cd16f6

Browse files
feat: store tracecontext in additionalcontext
1 parent dab3ea3 commit 5cd16f6

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/Voxel.MiddyNet.Tracing.ApiGatewayMiddleware/ApiGatewayHttpApiV2TracingMiddleware.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Voxel.MiddyNet.Tracing.ApiGatewayMiddleware
66
{
77
public class ApiGatewayHttpApiV2TracingMiddleware : ILambdaMiddleware<APIGatewayHttpApiV2ProxyRequest, APIGatewayHttpApiV2ProxyResponse>
88
{
9+
private const string TraceContextKey = "TraceContext";
910
private const string TraceParentHeaderName = "traceparent";
1011
private const string TraceStateHeaderName = "tracestate";
1112
private const string TraceIdHeaderName = "trace-id";
@@ -22,14 +23,12 @@ public Task Before(APIGatewayHttpApiV2ProxyRequest apiGatewayEvent, MiddyNetCont
2223

2324
var traceContext = TraceContext.Handle(traceParentHeaderValue, traceStateHeaderValue);
2425

26+
context.AdditionalContext.Add(TraceContextKey, traceContext);
27+
2528
context.Logger.EnrichWith(new LogProperty(TraceParentHeaderName, traceContext.TraceParent));
2629
context.Logger.EnrichWith(new LogProperty(TraceStateHeaderName, traceContext.TraceState));
2730
context.Logger.EnrichWith(new LogProperty(TraceIdHeaderName, traceContext.TraceId));
2831

29-
context.AdditionalContext.Add(TraceParentHeaderName, traceContext.TraceParent);
30-
context.AdditionalContext.Add(TraceStateHeaderName, traceContext.TraceState);
31-
context.AdditionalContext.Add(TraceIdHeaderName, traceContext.TraceId);
32-
3332
return Task.CompletedTask;
3433
}
3534

test/Voxel.MiddyNet.Tracing.ApiGatewayMiddleware.Tests/ApiGatewayHttpApiV2TracingMiddlewareShould.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Amazon.Lambda.Core;
55
using FluentAssertions;
66
using NSubstitute;
7+
using Voxel.MiddyNet.Tracing.Core;
78
using Xunit;
89

910
namespace Voxel.MiddyNet.Tracing.ApiGatewayMiddleware.Tests
@@ -16,6 +17,7 @@ public class ApiGatewayHttpApiV2TracingMiddlewareShould
1617
private const string TraceparentHeaderValue = "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01";
1718
private const string TracestateHeaderValue = "congo=ucfJifl5GOE";
1819
private const string TraceIdHeaderValue = "0af7651916cd43dd8448eb211c80319c";
20+
private const string TraceContextKey = "TraceContext";
1921

2022
[Fact]
2123
public async Task EnrichLoggerWithTraceContext()
@@ -55,12 +57,11 @@ public async Task EnrichContextWithTraceContext()
5557

5658
await middleware.Before(apiGatewayEvent, context);
5759

58-
context.AdditionalContext.Should().ContainKey(TraceparentHeaderName);
59-
context.AdditionalContext[TraceparentHeaderName].Should().Be(TraceparentHeaderValue);
60-
context.AdditionalContext.Should().ContainKey(TracestateHeaderName);
61-
context.AdditionalContext[TracestateHeaderName].Should().Be(TracestateHeaderValue);
62-
context.AdditionalContext.Should().ContainKey(TraceIdHeaderName);
63-
context.AdditionalContext[TraceIdHeaderName].Should().Be(TraceIdHeaderValue);
60+
context.AdditionalContext.Should().ContainKey(TraceContextKey);
61+
var traceContext = context.AdditionalContext[TraceContextKey] as TraceContext;
62+
traceContext.TraceParent.Should().Be(TraceparentHeaderValue);
63+
traceContext.TraceState.Should().Be(TracestateHeaderValue);
64+
traceContext.TraceId.Should().Be(TraceIdHeaderValue);
6465
}
6566
}
6667
}

0 commit comments

Comments
 (0)