Skip to content

Commit de8dc9a

Browse files
jamescrosswellgetsentry-botFlash0ver
authored
Ensure template is not sent for Structured logs with no parameters (#4544)
Co-authored-by: Sentry Github Bot <[email protected]> Co-authored-by: Stefan Pölz <[email protected]>
1 parent e9f75ac commit de8dc9a

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

CHANGELOG.md

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

1010
### Fixes
1111

12+
- Templates are no longer sent with Structured Logs that have no parameters ([#4544](https://github.com/getsentry/sentry-dotnet/pull/4544))
1213
- Upload linked PDBs to fix non-IL-stripped symbolication for iOS ([#4527](https://github.com/getsentry/sentry-dotnet/pull/4527))
1314
- 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))
1415
- Fixed WASM0001 warning when building Blazor WebAssembly projects ([#4519](https://github.com/getsentry/sentry-dotnet/pull/4519))

src/Sentry/SentryLog.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ internal void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger)
225225
writer.WritePropertyName("attributes");
226226
writer.WriteStartObject();
227227

228-
if (Template is not null)
228+
// the SDK MUST NOT attach a sentry.message.template attribute if there are no parameters
229+
// https://develop.sentry.dev/sdk/telemetry/logs/#default-attributes
230+
if (Template is not null && !Parameters.IsDefaultOrEmpty)
229231
{
230232
SentryAttributeSerializer.WriteStringAttribute(writer, "sentry.message.template", Template);
231233
}

test/Sentry.Tests/SentryLogTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,30 @@ public void Protocol_Default_VerifyAttributes()
6767
notFound.Should().BeNull();
6868
}
6969

70+
[Theory]
71+
[InlineData(true)]
72+
[InlineData(false)]
73+
public void WriteTo_NoParameters_NoTemplate(bool hasParameters)
74+
{
75+
// Arrange
76+
ImmutableArray<KeyValuePair<string, object>> parameters = hasParameters
77+
? [new KeyValuePair<string, object>("param", "params")]
78+
: [];
79+
var log = new SentryLog(Timestamp, TraceId, SentryLogLevel.Debug, "message")
80+
{
81+
Template = "template",
82+
Parameters = parameters,
83+
ParentSpanId = ParentSpanId,
84+
};
85+
86+
// Act
87+
var document = log.ToJsonDocument(static (obj, writer, logger) => obj.WriteTo(writer, logger), _output);
88+
var attributes = document.RootElement.GetProperty("attributes");
89+
90+
// Assert
91+
attributes.TryGetProperty("sentry.message.template", out _).Should().Be(hasParameters);
92+
}
93+
7094
[Fact]
7195
public void WriteTo_Envelope_MinimalSerializedSentryLog()
7296
{

0 commit comments

Comments
 (0)