Skip to content

Commit f33f4d4

Browse files
authored
fix: Ensure structured logs from an SDK integration has sentry.origin (#4566)
Structured Logs now have `sentry.origin` following the documentations
1 parent c57c0f2 commit f33f4d4

File tree

9 files changed

+23
-0
lines changed

9 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixes
6+
7+
- Structured Logs now have a `sentry.origin` attribute to so it's clearer where these come from ([#4566](https://github.com/getsentry/sentry-dotnet/pull/4566))
8+
59
### Dependencies
610

711
- Bump Java SDK from v8.22.0 to v8.23.0 ([#4586](https://github.com/getsentry/sentry-dotnet/pull/4586))

src/Sentry.Extensions.Logging/SentryStructuredLogger.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
8989
};
9090

9191
log.SetDefaultAttributes(_options, _sdk);
92+
log.SetOrigin("auto.log.extensions_logging");
9293

9394
if (_categoryName is not null)
9495
{

src/Sentry.Serilog/SentrySink.Structured.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ private static void CaptureStructuredLog(IHub hub, SentryOptions options, LogEve
1818
};
1919

2020
log.SetDefaultAttributes(options, Sdk);
21+
log.SetOrigin("auto.log.serilog");
2122

2223
foreach (var attribute in attributes)
2324
{

src/Sentry/SentryLog.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ internal void SetDefaultAttributes(SentryOptions options, SdkVersion sdk)
184184
}
185185
}
186186

187+
internal void SetOrigin(string origin)
188+
{
189+
SetAttribute("sentry.origin", origin);
190+
}
191+
187192
internal void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger)
188193
{
189194
writer.WriteStartObject();

test/Sentry.AspNetCore.Tests/SentryAspNetCoreStructuredLoggerProviderTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ public void CreateLogger_DependencyInjection_CanLog()
9191

9292
capturedLog.TryGetAttribute("sentry.sdk.version", out object? version).Should().BeTrue();
9393
version.Should().Be(SentryMiddleware.NameAndVersion.Version);
94+
95+
capturedLog.TryGetAttribute("sentry.origin", out object? origin).Should().BeTrue();
96+
origin.Should().Be("auto.log.extensions_logging");
9497
}
9598

9699
[Fact]

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ public void CreateLogger_DependencyInjection_CanLog()
9191

9292
capturedLog.TryGetAttribute("sentry.sdk.version", out object? version).Should().BeTrue();
9393
version.Should().Be(SentryLoggerProvider.NameAndVersion.Version);
94+
95+
capturedLog.TryGetAttribute("sentry.origin", out object? origin).Should().BeTrue();
96+
origin.Should().Be("auto.log.extensions_logging");
9497
}
9598

9699
[Fact]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public void Log_LogLevel_CaptureLog(LogLevel logLevel, SentryLogLevel expectedLe
111111
log.ParentSpanId.Should().Be(parentSpanId);
112112
log.AssertAttribute("sentry.environment", "my-environment");
113113
log.AssertAttribute("sentry.release", "my-release");
114+
log.AssertAttribute("sentry.origin", "auto.log.extensions_logging");
114115
log.AssertAttribute("sentry.sdk.name", "SDK Name");
115116
log.AssertAttribute("sentry.sdk.version", "SDK Version");
116117
log.AssertAttribute("category.name", _fixture.CategoryName);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ public void Emit_StructuredLogging_LogEvent(bool withActiveSpan)
118118
environment.Should().Be("test-environment");
119119
log.TryGetAttribute("sentry.release", out object? release).Should().BeTrue();
120120
release.Should().Be("test-release");
121+
log.TryGetAttribute("sentry.origin", out object? origin).Should().BeTrue();
122+
origin.Should().Be("auto.log.serilog");
121123
log.TryGetAttribute("sentry.sdk.name", out object? sdkName).Should().BeTrue();
122124
sdkName.Should().Be(SentrySink.SdkName);
123125
log.TryGetAttribute("sentry.sdk.version", out object? sdkVersion).Should().BeTrue();

test/Sentry.Tests/SentryLogTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public void Protocol_Default_VerifyAttributes()
5353
log.Parameters.Should().BeEquivalentTo(new KeyValuePair<string, object>[] { new("param", "params"), });
5454
log.ParentSpanId.Should().Be(ParentSpanId);
5555

56+
// should only show up in sdk integrations
57+
log.TryGetAttribute("sentry.origin", out object origin).Should().BeFalse();
58+
5659
log.TryGetAttribute("attribute", out object attribute).Should().BeTrue();
5760
attribute.Should().Be("value");
5861
log.TryGetAttribute("sentry.environment", out string environment).Should().BeTrue();

0 commit comments

Comments
 (0)