Skip to content

Commit af83cc5

Browse files
authored
Add manual repo for duplicate span ids (#6475)
1 parent be2a895 commit af83cc5

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

playground/Stress/Stress.ApiService/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,16 @@ async IAsyncEnumerable<string> WriteOutput()
225225
return "Log with formatted data";
226226
});
227227

228+
app.MapGet("/duplicate-spanid", async () =>
229+
{
230+
var traceCreator = new TraceCreator();
231+
var span1 = traceCreator.CreateActivity("Test 1", "0485b1947fe788bb");
232+
await Task.Delay(1000);
233+
span1?.Stop();
234+
var span2 = traceCreator.CreateActivity("Test 2", "0485b1947fe788bb");
235+
await Task.Delay(1000);
236+
span2?.Stop();
237+
return $"Created duplicate span IDs.";
238+
});
239+
228240
app.Run();

playground/Stress/Stress.ApiService/TraceCreator.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
5+
using System.Reflection;
56

67
namespace Stress.ApiService;
78

@@ -13,6 +14,22 @@ public class TraceCreator
1314

1415
private readonly List<Activity> _allActivities = new List<Activity>();
1516

17+
public Activity? CreateActivity(string name, string? spandId)
18+
{
19+
var activity = s_activitySource.StartActivity(name, ActivityKind.Client);
20+
if (activity != null)
21+
{
22+
if (spandId != null)
23+
{
24+
// Gross but it's the only way.
25+
typeof(Activity).GetField("_spanId", BindingFlags.Instance | BindingFlags.NonPublic)!.SetValue(activity, spandId);
26+
typeof(Activity).GetField("_traceId", BindingFlags.Instance | BindingFlags.NonPublic)!.SetValue(activity, activity.TraceId.ToString());
27+
}
28+
}
29+
30+
return activity;
31+
}
32+
1633
public async Task CreateTraceAsync(int count, bool createChildren)
1734
{
1835
var activityStack = new Stack<Activity>();

0 commit comments

Comments
 (0)