-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathEnricher.cs
More file actions
116 lines (97 loc) · 4.03 KB
/
Enricher.cs
File metadata and controls
116 lines (97 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using Sentry.PlatformAbstractions;
using OperatingSystem = Sentry.Protocol.OperatingSystem;
using Runtime = Sentry.Protocol.Runtime;
namespace Sentry.Internal;
internal class Enricher
{
internal const string DefaultIpAddress = "{{auto}}";
private readonly SentryOptions _options;
private readonly Lazy<Runtime> _runtimeLazy = new(() =>
{
var current = SentryRuntime.Current;
return new Runtime
{
Name = current.Name,
Version = current.Version,
Identifier = current.Identifier,
RawDescription = current.Raw
};
});
public Enricher(SentryOptions options) => _options = options;
public void Apply(IEventLike eventLike)
{
// Runtime
if (!eventLike.Contexts.ContainsKey(Runtime.Type))
{
eventLike.Contexts[Runtime.Type] = _runtimeLazy.Value;
}
// Operating System
if (!eventLike.Contexts.ContainsKey(OperatingSystem.Type))
{
// RuntimeInformation.OSDescription is throwing on Mono 5.12
if (!SentryRuntime.Current.IsMono())
{
#if NETFRAMEWORK
// RuntimeInformation.* throws on .NET Framework on macOS/Linux
try {
eventLike.Contexts.OperatingSystem.RawDescription = RuntimeInformation.OSDescription;
} catch {
eventLike.Contexts.OperatingSystem.RawDescription = Environment.OSVersion.VersionString;
}
#else
eventLike.Contexts.OperatingSystem.RawDescription = RuntimeInformation.OSDescription;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
// works for catalyst and net9 base
eventLike.Contexts.OperatingSystem.Name = "macOS";
eventLike.Contexts.OperatingSystem.Version = Environment.OSVersion.Version.ToString(); // reports macOS version (ie. 15.3.0)
}
#endif
}
}
// SDK Name/Version might have be already set by an outer package
// e.g.: ASP.NET Core can set itself as the SDK
if (eventLike.Sdk.Version is null && eventLike.Sdk.Name is null)
{
eventLike.Sdk.Name = Constants.SdkName;
eventLike.Sdk.Version = SdkVersion.Instance.Version;
}
if (SdkVersion.Instance.Version is not null)
{
eventLike.Sdk.AddPackage("nuget:" + SdkVersion.Instance.Name, SdkVersion.Instance.Version);
}
// Release
eventLike.Release ??= _options.SettingLocator.GetRelease();
// Distribution
eventLike.Distribution ??= _options.Distribution;
// Environment
eventLike.Environment ??= _options.SettingLocator.GetEnvironment();
// User
// Report local user if opt-in PII, no user was already set to event and feature not opted-out:
if (_options.SendDefaultPii)
{
if (_options.IsEnvironmentUser && !eventLike.HasUser())
{
eventLike.User.Username = Environment.UserName;
}
eventLike.User.IpAddress ??= DefaultIpAddress;
}
// Set by the GlobalRootScopeIntegration In global mode so that it can be overridden by the user.
// In non-global mode (e.g. ASP.NET Core) the enricher sets it here as a fallback.
if (!_options.IsGlobalModeEnabled)
{
eventLike.User.Id ??= _options.InstallationId;
}
//Apply App startup and Boot time
eventLike.Contexts.App.StartTime ??= ProcessInfo.Instance?.StartupTime;
eventLike.Contexts.Device.BootTime ??= ProcessInfo.Instance?.BootTime;
eventLike.Contexts.App.InForeground = ProcessInfo.Instance?.ApplicationIsActivated(_options);
// Default tags
_options.ApplyDefaultTags(eventLike);
}
public void Apply(SentryCheckIn checkIn)
{
checkIn.Release ??= _options.SettingLocator.GetRelease();
checkIn.Environment ??= _options.SettingLocator.GetEnvironment();
}
}