Skip to content

Commit 5b42cad

Browse files
committed
fix: only set sentry.trace.parent_span_id when Span was active
1 parent a36720b commit 5b42cad

File tree

6 files changed

+24
-14
lines changed

6 files changed

+24
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
### Fixes
1010

11+
- Parent-Span-IDs are no longer sent with Structured Logs when recorded without an active Span ([#4565](https://github.com/getsentry/sentry-dotnet/pull/4565))
1112
- Upload linked PDBs to fix non-IL-stripped symbolication for iOS ([#4527](https://github.com/getsentry/sentry-dotnet/pull/4527))
1213
- In MAUI Android apps, generate and inject UUID to APK and upload ProGuard mapping to Sentry with the UUID ([#4532](https://github.com/getsentry/sentry-dotnet/pull/4532))
1314
- Fixed WASM0001 warning when building Blazor WebAssembly projects ([#4519](https://github.com/getsentry/sentry-dotnet/pull/4519))

src/Sentry/SentryLog.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
using Sentry.Extensibility;
22
using Sentry.Infrastructure;
3+
using Sentry.Internal;
34
using Sentry.Protocol;
45

56
namespace Sentry;
67

78
/// <summary>
8-
/// Represents the Sentry Log protocol.
9+
/// Represents a Sentry Structured Log.
910
/// <para>This API is experimental and it may change in the future.</para>
1011
/// </summary>
12+
/// <remarks>
13+
/// Sentry Docs: <see href="https://docs.sentry.io/product/explore/logs/"/>.
14+
/// Sentry Developer Documentation: <see href="https://develop.sentry.dev/sdk/telemetry/logs/"/>.
15+
/// Sentry .NET SDK Docs: <see href="https://docs.sentry.io/platforms/dotnet/logs/"/>.
16+
/// </remarks>
1117
[Experimental(DiagnosticId.ExperimentalFeature)]
1218
[DebuggerDisplay(@"SentryLog \{ Level = {Level}, Message = '{Message}' \}")]
1319
public sealed class SentryLog
@@ -260,23 +266,26 @@ internal void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger)
260266

261267
internal static void GetTraceIdAndSpanId(IHub hub, out SentryId traceId, out SpanId? spanId)
262268
{
263-
var span = hub.GetSpan();
264-
if (span is not null)
269+
var activeSpan = hub.GetSpan();
270+
if (activeSpan is not null)
265271
{
266-
traceId = span.TraceId;
267-
spanId = span.SpanId;
272+
traceId = activeSpan.TraceId;
273+
spanId = activeSpan.SpanId;
268274
return;
269275
}
270276

277+
// set "sentry.trace.parent_span_id" to the ID of the Span that was active when the Log was collected
278+
// do not set "sentry.trace.parent_span_id" if there was no active Span
279+
spanId = null;
280+
271281
var scope = hub.GetScope();
272282
if (scope is not null)
273283
{
274284
traceId = scope.PropagationContext.TraceId;
275-
spanId = scope.PropagationContext.SpanId;
276285
return;
277286
}
278287

288+
Debug.Assert(hub is not Hub, "In case of a 'full' Hub, there is always a Scope. Otherwise (disabled) there is no Scope, but this branch should be unreachable.");
279289
traceId = SentryId.Empty;
280-
spanId = null;
281290
}
282291
}

test/Sentry.Extensions.Logging.Tests/SentryStructuredLoggerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void Log_WithoutActiveSpan_CaptureLog()
140140

141141
var log = _fixture.CapturedLogs.Dequeue();
142142
log.TraceId.Should().Be(scope.PropagationContext.TraceId);
143-
log.ParentSpanId.Should().Be(scope.PropagationContext.SpanId);
143+
log.ParentSpanId.Should().BeNull();
144144
}
145145

146146
[Fact]

test/Sentry.Serilog.Tests/SentrySinkTests.Structured.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void Emit_StructuredLogging_LogEvent(bool withActiveSpan)
112112
log.Parameters[1].Should().BeEquivalentTo(new KeyValuePair<string, string>("Sequence", "[41, 42, 43]"));
113113
log.Parameters[2].Should().BeEquivalentTo(new KeyValuePair<string, string>("Dictionary", """[("key": "value")]"""));
114114
log.Parameters[3].Should().BeEquivalentTo(new KeyValuePair<string, string>("Structure", """[42, "42"]"""));
115-
log.ParentSpanId.Should().Be(withActiveSpan ? _fixture.Hub.GetSpan()!.SpanId : _fixture.Scope.PropagationContext.SpanId);
115+
log.ParentSpanId.Should().Be(withActiveSpan ? _fixture.Hub.GetSpan()!.SpanId : null);
116116

117117
log.TryGetAttribute("sentry.environment", out object? environment).Should().BeTrue();
118118
environment.Should().Be("test-environment");

test/Sentry.Tests/SentryLogTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public void WriteTo_Attributes_AsJson()
384384
}
385385

386386
[Fact]
387-
public void GetTraceIdAndSpanId_WithActiveSpan_ReturnAsOut()
387+
public void GetTraceIdAndSpanId_WithActiveSpan_HasBothTraceIdAndSpanId()
388388
{
389389
// Arrange
390390
var span = Substitute.For<ISpan>();
@@ -403,7 +403,7 @@ public void GetTraceIdAndSpanId_WithActiveSpan_ReturnAsOut()
403403
}
404404

405405
[Fact]
406-
public void GetTraceIdAndSpanId_WithoutActiveSpan_ReturnAsOut()
406+
public void GetTraceIdAndSpanId_WithoutActiveSpan_HasOnlyTraceIdButNoSpanId()
407407
{
408408
// Arrange
409409
var hub = Substitute.For<IHub>();
@@ -417,11 +417,11 @@ public void GetTraceIdAndSpanId_WithoutActiveSpan_ReturnAsOut()
417417

418418
// Assert
419419
traceId.Should().Be(scope.PropagationContext.TraceId);
420-
spanId.Should().Be(scope.PropagationContext.SpanId);
420+
spanId.Should().BeNull();
421421
}
422422

423423
[Fact]
424-
public void GetTraceIdAndSpanId_WithoutIds_ReturnAsOut()
424+
public void GetTraceIdAndSpanId_WithoutIds_ShouldBeUnreachable()
425425
{
426426
// Arrange
427427
var hub = Substitute.For<IHub>();

test/Sentry.Tests/SentryStructuredLoggerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void WithoutActiveSpan()
5555
var scope = new Scope();
5656
Hub.SubstituteConfigureScope(scope);
5757
TraceId = scope.PropagationContext.TraceId;
58-
ParentSpanId = scope.PropagationContext.SpanId;
58+
ParentSpanId = null;
5959
}
6060

6161
public SentryStructuredLogger GetSut() => SentryStructuredLogger.Create(Hub, Options, Clock, BatchSize, BatchTimeout);

0 commit comments

Comments
 (0)