diff --git a/playground/Stress/Stress.ApiService/Program.cs b/playground/Stress/Stress.ApiService/Program.cs index 086e6fafb9c..bcf9429ebbe 100644 --- a/playground/Stress/Stress.ApiService/Program.cs +++ b/playground/Stress/Stress.ApiService/Program.cs @@ -225,4 +225,16 @@ async IAsyncEnumerable WriteOutput() return "Log with formatted data"; }); +app.MapGet("/duplicate-spanid", async () => +{ + var traceCreator = new TraceCreator(); + var span1 = traceCreator.CreateActivity("Test 1", "0485b1947fe788bb"); + await Task.Delay(1000); + span1?.Stop(); + var span2 = traceCreator.CreateActivity("Test 2", "0485b1947fe788bb"); + await Task.Delay(1000); + span2?.Stop(); + return $"Created duplicate span IDs."; +}); + app.Run(); diff --git a/playground/Stress/Stress.ApiService/TraceCreator.cs b/playground/Stress/Stress.ApiService/TraceCreator.cs index 5a7accc699d..c8b08c60a8e 100644 --- a/playground/Stress/Stress.ApiService/TraceCreator.cs +++ b/playground/Stress/Stress.ApiService/TraceCreator.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; +using System.Reflection; namespace Stress.ApiService; @@ -13,6 +14,22 @@ public class TraceCreator private readonly List _allActivities = new List(); + public Activity? CreateActivity(string name, string? spandId) + { + var activity = s_activitySource.StartActivity(name, ActivityKind.Client); + if (activity != null) + { + if (spandId != null) + { + // Gross but it's the only way. + typeof(Activity).GetField("_spanId", BindingFlags.Instance | BindingFlags.NonPublic)!.SetValue(activity, spandId); + typeof(Activity).GetField("_traceId", BindingFlags.Instance | BindingFlags.NonPublic)!.SetValue(activity, activity.TraceId.ToString()); + } + } + + return activity; + } + public async Task CreateTraceAsync(int count, bool createChildren) { var activityStack = new Stack();