Skip to content
Closed
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
3 changes: 2 additions & 1 deletion src/SeqCli/Output/NativeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public static void WriteEvent(TextWriter output, EventEntity evt)
("@Elapsed", evt.Elapsed ?? UndefinedValue),
("@TraceId", evt.TraceId ?? UndefinedValue),
("@SpanId", evt.SpanId ?? UndefinedValue),
("@SpanKind", evt.SpanKind ?? UndefinedValue),
// This temporarily works around an events API bug; non-span events should not carry span kinds.
("@SpanKind", evt.Start != null ? evt.SpanKind ?? UndefinedValue : UndefinedValue),
("@Start", evt.Start != null ? DateTimeOffset.Parse(evt.Start).UtcDateTime : UndefinedValue),
("@ParentId", evt.ParentId ?? UndefinedValue),
("@Properties", evt.Properties?.Count > 0 ? new Action<TextWriter>(w => WritePropertiesObject(w, evt.Properties)) : UndefinedValue),
Expand Down
3 changes: 2 additions & 1 deletion src/SeqCli/Output/OutputFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ public static LogEvent ToSerilogEvent(EventEntity evt)
if (!string.IsNullOrWhiteSpace(evt.Start))
serilogEvent.AddOrUpdateProperty(new("@st", new ScalarValue(evt.Start)));

if (!string.IsNullOrWhiteSpace(evt.SpanKind))
// This temporarily works around an events API bug; non-span events should not carry span kinds.
if (!string.IsNullOrWhiteSpace(evt.Start) && !string.IsNullOrWhiteSpace(evt.SpanKind))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of IsNullOrWhitespace is consistent with the handling of Start a little earlier in the same method, but I believe null equality checks are sufficient; I didn't want to create more churn in this PR, but we might want to review OutputFormat, which is a bit older than NativeFormatter, and get the two into agreement about what communicates a "missing" value.

serilogEvent.AddOrUpdateProperty(new("@sk", new ScalarValue(evt.SpanKind)));

return serilogEvent;
Expand Down
16 changes: 15 additions & 1 deletion test/SeqCli.Tests/Output/NativeFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ public static IEnumerable<object[]> EventPropertyCases() =>
[Some.MakeEvent(e => e.Elapsed = TimeSpan.FromSeconds(13)), "@Elapsed: 13s"],
[Some.MakeEvent(e => e.TraceId = "abc123"), "@TraceId: 'abc123'"],
[Some.MakeEvent(e => e.SpanId = "def456"), "@SpanId: 'def456'"],
[Some.MakeEvent(e => e.SpanKind = "server"), "@SpanKind: 'server'"],
[
Some.MakeEvent(e =>
{
e.Start = "2024-01-01T00:00:00.0000000Z";
e.SpanKind = "server";
}),
"@SpanKind: 'server'"
],
[Some.MakeEvent(e => e.Start = "2024-01-01T00:00:00.0000000Z"), "@Start: DateTime('2024-01-01T00:00:00.0000000Z')"],
[Some.MakeEvent(e => e.ParentId = "p1"), "@ParentId: 'p1'"],
[Some.MakeEvent(e => e.Properties = Some.MakeProperties(("UserId", 42))), "@Properties: {UserId: 42}"],
Expand Down Expand Up @@ -118,6 +125,13 @@ public void OptionalPropertiesAreOmittedWhenAbsent(string token)
Assert.DoesNotContain(token, Render(Some.MakeEvent()));
}

[Fact]
public void SpanKindIsOmittedFromEventsThatAreNotSpans()
{
// The events API reports a span kind for every event, defaulting to `Internal`.
Assert.DoesNotContain("@SpanKind", Render(Some.MakeEvent(e => e.SpanKind = "Internal")));
}

[Fact]
public void EmptyPropertyCollectionIsOmitted()
{
Expand Down
36 changes: 36 additions & 0 deletions test/SeqCli.Tests/Output/OutputFormatTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#nullable enable
using SeqCli.Tests.Support;
using Serilog.Events;
using Xunit;
using OutputFormat = SeqCli.Output.OutputFormat;

namespace SeqCli.Tests.Output;

public class OutputFormatTests
{
[Fact]
public void SpanKindIsMappedFromSpans()
{
var evt = OutputFormat.ToSerilogEvent(Some.MakeEvent(e =>
{
e.Properties = [];
e.Start = "2024-01-01T00:00:00.0000000Z";
e.SpanKind = "Server";
}));

Assert.Equal("Server", Assert.IsType<ScalarValue>(evt.Properties["@sk"]).Value);
}

[Fact]
public void SpanKindIsOmittedFromEventsThatAreNotSpans()
{
// The events API reports a span kind for every event, defaulting to `Internal`.
var evt = OutputFormat.ToSerilogEvent(Some.MakeEvent(e =>
{
e.Properties = [];
e.SpanKind = "Internal";
}));

Assert.DoesNotContain("@sk", evt.Properties.Keys);
}
}