Skip to content

Commit e6a9743

Browse files
committed
ref(logs): apply default attributes from scope
1 parent d116dd9 commit e6a9743

File tree

5 files changed

+91
-83
lines changed

5 files changed

+91
-83
lines changed

src/Sentry.Extensions.Logging/SentryStructuredLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
8787
ParentSpanId = spanId,
8888
};
8989

90-
log.SetDefaultAttributes(_options, _sdk);
90+
log.SetDefaultAttributes(_options, _hub.GetScope(), _sdk);
9191
log.SetOrigin("auto.log.extensions_logging");
9292

9393
if (_categoryName is not null)

src/Sentry.Serilog/SentrySink.Structured.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private static void CaptureStructuredLog(IHub hub, SentryOptions options, LogEve
1717
ParentSpanId = spanId,
1818
};
1919

20-
log.SetDefaultAttributes(options, Sdk);
20+
log.SetDefaultAttributes(options, hub.GetScope(), Sdk);
2121
log.SetOrigin("auto.log.serilog");
2222

2323
foreach (var attribute in attributes)

src/Sentry/Internal/DefaultSentryStructuredLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private protected override void CaptureLog(SentryLogLevel level, string template
6969
}
7070

7171
var scope = _hub.GetScope();
72-
log.SetDefaultAttributes(_options, scope?.Sdk ?? SdkVersion.Instance);
72+
log.SetDefaultAttributes(_options, scope, scope?.Sdk ?? SdkVersion.Instance);
7373

7474
CaptureLog(log);
7575
}

src/Sentry/SentryLog.cs

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal SentryLog(DateTimeOffset timestamp, SentryId traceId, SentryLogLevel le
2424
TraceId = traceId;
2525
Level = level;
2626
Message = message;
27-
// we currently set up to 18 default attributes, 23 is the next prime number
27+
// we currently set up to 18 default attributes, the next prime number is 23
2828
_attributes = new Dictionary<string, SentryAttribute>(23);
2929
}
3030

@@ -163,12 +163,12 @@ internal void SetAttribute(string key, int value)
163163
_attributes[key] = new SentryAttribute(value, "integer");
164164
}
165165

166-
internal void SetDefaultAttributes(SentryOptions options, SdkVersion sdk)
166+
internal void SetDefaultAttributes(SentryOptions options, Scope scope)
167167
{
168-
SetDefaultAttributes(options, sdk, ReplaySession.Instance, SentrySdk.CurrentHub);
168+
SetDefaultAttributes(options, scope, scope.Sdk);
169169
}
170170

