Skip to content

Commit 58b2ad0

Browse files
committed
refactor where baggage is stored
1 parent b230801 commit 58b2ad0

File tree

10 files changed

+35
-50
lines changed

10 files changed

+35
-50
lines changed

src/DotNetWorker.Core/Context/DefaultTraceContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ namespace Microsoft.Azure.Functions.Worker
77
{
88
internal sealed class DefaultTraceContext : TraceContext
99
{
10-
public DefaultTraceContext(string traceParent, string traceState, IReadOnlyDictionary<string, string> attributes)
10+
public DefaultTraceContext(string traceParent, string traceState, IReadOnlyDictionary<string, string> attributes, IReadOnlyDictionary<string, string> baggage)
1111
{
1212
TraceParent = traceParent;
1313
TraceState = traceState;
1414
Attributes = attributes;
15+
Baggage = baggage;
1516
}
1617

1718
public override string TraceParent { get; }
1819

1920
public override string TraceState { get; }
2021

2122
public override IReadOnlyDictionary<string, string> Attributes { get; }
23+
24+
public override IReadOnlyDictionary<string, string> Baggage { get; }
2225
}
2326
}

src/DotNetWorker.Core/Context/TraceContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,10 @@ public abstract class TraceContext
2525
/// Gets the attributes associated with the trace.
2626
/// </summary>
2727
public virtual IReadOnlyDictionary<string, string> Attributes => ImmutableDictionary<string, string>.Empty;
28+
29+
/// <summary>
30+
/// Gets the baggage associated with the trace.
31+
/// </summary>
32+
public virtual IReadOnlyDictionary<string, string> Baggage => ImmutableDictionary<string, string>.Empty;
2833
}
2934
}

src/DotNetWorker.Core/Diagnostics/TraceConstants.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public static class InternalKeys
4646
public const string FunctionName = "AzureFunctions_FunctionName";
4747
public const string HostInstanceId = "HostInstanceId";
4848
public const string AzFuncLiveLogsSessionId = "#AzFuncLiveLogsSessionId";
49-
public const string BaggageKeyName = "AzureFunctions_TraceContext_Baggage";
5049
}
5150

5251
public static class CapabilityFlags

src/DotNetWorker.Grpc/GrpcFunctionInvocation.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ internal sealed class GrpcFunctionInvocation : FunctionInvocation, IExecutionRet
1313
public GrpcFunctionInvocation(InvocationRequest invocationRequest)
1414
{
1515
_invocationRequest = invocationRequest;
16-
TraceContext = new DefaultTraceContext(_invocationRequest.TraceContext.TraceParent, _invocationRequest.TraceContext.TraceState, _invocationRequest.TraceContext.Attributes);
16+
TraceContext = new DefaultTraceContext(_invocationRequest.TraceContext.TraceParent,
17+
_invocationRequest.TraceContext.TraceState, _invocationRequest.TraceContext.Attributes, _invocationRequest.TraceContext.Baggage);
1718
}
1819

1920
public override string Id => _invocationRequest.InvocationId;

src/DotNetWorker.Grpc/Handlers/InvocationHandler.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,6 @@ public async Task<InvocationResponse> InvokeAsync(InvocationRequest request)
8787
invocationFeatures.Set<IInputConversionFeature>(conversion!);
8888
}
8989

90-
var baggage = request.TraceContext?.Baggage;
91-
92-
if (baggage is not null)
93-
{
94-
context.Items[TraceConstants.InternalKeys.BaggageKeyName] = baggage;
95-
}
96-
9790
await _application.InvokeFunctionAsync(context);
9891

9992
var serializer = _workerOptions.Serializer!;

