-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
SentryEvent.cs
298 lines (252 loc) · 12 KB
/
SentryEvent.cs
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using Sentry.Internal.Extensions;
using Sentry.Protocol;
namespace Sentry
{
/// <summary>
/// An event to be sent to Sentry.
/// </summary>
/// <seealso href="https://develop.sentry.dev/sdk/event-payloads/" />
[DebuggerDisplay("{GetType().Name,nq}: {" + nameof(EventId) + ",nq}")]
public sealed class SentryEvent : IEventLike, IJsonSerializable
{
private IDictionary<string, string>? _modules;
/// <summary>
/// The <see cref="System.Exception"/> used to create this event.
/// </summary>
/// <remarks>
/// The information from this exception is used by the Sentry SDK
/// to add the relevant data to the event prior to sending to Sentry.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public Exception? Exception { get; }
/// <summary>
/// The unique identifier of this event.
/// </summary>
/// <remarks>
/// Hexadecimal string representing a uuid4 value.
/// The length is exactly 32 characters (no dashes!).
/// </remarks>
public SentryId EventId { get; }
/// <summary>
/// Indicates when the event was created.
/// </summary>
/// <example>2018-04-03T17:41:36</example>
public DateTimeOffset Timestamp { get; }
/// <summary>
/// Gets the structured message that describes this event.
/// </summary>
/// <remarks>
/// This helps Sentry group events together as the grouping happens
/// on the template message instead of the result string message.
/// </remarks>
/// <example>
/// SentryMessage will have a template like: 'user {0} logged in'
/// Or structured logging template: '{user} has logged in'
/// </example>
public SentryMessage? Message { get; set; }
/// <summary>
/// Name of the logger (or source) of the event.
/// </summary>
public string? Logger { get; set; }
/// <inheritdoc />
public string? Platform { get; set; }
/// <summary>
/// Identifies the host SDK from which the event was recorded.
/// </summary>
public string? ServerName { get; set; }
/// <inheritdoc />
public string? Release { get; set; }
internal SentryValues<SentryException>? SentryExceptionValues { get; set; }
/// <summary>
/// The Sentry Exception interface.
/// </summary>
public IEnumerable<SentryException>? SentryExceptions
{
get => SentryExceptionValues?.Values ?? Enumerable.Empty<SentryException>();
set => SentryExceptionValues = value != null ? new SentryValues<SentryException>(value) : null;
}
private SentryValues<SentryThread>? SentryThreadValues { get; set; }
/// <summary>
/// The Sentry Thread interface.
/// </summary>
/// <see href="https://develop.sentry.dev/sdk/event-payloads/threads/"/>
public IEnumerable<SentryThread>? SentryThreads
{
get => SentryThreadValues?.Values ?? Enumerable.Empty<SentryThread>();
set => SentryThreadValues = value != null ? new SentryValues<SentryThread>(value) : null;
}
/// <summary>
/// A list of relevant modules and their versions.
/// </summary>
public IDictionary<string, string> Modules => _modules ??= new Dictionary<string, string>();
/// <inheritdoc />
public SentryLevel? Level { get; set; }
/// <inheritdoc />
public string? TransactionName { get; set; }
private Request? _request;
/// <inheritdoc />
public Request Request
{
get => _request ??= new Request();
set => _request = value;
}
private Contexts? _contexts;
/// <inheritdoc />
public Contexts Contexts
{
get => _contexts ??= new Contexts();
set => _contexts = value;
}
private User? _user;
/// <inheritdoc />
public User User
{
get => _user ??= new User();
set => _user = value;
}
/// <inheritdoc />
public string? Environment { get; set; }
/// <inheritdoc />
public SdkVersion Sdk { get; internal set; } = new();
private IReadOnlyList<string>? _fingerprint;
/// <inheritdoc />
public IReadOnlyList<string> Fingerprint
{
get => _fingerprint ?? Array.Empty<string>();
set => _fingerprint = value;
}
// Default values are null so no serialization of empty objects or arrays
private List<Breadcrumb>? _breadcrumbs;
/// <inheritdoc />
public IReadOnlyCollection<Breadcrumb> Breadcrumbs => _breadcrumbs ??= new List<Breadcrumb>();
private Dictionary<string, object?>? _extra;
/// <inheritdoc />
public IReadOnlyDictionary<string, object?> Extra => _extra ??= new Dictionary<string, object?>();
private Dictionary<string, string>? _tags;
/// <inheritdoc />
public IReadOnlyDictionary<string, string> Tags => _tags ??= new Dictionary<string, string>();
internal bool HasException => Exception is not null || SentryExceptions?.Any() == true;
internal bool HasUnhandledException => (SentryExceptions?.Any(e => !(e.Mechanism?.Handled ?? true)) ?? false)
// Before event is processed by the client and SentryExceptions created.
// See: AppDomainUnhandledExceptionIntegration
|| Exception?.Data[Mechanism.HandledKey] is false;
/// <summary>
/// Creates a new instance of <see cref="T:Sentry.SentryEvent" />.
/// </summary>
public SentryEvent() : this(null)
{
}
/// <summary>
/// Creates a Sentry event with optional Exception details and default values like Id and Timestamp.
/// </summary>
/// <param name="exception">The exception.</param>
public SentryEvent(Exception? exception)
: this(exception, null)
{
}
internal SentryEvent(
Exception? exception = null,
DateTimeOffset? timestamp = null,
SentryId eventId = default)
{
Exception = exception;
Timestamp = timestamp ?? DateTimeOffset.UtcNow;
EventId = eventId != default ? eventId : SentryId.Create();
Platform = Constants.Platform;
}
/// <inheritdoc />
public void AddBreadcrumb(Breadcrumb breadcrumb) =>
(_breadcrumbs ??= new List<Breadcrumb>()).Add(breadcrumb);
/// <inheritdoc />
public void SetExtra(string key, object? value) =>
(_extra ??= new Dictionary<string, object?>())[key] = value;
/// <inheritdoc />
public void SetTag(string key, string value) =>
(_tags ??= new Dictionary<string, string>())[key] = value;
/// <inheritdoc />
public void UnsetTag(string key) =>
(_tags ??= new Dictionary<string, string>()).Remove(key);
/// <inheritdoc />
public void WriteTo(Utf8JsonWriter writer)
{
writer.WriteStartObject();
writer.WriteStringDictionaryIfNotEmpty("modules", _modules!);
writer.WriteSerializable("event_id", EventId);
writer.WriteString("timestamp", Timestamp);
writer.WriteSerializableIfNotNull("logentry", Message);
writer.WriteStringIfNotWhiteSpace("logger", Logger);
writer.WriteStringIfNotWhiteSpace("platform", Platform);
writer.WriteStringIfNotWhiteSpace("server_name", ServerName);
writer.WriteStringIfNotWhiteSpace("release", Release);
writer.WriteSerializableIfNotNull("exception", SentryExceptionValues);
writer.WriteSerializableIfNotNull("threads", SentryThreadValues);
writer.WriteStringIfNotWhiteSpace("level", Level?.ToString().ToLowerInvariant());
writer.WriteStringIfNotWhiteSpace("transaction", TransactionName);
writer.WriteSerializableIfNotNull("request", _request);
writer.WriteSerializableIfNotNull("contexts", _contexts);
writer.WriteSerializableIfNotNull("user", _user);
writer.WriteStringIfNotWhiteSpace("environment", Environment);
writer.WriteSerializable("sdk", Sdk);
writer.WriteStringArrayIfNotEmpty("fingerprint", _fingerprint);
writer.WriteArrayIfNotEmpty("breadcrumbs", _breadcrumbs);
writer.WriteDictionaryIfNotEmpty("extra", _extra);
writer.WriteStringDictionaryIfNotEmpty("tags", _tags!);
writer.WriteEndObject();
}
/// <summary>
/// Parses from JSON.
/// </summary>
public static SentryEvent FromJson(JsonElement json)
{
var modules = json.GetPropertyOrNull("modules")?.GetStringDictionaryOrNull();
var eventId = json.GetPropertyOrNull("event_id")?.Pipe(SentryId.FromJson) ?? SentryId.Empty;
var timestamp = json.GetPropertyOrNull("timestamp")?.GetDateTimeOffset();
var message = json.GetPropertyOrNull("logentry")?.Pipe(SentryMessage.FromJson);
var logger = json.GetPropertyOrNull("logger")?.GetString();
var platform = json.GetPropertyOrNull("platform")?.GetString();
var serverName = json.GetPropertyOrNull("server_name")?.GetString();
var release = json.GetPropertyOrNull("release")?.GetString();
var exceptionValues = json.GetPropertyOrNull("exception")?.GetPropertyOrNull("values")?.EnumerateArray().Select(SentryException.FromJson).Pipe(v => new SentryValues<SentryException>(v));
var threadValues = json.GetPropertyOrNull("threads")?.GetPropertyOrNull("values")?.EnumerateArray().Select(SentryThread.FromJson).Pipe(v => new SentryValues<SentryThread>(v));
var level = json.GetPropertyOrNull("level")?.GetString()?.ParseEnum<SentryLevel>();
var transaction = json.GetPropertyOrNull("transaction")?.GetString();
var request = json.GetPropertyOrNull("request")?.Pipe(Request.FromJson);
var contexts = json.GetPropertyOrNull("contexts")?.Pipe(Contexts.FromJson);
var user = json.GetPropertyOrNull("user")?.Pipe(User.FromJson);
var environment = json.GetPropertyOrNull("environment")?.GetString();
var sdk = json.GetPropertyOrNull("sdk")?.Pipe(SdkVersion.FromJson) ?? new SdkVersion();
var fingerprint = json.GetPropertyOrNull("fingerprint")?.EnumerateArray().Select(j => j.GetString()).ToArray();
var breadcrumbs = json.GetPropertyOrNull("breadcrumbs")?.EnumerateArray().Select(Breadcrumb.FromJson).ToList();
var extra = json.GetPropertyOrNull("extra")?.GetDictionaryOrNull();
var tags = json.GetPropertyOrNull("tags")?.GetStringDictionaryOrNull();
return new SentryEvent(null, timestamp, eventId)
{
_modules = modules?.WhereNotNullValue().ToDictionary(),
Message = message,
Logger = logger,
Platform = platform,
ServerName = serverName,
Release = release,
SentryExceptionValues = exceptionValues,
SentryThreadValues = threadValues,
Level = level,
TransactionName = transaction,
_request = request,
_contexts = contexts,
_user = user,
Environment = environment,
Sdk = sdk,
_fingerprint = fingerprint!,
_breadcrumbs = breadcrumbs,
_extra = extra?.ToDictionary(),
_tags = tags?.WhereNotNullValue().ToDictionary()
};
}
}
}