171-
internal void SetDefaultAttributes(SentryOptions options, SdkVersion sdk, IReplaySession replaySession, IHub hub)
171+
internal void SetDefaultAttributes(SentryOptions options, Scope? scope, SdkVersion sdk)
172172
{
173173
var environment = options.SettingLocator.GetEnvironment();
174174
SetAttribute("sentry.environment", environment);
@@ -188,67 +188,70 @@ internal void SetDefaultAttributes(SentryOptions options, SdkVersion sdk, IRepla
188188
SetAttribute("sentry.sdk.version", version);
189189
}
190190

191-
var replayId = replaySession.ActiveReplayId;
192-
if (replayId.HasValue && replayId.Value != SentryId.Empty)
191+
if (scope is null)
193192
{
194-
SetAttribute("sentry.replay_id", replayId.Value.ToString());
193+
return;
195194
}
196195

197-
var scope = hub.GetScope();
198-
if (scope is not null)
196+
if (scope.PropagationContext._dynamicSamplingContext is { } dynamicSamplingContext)
199197
{
200-
if (scope.User.Id is { } userId)
201-
{
202-
SetAttribute("user.id", userId);
203-
}
204-
if (scope.User.Username is { } userName)
205-
{
206-
SetAttribute("user.name", userName);
207-
}
208-
if (scope.User.Email is { } userEmail)
198+
if (dynamicSamplingContext.Items.TryGetValue("replay_id", out var replayId))
209199
{
210-
SetAttribute("user.email", userEmail);
200+
SetAttribute("sentry.replay_id", replayId);
211201
}
202+
}
212203

213-
if (scope.Contexts.Browser.Name is { } browserName)
214-
{
215-
SetAttribute("browser.name", browserName);
216-
}
217-
if (scope.Contexts.Browser.Version is { } browserVersion)
218-
{
219-
SetAttribute("browser.version", browserVersion);
220-
}
204+
if (scope.User.Id is { } userId)
205+
{
206+
SetAttribute("user.id", userId);
207+
}
208+
if (scope.User.Username is { } userName)
209+
{
210+
SetAttribute("user.name", userName);
211+
}
212+
if (scope.User.Email is { } userEmail)
213+
{
214+
SetAttribute("user.email", userEmail);
215+
}
221216

222-
var serverAddress = options.ServerName;
223-
if (!string.IsNullOrEmpty(serverAddress))
224-
{
225-
SetAttribute("server.address", serverAddress);
226-
}
227-
else if (options.SendDefaultPii)
228-
{
229-
SetAttribute("server.address", Environment.MachineName);
230-
}
217+
if (scope.Contexts.Browser.Name is { } browserName)
218+
{
219+
SetAttribute("browser.name", browserName);
220+
}
221+
if (scope.Contexts.Browser.Version is { } browserVersion)
222+
{
223+
SetAttribute("browser.version", browserVersion);
224+
}
231225

232-
if (scope.Contexts.OperatingSystem.Name is { } osName)
233-
{
234-
SetAttribute("os.name", osName);
235-
}
236-
if (scope.Contexts.OperatingSystem.Version is { } osVersion)
237-
{
238-
SetAttribute("os.version", osVersion);
239-
}
240-
if (scope.Contexts.Device.Brand is { } deviceBrand)
241-
{
242-
SetAttribute("device.brand", deviceBrand);
243-
}
244-
if (scope.Contexts.Device.Model is { } deviceModel)
245-
{
246-
SetAttribute("device.model", deviceModel);
247-
}
248-
if (scope.Contexts.Device.Family is { } deviceFamily)
249-
{
250-
SetAttribute("device.family", deviceFamily);
251-
}
226+
var serverAddress = options.ServerName;
227+
if (!string.IsNullOrEmpty(serverAddress))
228+
{
229+
SetAttribute("server.address", serverAddress);
230+
}
231+
else if (options.SendDefaultPii)
232+
{
233+
SetAttribute("server.address", Environment.MachineName);
234+
}
235+
236+
if (scope.Contexts.OperatingSystem.Name is { } osName)
237+
{
238+
SetAttribute("os.name", osName);
239+
}
240+
if (scope.Contexts.OperatingSystem.Version is { } osVersion)
241+
{
242+
SetAttribute("os.version", osVersion);
243+
}
244+
if (scope.Contexts.Device.Brand is { } deviceBrand)
245+
{
246+
SetAttribute("device.brand", deviceBrand);
247+
}
248+
if (scope.Contexts.Device.Model is { } deviceModel)
249+
{
250+
SetAttribute("device.model", deviceModel);
251+
}
252+
if (scope.Contexts.Device.Family is { } deviceFamily)
253+
{
254+
SetAttribute("device.family", deviceFamily);
252255
}
253256
}
254257

test/Sentry.Tests/SentryLogTests.cs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ public void Protocol_Default_VerifyAttributes()
3030
Environment = "my-environment",
3131
Release = "my-release",
3232
};
33-
var sdk = new SdkVersion
33+
var scope = new Scope(options)
3434
{
35-
Name = "Sentry.Test.SDK",
36-
Version = "1.2.3-test+Sentry"
35+
Sdk =
36+
{
37+
Name = "Sentry.Test.SDK",
38+
Version = "1.2.3-test+Sentry",
39+
}
3740
};
3841

3942
var log = new SentryLog(Timestamp, TraceId, (SentryLogLevel)24, "message")
@@ -43,7 +46,7 @@ public void Protocol_Default_VerifyAttributes()
4346
ParentSpanId = ParentSpanId,
4447
};
4548
log.SetAttribute("attribute", "value");
46-
log.SetDefaultAttributes(options, sdk);
49+
log.SetDefaultAttributes(options, scope);
4750