src/DotNetWorker.OpenTelemetry/BaggageMiddleware.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using System;
5-
using System.Collections.Generic;
64
using System.Threading.Tasks;
75
using Microsoft.Azure.Functions.Worker.Middleware;
86
using OpenTelemetry;
@@ -15,13 +13,9 @@ public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next
1513
{
1614
try
1715
{
18-
if (context.Items.TryGetValue(TraceConstants.BaggageKeyName, out var value) &&
19-
value is IEnumerable<KeyValuePair<string, string>> dict)
16+
foreach (var kv in context.TraceContext.Baggage)
2017
{
21-
foreach (var kv in dict)
22-
{
23-
Baggage.SetBaggage(kv.Key, kv.Value);
24-
}
18+
Baggage.SetBaggage(kv.Key, kv.Value);
2519
}
2620

2721
await next(context);

src/DotNetWorker.OpenTelemetry/TraceConstants.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/DotNetWorker.OpenTelemetry.Tests/BaggageMiddlewareTests.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@ public async Task Invoke_SetsAndClearsBaggage_WhenBaggagePresent()
1717
{
1818
var baggageKey = "TestKey";
1919
var baggageValue = "TestValue";
20-
var baggageDict = new List<KeyValuePair<string, string>>
20+
var baggageDict = new Dictionary<string, string>
2121
{
22-
new KeyValuePair<string, string>(baggageKey, baggageValue)
22+
{ baggageKey, baggageValue }
2323
};
2424

25+
var traceContextMock = new Mock<TraceContext>();
26+
traceContextMock.Setup(t => t.Baggage).Returns(baggageDict);
27+
2528
var contextMock = new Mock<FunctionContext>();
26-
var items = new Dictionary<object, object>
27-
{
28-
{ TraceConstants.BaggageKeyName, baggageDict }
29-
};
30-
contextMock.Setup(c => c.Items).Returns(items);
29+
contextMock.Setup(c => c.TraceContext).Returns(traceContextMock.Object);
3130

3231
bool nextCalled = false;
3332
var middleware = new BaggageMiddleware();
@@ -52,17 +51,16 @@ public async Task Invoke_ClearsBaggage_OnException()
5251
{
5352
var baggageKey = "TestKey";
5453
var baggageValue = "TestValue";
55-
var baggageDict = new List<KeyValuePair<string, string>>
56-
{
57-
new KeyValuePair<string, string>(baggageKey, baggageValue)
58-
};
54+
var baggageDict = new Dictionary<string, string>
55+
{
56+
{ baggageKey, baggageValue }
57+
};
58+
59+
var traceContextMock = new Mock<TraceContext>();
60+
traceContextMock.Setup(t => t.Baggage).Returns(baggageDict);
5961

6062
var contextMock = new Mock<FunctionContext>();
61-
var items = new Dictionary<object, object>
62-
{
63-
{ TraceConstants.BaggageKeyName, baggageDict }
64-
};
65-
contextMock.Setup(c => c.Items).Returns(items);
63+
contextMock.Setup(c => c.TraceContext).Returns(traceContextMock.Object);
6664

6765
var middleware = new BaggageMiddleware();
6866

test/DotNetWorker.Tests/Handlers/InvocationHandlerTests.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,16 +273,13 @@ public async Task InvokeAsync_SetsBaggage_WhenPresentInInvocationRequest()
273273
var testValue = "testValue";
274274
request.TraceContext.Baggage[testKey] = testValue;
275275

276-
// Setup to verify baggage is set in context.Items during invocation
277276
_mockApplication
278277
.Setup(m => m.InvokeFunctionAsync(It.IsAny<FunctionContext>()))
279278
.Returns(Task.CompletedTask)
280279
.Callback<FunctionContext>(ctx =>
281280
{
282-
Assert.True(ctx.Items.ContainsKey(CoreTraceConstants.InternalKeys.BaggageKeyName));
283-
var actualBaggage = ctx.Items[CoreTraceConstants.InternalKeys.BaggageKeyName] as System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>;
284-
Assert.NotNull(actualBaggage);
285-
Assert.Contains(actualBaggage, kv => kv.Key == testKey && kv.Value == testValue);
281+
Assert.True(ctx.TraceContext.Baggage.ContainsKey(testKey));
282+
Assert.Equal(testValue, ctx.TraceContext.Baggage[testKey]);
286283
});
287284

288285
var handler = CreateInvocationHandler();

test/TestUtility/TestFunctionInvocation.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ public TestFunctionInvocation(string id = null, string functionId = null)
3030
{ TraceConstants.InternalKeys.AzFuncLiveLogsSessionId, Guid.NewGuid().ToString() },
3131
};
3232

33-
TraceContext = new DefaultTraceContext(activity.Id, Guid.NewGuid().ToString(), attributes);
33+
Dictionary<string, string> baggage = new Dictionary<string, string>
34+
{
35+
{ "TestBaggageKey", "TestBaggageValue" }
36+
};
37+
38+
TraceContext = new DefaultTraceContext(activity.Id, Guid.NewGuid().ToString(), attributes, baggage);
3439
}
3540

3641
public override string Id { get; } = Guid.NewGuid().ToString();

0 commit comments

Comments
 (0)