Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/9.0] Reject duplicate span ids #6471

Merged
merged 3 commits into from
Oct 24, 2024
Merged
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
15 changes: 15 additions & 0 deletions playground/Stress/Stress.ApiService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,19 @@ async IAsyncEnumerable<string> 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();
17 changes: 17 additions & 0 deletions playground/Stress/Stress.ApiService/TraceCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -13,6 +14,22 @@ public class TraceCreator

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

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<Activity>();
Expand Down
5 changes: 5 additions & 0 deletions src/Aspire.Dashboard/Otlp/Model/OtlpTrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public int CalculateDepth(OtlpSpan span)

public void AddSpan(OtlpSpan span)
{
if (Spans.Any(s => s.SpanId == span.SpanId))
{
throw new InvalidOperationException($"Duplicate span id '{span.SpanId}' detected.");
}

var added = false;
for (var i = Spans.Count - 1; i >= 0; i--)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,57 @@ public void AddTraces()
});
}

[Fact]
public void AddTraces_DuplicateTraceIds_Reject()
{
// Arrange
var repository = CreateRepository();
// Act
var addContext = new AddContext();
repository.AddTraces(addContext, new RepeatedField<ResourceSpans>()
{
new ResourceSpans
{
Resource = CreateResource(),
ScopeSpans =
{
new ScopeSpans
{
Scope = CreateScope(),
Spans =
{
CreateSpan(traceId: "1", spanId: "1-1", startTime: s_testTime.AddMinutes(1), endTime: s_testTime.AddMinutes(10)),
CreateSpan(traceId: "1", spanId: "1-1", startTime: s_testTime.AddMinutes(5), endTime: s_testTime.AddMinutes(10)),
CreateSpan(traceId: "1", spanId: "1-2", startTime: s_testTime.AddMinutes(1), endTime: s_testTime.AddMinutes(10), parentSpanId: "1-1"),
}
}
}
}
});
// Assert
Assert.Equal(1, addContext.FailureCount);
var applications = repository.GetApplications();
Assert.Collection(applications,
app =>
{
Assert.Equal("TestService", app.ApplicationName);
Assert.Equal("TestId", app.InstanceId);
});
var traces = repository.GetTraces(new GetTracesRequest
{
ApplicationKey = applications[0].ApplicationKey,
FilterText = string.Empty,
StartIndex = 0,
Count = 10,
Filters = []
});
Assert.Collection(traces.PagedResult.Items,
trace =>
{
Assert.Equal(2, trace.Spans.Count);
});
}

[Fact]
public void AddTraces_Scope_Multiple()
{
Expand Down