4851
log.Timestamp.Should().Be(Timestamp);
4952
log.TraceId.Should().Be(TraceId);
@@ -63,9 +66,9 @@ public void Protocol_Default_VerifyAttributes()
6366
log.TryGetAttribute("sentry.release", out string release).Should().BeTrue();
6467
release.Should().Be(options.Release);
6568
log.TryGetAttribute("sentry.sdk.name", out string name).Should().BeTrue();
66-
name.Should().Be(sdk.Name);
69+
name.Should().Be(scope.Sdk.Name);
6770
log.TryGetAttribute("sentry.sdk.version", out string version).Should().BeTrue();
68-
version.Should().Be(sdk.Version);
71+
version.Should().Be(scope.Sdk.Version);
6972
log.TryGetAttribute("not-found", out object notFound).Should().BeFalse();
7073
notFound.Should().BeNull();
7174
}
@@ -81,18 +84,15 @@ public void Protocol_Default_VerifyAdditionalAttributes(bool hasAdditionalDefaul
8184
Release = "my-release",
8285
ServerName = hasAdditionalDefaultAttributes ? "my-server-address" : null,
8386
};
84-
var sdk = new SdkVersion
85-
{
86-
Name = "Sentry.Test.SDK",
87-
Version = "1.2.3-test+Sentry"
88-
};
89-
90-
var replaySession = Substitute.For<IReplaySession>();
91-
replaySession.ActiveReplayId.Returns(hasAdditionalDefaultAttributes ? SentryId.Create() : SentryId.Empty);
92-
var hub = Substitute.For<IHub>();
9387
var scope = new Scope(options);
88+
9489
if (hasAdditionalDefaultAttributes)
9590
{
91+
options.Dsn = ValidDsn;
92+
var replaySession = Substitute.For<IReplaySession>();
93+
replaySession.ActiveReplayId.Returns(SentryId.Parse("f18176ecbb544e549fd23fbbe39064cc"));
94+
_ = scope.PropagationContext.GetOrCreateDynamicSamplingContext(options, replaySession);
95+
9696
scope.User = new SentryUser
9797
{
9898
Id = "my-user-id",
@@ -107,7 +107,6 @@ public void Protocol_Default_VerifyAdditionalAttributes(bool hasAdditionalDefaul
107107
scope.Contexts.Device.Model = "my-device-model";
108108
scope.Contexts.Device.Family = "my-device-family";
109109
}
110-
hub.SubstituteConfigureScope(scope);
111110

112111
var log = new SentryLog(Timestamp, TraceId, (SentryLogLevel)24, "message")
113112
{
@@ -116,7 +115,7 @@ public void Protocol_Default_VerifyAdditionalAttributes(bool hasAdditionalDefaul
116115
ParentSpanId = ParentSpanId,
117116
};
118117
log.SetAttribute("attribute", "value");
119-
log.SetDefaultAttributes(options, sdk, replaySession, hub);
118+
log.SetDefaultAttributes(options, scope);
120119

121120
log.TryGetAttribute("sentry.replay_id", out string replayId).Should().Be(hasAdditionalDefaultAttributes);
122121
log.TryGetAttribute("user.id", out string userId).Should().Be(hasAdditionalDefaultAttributes);
@@ -133,7 +132,7 @@ public void Protocol_Default_VerifyAdditionalAttributes(bool hasAdditionalDefaul
133132

134133
if (hasAdditionalDefaultAttributes)
135134
{
136-
replayId.Should().Be(replaySession.ActiveReplayId.ToString());
135+
replayId.Should().Be("f18176ecbb544e549fd23fbbe39064cc");
137136
userId.Should().Be("my-user-id");
138137
userName.Should().Be("my-user-name");
139138
userEmail.Should().Be("my-user-email");
@@ -181,9 +180,10 @@ public void WriteTo_Envelope_MinimalSerializedSentryLog()
181180
Release = "my-release",
182181
SendDefaultPii = false,
183182
};
183+
var scope = new Scope(options);
184184

185185
var log = new SentryLog(Timestamp, TraceId, SentryLogLevel.Trace, "message");
186-
log.SetDefaultAttributes(options, new SdkVersion());
186+
log.SetDefaultAttributes(options, scope);
187187

188188
var envelope = Envelope.FromLog(new StructuredLog([log]));
189189

@@ -251,13 +251,19 @@ public void WriteTo_EnvelopeItem_MaximalSerializedSentryLog()
251251
Release = "my-release",
252252
ServerName = "my-server-address",
253253
};
254-
255254
var replaySession = Substitute.For<IReplaySession>();
256255
var replayId = SentryId.Create();
257256
replaySession.ActiveReplayId.Returns(replayId);
258-
var hub = Substitute.For<IHub>();
259-
var scope = new Scope(options)
257+
var dynamicSamplingContext = DynamicSamplingContext.Empty();
258+
dynamicSamplingContext.SetReplayId(replaySession);
259+
var propagationContext = new SentryPropagationContext(TraceId, ParentSpanId!.Value, dynamicSamplingContext);
260+
var scope = new Scope(options, propagationContext)
260261
{
262+
Sdk =
263+
{
264+
Name = "Sentry.Test.SDK",
265+
Version = "1.2.3-test+Sentry",
266+
},
261267
User = new SentryUser
262268
{
263269
Id = "my-user-id",
@@ -284,7 +290,6 @@ public void WriteTo_EnvelopeItem_MaximalSerializedSentryLog()
284290
},
285291
},
286292
};
287-
hub.SubstituteConfigureScope(scope);
288293

289294
var log = new SentryLog(Timestamp, TraceId, (SentryLogLevel)24, "message")
290295
{
@@ -296,7 +301,7 @@ public void WriteTo_EnvelopeItem_MaximalSerializedSentryLog()
296301
log.SetAttribute("boolean-attribute", true);
297302
log.SetAttribute("integer-attribute", 3);
298303
log.SetAttribute("double-attribute", 4.4);
299-
log.SetDefaultAttributes(options, new SdkVersion { Name = "Sentry.Test.SDK", Version = "1.2.3-test+Sentry" }, replaySession, hub);
304+
log.SetDefaultAttributes(options, scope);
300305

301306
var envelope = EnvelopeItem.FromLog(new StructuredLog([log]));
302307

0 commit comments

Comments
 (0)