Skip to content

Commit 8662a41

Browse files
authored
Fix empty trace name when there is no root span (#5527)
1 parent e385e00 commit 8662a41

File tree

3 files changed

+128
-2
lines changed

3 files changed

+128
-2
lines changed

src/Aspire.Dashboard/Otlp/Model/OtlpTrace.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ public void AddSpan(OtlpSpan span)
6464
if (!added)
6565
{
6666
Spans.Insert(0, span);
67+
68+
// If there isn't a root span then the first span is used as the trace name.
69+
if (_rootSpan == null && !string.IsNullOrEmpty(span.ParentSpanId))
70+
{
71+
FullName = BuildFullName(span);
72+
}
6773
}
6874

6975
if (string.IsNullOrEmpty(span.ParentSpanId))
@@ -75,13 +81,18 @@ public void AddSpan(OtlpSpan span)
7581
if (string.IsNullOrEmpty(existingSpan.ParentSpanId))
7682
{
7783
_rootSpan = existingSpan;
78-
FullName = $"{existingSpan.Source.ApplicationName}: {existingSpan.Name}";
84+
FullName = BuildFullName(existingSpan);
7985
break;
8086
}
8187
}
8288
}
8389

8490
AssertSpanOrder();
91+
92+
static string BuildFullName(OtlpSpan existingSpan)
93+
{
94+
return $"{existingSpan.Source.ApplicationName}: {existingSpan.Name}";
95+
}
8596
}
8697

8798
[Conditional("DEBUG")]

tests/Aspire.Dashboard.Tests/TelemetryRepositoryTests/TraceTests.cs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,4 +944,119 @@ public void GetTraces_MultipleInstances()
944944
AssertId("2", trace.TraceId);
945945
});
946946
}
947+
948+
[Fact]
949+
public void AddTraces_OutOfOrder_FullName()
950+
{
951+
// Arrange
952+
var repository = CreateRepository();
953+
var request = new GetTracesRequest
954+
{
955+
ApplicationKey = new ApplicationKey("TestService", "TestId"),
956+
FilterText = string.Empty,
957+
StartIndex = 0,
958+
Count = 10
959+
};
960+
961+
// Act 1
962+
var addContext = new AddContext();
963+
repository.AddTraces(addContext, new RepeatedField<ResourceSpans>()
964+
{
965+
new ResourceSpans
966+
{
967+
Resource = CreateResource(),
968+
ScopeSpans =
969+
{
970+
new ScopeSpans
971+
{
972+
Scope = CreateScope(),
973+
Spans =
974+
{
975+
CreateSpan(traceId: "1", spanId: "1-3", startTime: s_testTime.AddMinutes(10), endTime: s_testTime.AddMinutes(10), parentSpanId: "1-1")
976+
}
977+
}
978+
}
979+
}
980+
});
981+
Assert.Equal(0, addContext.FailureCount);
982+
983+
// Assert 1
984+
var trace = Assert.Single(repository.GetTraces(request).PagedResult.Items);
985+
Assert.Equal("TestService: Test span. Id: 1-3", trace.FullName);
986+
987+
// Act 2
988+
repository.AddTraces(addContext, new RepeatedField<ResourceSpans>()
989+
{
990+
new ResourceSpans
991+
{
992+
Resource = CreateResource(),
993+
ScopeSpans =
994+
{
995+
new ScopeSpans
996+
{
997+
Scope = CreateScope(),
998+
Spans =
999+
{
1000+
CreateSpan(traceId: "1", spanId: "1-2", startTime: s_testTime.AddMinutes(5), endTime: s_testTime.AddMinutes(10), parentSpanId: "1-1")
1001+
}
1002+
}
1003+
}
1004+
}
1005+
});
1006+
Assert.Equal(0, addContext.FailureCount);
1007+
1008+
// Assert 2
1009+
trace = Assert.Single(repository.GetTraces(request).PagedResult.Items);
1010+
Assert.Equal("TestService: Test span. Id: 1-2", trace.FullName);
1011+
1012+
// Act 3
1013+
repository.AddTraces(addContext, new RepeatedField<ResourceSpans>()
1014+
{
1015+
new ResourceSpans
1016+
{
1017+
Resource = CreateResource(),
1018+
ScopeSpans =
1019+
{
1020+
new ScopeSpans
1021+
{
1022+
Scope = CreateScope(),
1023+
Spans =
1024+
{
1025+
CreateSpan(traceId: "1", spanId: "1-1", startTime: s_testTime.AddMinutes(10), endTime: s_testTime.AddMinutes(10))
1026+
}
1027+
}
1028+
}
1029+
}
1030+
});
1031+
Assert.Equal(0, addContext.FailureCount);
1032+
1033+
// Assert 3
1034+
trace = Assert.Single(repository.GetTraces(request).PagedResult.Items);
1035+
Assert.Equal("TestService: Test span. Id: 1-1", trace.FullName);
1036+
1037+
// Act 4
1038+
repository.AddTraces(addContext, new RepeatedField<ResourceSpans>()
1039+
{
1040+
new ResourceSpans
1041+
{
1042+
Resource = CreateResource(),
1043+
ScopeSpans =
1044+
{
1045+
new ScopeSpans
1046+
{
1047+
Scope = CreateScope(),
1048+
Spans =
1049+
{
1050+
CreateSpan(traceId: "1", spanId: "1-4", startTime: s_testTime, endTime: s_testTime.AddMinutes(10), parentSpanId: "1-1")
1051+
}
1052+
}
1053+
}
1054+
}
1055+
});
1056+
Assert.Equal(0, addContext.FailureCount);
1057+
1058+
// Assert 4
1059+
trace = Assert.Single(repository.GetTraces(request).PagedResult.Items);
1060+
Assert.Equal("TestService: Test span. Id: 1-1", trace.FullName);
1061+
}
9471062
}

tests/Shared/Telemetry/TelemetryTestHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public static Span CreateSpan(string traceId, string spanId, DateTime startTime,
152152
ParentSpanId = parentSpanId is null ? ByteString.Empty : ByteString.CopyFrom(Encoding.UTF8.GetBytes(parentSpanId)),
153153
StartTimeUnixNano = DateTimeToUnixNanoseconds(startTime),
154154
EndTimeUnixNano = DateTimeToUnixNanoseconds(endTime),
155-
Name = "Test span"
155+
Name = $"Test span. Id: {spanId}"
156156
};
157157
if (events != null)
158158
{

0 commit comments

Comments
 (0)