-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
SentrySdk.cs
384 lines (341 loc) · 15 KB
/
SentrySdk.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Sentry.Extensibility;
using Sentry.Infrastructure;
using Sentry.Internal;
using Sentry.Protocol;
namespace Sentry
{
/// <summary>
/// Sentry SDK entrypoint.
/// </summary>
/// <remarks>
/// This is a facade to the SDK instance.
/// It allows safe static access to a client and scope management.
/// When the SDK is uninitialized, calls to this class result in no-op so no callbacks are invoked.
/// </remarks>
public static class SentrySdk
{
private static IHub _hub = DisabledHub.Instance;
/// <summary>
/// Last event id recorded in the current scope.
/// </summary>
public static SentryId LastEventId { [DebuggerStepThrough] get => _hub.LastEventId; }
internal static IHub InitHub(SentryOptions options)
{
// Side-effects in a factory function 🤮
options.SetupLogging();
// If DSN is null (i.e. not explicitly disabled, just unset), then
// try to resolve the value from environment.
var dsn = options.Dsn ??= DsnLocator.FindDsnStringOrDisable();
// If it's either explicitly disabled or we couldn't resolve the DSN
// from anywhere else, return a disabled hub.
if (Dsn.IsDisabled(dsn))
{
options.DiagnosticLogger?.LogWarning(
"Init was called but no DSN was provided nor located. Sentry SDK will be disabled."
);
return DisabledHub.Instance;
}
// Validate DSN for an early exception in case it's malformed
_ = Dsn.Parse(dsn);
return new Hub(options);
}
/// <summary>
/// Initializes the SDK while attempting to locate the DSN.
/// </summary>
/// <remarks>
/// If the DSN is not found, the SDK will not change state.
/// </remarks>
public static IDisposable Init() => Init((string?)null);
/// <summary>
/// Initializes the SDK with the specified DSN.
/// </summary>
/// <remarks>
/// An empty string is interpreted as a disabled SDK.
/// </remarks>
/// <seealso href="https://develop.sentry.dev/sdk/overview/#usage-for-end-users"/>
/// <param name="dsn">The dsn.</param>
public static IDisposable Init(string? dsn) => !Dsn.IsDisabled(dsn)
? Init(c => c.Dsn = dsn)
: DisabledHub.Instance;
/// <summary>
/// Initializes the SDK with an optional configuration options callback.
/// </summary>
/// <param name="configureOptions">The configure options.</param>
public static IDisposable Init(Action<SentryOptions>? configureOptions)
{
var options = new SentryOptions();
configureOptions?.Invoke(options);
return Init(options);
}
/// <summary>
/// Initializes the SDK with the specified options instance.
/// </summary>
/// <param name="options">The options instance</param>
/// <remarks>
/// Used by integrations which have their own delegates.
/// </remarks>
/// <returns>A disposable to close the SDK.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public static IDisposable Init(SentryOptions options) => UseHub(InitHub(options));
internal static IDisposable UseHub(IHub hub)
{
var oldHub = Interlocked.Exchange(ref _hub, hub);
(oldHub as IDisposable)?.Dispose();
return new DisposeHandle(hub);
}
/// <summary>
/// Flushes events queued up.
/// </summary>
[DebuggerStepThrough]
public static Task FlushAsync(TimeSpan timeout) => _hub.FlushAsync(timeout);
/// <summary>
/// Close the SDK.
/// </summary>
/// <remarks>
/// Flushes the events and disables the SDK.
/// This method is mostly used for testing the library since
/// Init returns a IDisposable that can be used to shutdown the SDK.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public static void Close()
{
var oldHub = Interlocked.Exchange(ref _hub, DisabledHub.Instance);
(oldHub as IDisposable)?.Dispose();
}
private class DisposeHandle : IDisposable
{
private IHub _localHub;
public DisposeHandle(IHub hub) => _localHub = hub;
public void Dispose()
{
_ = Interlocked.CompareExchange(ref _hub, DisabledHub.Instance, _localHub);
(_localHub as IDisposable)?.Dispose();
_localHub = null!;
}
}
/// <summary>
/// Whether the SDK is enabled or not.
/// </summary>
public static bool IsEnabled { [DebuggerStepThrough] get => _hub.IsEnabled; }
/// <summary>
/// Creates a new scope that will terminate when disposed.
/// </summary>
/// <remarks>
/// Pushes a new scope while inheriting the current scope's data.
/// </remarks>
/// <param name="state">A state object to be added to the scope.</param>
/// <returns>A disposable that when disposed, ends the created scope.</returns>
[DebuggerStepThrough]
public static IDisposable PushScope<TState>(TState state) => _hub.PushScope(state);
/// <summary>
/// Creates a new scope that will terminate when disposed.
/// </summary>
/// <returns>A disposable that when disposed, ends the created scope.</returns>
[DebuggerStepThrough]
public static IDisposable PushScope() => _hub.PushScope();
/// <summary>
/// Binds the client to the current scope.
/// </summary>
/// <param name="client">The client.</param>
[DebuggerStepThrough]
public static void BindClient(ISentryClient client) => _hub.BindClient(client);
/// <summary>
/// Adds a breadcrumb to the current Scope.
/// </summary>
/// <param name="message">
/// If a message is provided it’s rendered as text and the whitespace is preserved.
/// Very long text might be abbreviated in the UI.</param>
/// <param name="category">
/// Categories are dotted strings that indicate what the crumb is or where it comes from.
/// Typically it’s a module name or a descriptive string.
/// For instance ui.click could be used to indicate that a click happened in the UI or flask could be used to indicate that the event originated in the Flask framework.
/// </param>
/// <param name="type">
/// The type of breadcrumb.
/// The default type is default which indicates no specific handling.
/// Other types are currently http for HTTP requests and navigation for navigation events.
/// <seealso href="https://develop.sentry.dev/sdk/event-payloads/breadcrumbs/#breadcrumb-types"/>
/// </param>
/// <param name="data">
/// Data associated with this breadcrumb.
/// Contains a sub-object whose contents depend on the breadcrumb type.
/// Additional parameters that are unsupported by the type are rendered as a key/value table.
/// </param>
/// <param name="level">Breadcrumb level.</param>
/// <seealso href="https://develop.sentry.dev/sdk/event-payloads/breadcrumbs/"/>
[DebuggerStepThrough]
public static void AddBreadcrumb(
string message,
string? category = null,
string? type = null,
IDictionary<string, string>? data = null,
BreadcrumbLevel level = default)
=> _hub.AddBreadcrumb(message, category, type, data, level);
/// <summary>
/// Adds a breadcrumb to the current scope.
/// </summary>
/// <remarks>
/// This overload is intended to be used by integrations only.
/// The objective is to allow better testability by allowing control of the timestamp set to the breadcrumb.
/// </remarks>
/// <param name="clock">An optional <see cref="ISystemClock"/>.</param>
/// <param name="message">The message.</param>
/// <param name="category">The category.</param>
/// <param name="type">The type.</param>
/// <param name="data">The data.</param>
/// <param name="level">The level.</param>
[DebuggerStepThrough]
[EditorBrowsable(EditorBrowsableState.Never)]
public static void AddBreadcrumb(
ISystemClock? clock,
string message,
string? category = null,
string? type = null,
IDictionary<string, string>? data = null,
BreadcrumbLevel level = default)
=> _hub.AddBreadcrumb(clock, message, category, type, data, level);
/// <summary>
/// Runs the callback with a new scope which gets dropped at the end.
/// </summary>
/// <remarks>
/// Pushes a new scope, runs the callback, pops the scope.
/// </remarks>
/// <see href="https://docs.sentry.io/platforms/dotnet/enriching-events/scopes/#local-scopes"/>
/// <param name="scopeCallback">The callback to run with the one time scope.</param>
[DebuggerStepThrough]
public static void WithScope(Action<Scope> scopeCallback)
=> _hub.WithScope(scopeCallback);
/// <summary>
/// Configures the scope through the callback.
/// </summary>
/// <param name="configureScope">The configure scope callback.</param>
[DebuggerStepThrough]
public static void ConfigureScope(Action<Scope> configureScope)
=> _hub.ConfigureScope(configureScope);
/// <summary>
/// Configures the scope asynchronously.
/// </summary>
/// <param name="configureScope">The configure scope callback.</param>
/// <returns>The Id of the event.</returns>
[DebuggerStepThrough]
public static Task ConfigureScopeAsync(Func<Scope, Task> configureScope)
=> _hub.ConfigureScopeAsync(configureScope);
/// <summary>
/// Captures the event.
/// </summary>
/// <param name="evt">The event.</param>
/// <returns>The Id of the event.</returns>
[DebuggerStepThrough]
public static SentryId CaptureEvent(SentryEvent evt)
=> _hub.CaptureEvent(evt);
/// <summary>
/// Captures the event using the specified scope.
/// </summary>
/// <param name="evt">The event.</param>
/// <param name="scope">The scope.</param>
/// <returns>The Id of the event.</returns>
[DebuggerStepThrough]
[EditorBrowsable(EditorBrowsableState.Never)]
public static SentryId CaptureEvent(SentryEvent evt, Scope? scope)
=> _hub.CaptureEvent(evt, scope);
/// <summary>
/// Captures the exception.
/// </summary>
/// <param name="exception">The exception.</param>
/// <returns>The Id of the even.t</returns>
[DebuggerStepThrough]
public static SentryId CaptureException(Exception exception)
=> _hub.CaptureException(exception);
/// <summary>
/// Captures the message.
/// </summary>
/// <param name="message">The message to send.</param>
/// <param name="level">The message level.</param>
/// <returns>The Id of the event.</returns>
[DebuggerStepThrough]
public static SentryId CaptureMessage(string message, SentryLevel level = SentryLevel.Info)
=> _hub.CaptureMessage(message, level);
/// <summary>
/// Captures a user feedback.
/// </summary>
/// <param name="userFeedback">The user feedback to send to Sentry.</param>
[DebuggerStepThrough]
public static void CaptureUserFeedback(UserFeedback userFeedback)
=> _hub.CaptureUserFeedback(userFeedback);
/// <summary>
/// Captures a user feedback.
/// </summary>
/// <param name="eventId">The event Id.</param>
/// <param name="email">The user email.</param>
/// <param name="comments">The user comments.</param>
/// <param name="name">The optional username.</param>
[DebuggerStepThrough]
public static void CaptureUserFeedback(SentryId eventId, string email, string comments, string? name = null)
=> _hub.CaptureUserFeedback(new UserFeedback(eventId, name, email, comments));
/// <summary>
/// Captures a transaction.
/// </summary>
[DebuggerStepThrough]
public static void CaptureTransaction(ITransaction transaction)
=> _hub.CaptureTransaction(transaction);
/// <summary>
/// Starts a transaction.
/// </summary>
[DebuggerStepThrough]
public static ITransaction StartTransaction(
ITransactionContext context,
IReadOnlyDictionary<string, object?> customSamplingContext)
=> _hub.StartTransaction(context, customSamplingContext);
/// <summary>
/// Starts a transaction.
/// </summary>
[DebuggerStepThrough]
public static ITransaction StartTransaction(ITransactionContext context)
=> _hub.StartTransaction(context);
/// <summary>
/// Starts a transaction.
/// </summary>
[DebuggerStepThrough]
public static ITransaction StartTransaction(string name, string operation)
=> _hub.StartTransaction(name, operation);
/// <summary>
/// Starts a transaction.
/// </summary>
[DebuggerStepThrough]
public static ITransaction StartTransaction(string name, string operation, string description)
=> _hub.StartTransaction(name, operation, description);
/// <summary>
/// Starts a transaction.
/// </summary>
[DebuggerStepThrough]
public static ITransaction StartTransaction(string name, string operation, SentryTraceHeader traceHeader)
=> _hub.StartTransaction(name, operation, traceHeader);
/// <summary>
/// Binds specified exception the specified span.
/// </summary>
/// <remarks>
/// This method is used internally and is not meant for public use.
/// </remarks>
[DebuggerStepThrough]
public static void BindException(Exception exception, ISpan span)
=> _hub.BindException(exception, span);
/// <summary>
/// Gets the last active span.
/// </summary>
[DebuggerStepThrough]
public static ISpan? GetSpan()
=> _hub.GetSpan();
/// <summary>
/// Gets the Sentry trace header.
/// </summary>
[DebuggerStepThrough]
public static SentryTraceHeader? GetTraceHeader()
=> _hub.GetTraceHeader();
}
}