-
-
Notifications
You must be signed in to change notification settings - Fork 205
/
SentryOptions.cs
431 lines (383 loc) · 17.2 KB
/
SentryOptions.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Net;
using System.Net.Http;
using Sentry.Extensibility;
using Sentry.Http;
using Sentry.Integrations;
using Sentry.Internal;
using Sentry.Protocol;
#if SYSTEM_WEB
using System.Linq;
using Sentry.Internal.Web;
#endif
using static Sentry.Internal.Constants;
using static Sentry.Protocol.Constants;
namespace Sentry
{
/// <summary>
/// Sentry SDK options
/// </summary>
public class SentryOptions : IScopeOptions
{
private readonly Func<ISentryStackTraceFactory> _sentryStackTraceFactoryAccessor;
internal ISentryStackTraceFactory SentryStackTraceFactory { get; set; }
internal string ClientVersion { get; } = SdkName;
internal int SentryVersion { get; } = ProtocolVersion;
/// <summary>
/// A list of exception processors
/// </summary>
internal ISentryEventExceptionProcessor[] ExceptionProcessors { get; set; } = Array.Empty<ISentryEventExceptionProcessor>();
/// <summary>
/// A list of event processors
/// </summary>
internal ISentryEventProcessor[] EventProcessors { get; set; } = Array.Empty<ISentryEventProcessor>();
/// <summary>
/// A list of providers of <see cref="ISentryEventProcessor"/>
/// </summary>
internal Func<IEnumerable<ISentryEventProcessor>>[] EventProcessorsProviders { get; set; }
/// <summary>
/// A list of providers of <see cref="ISentryEventExceptionProcessor"/>
/// </summary>
internal Func<IEnumerable<ISentryEventExceptionProcessor>>[] ExceptionProcessorsProviders { get; set; }
/// <summary>
/// A list of integrations to be added when the SDK is initialized
/// </summary>
internal ISdkIntegration[] Integrations { get; set; }
internal IBackgroundWorker BackgroundWorker { get; set; }
internal ISentryHttpClientFactory SentryHttpClientFactory { get; set; }
/// <summary>
/// A list of namespaces (or prefixes) considered not part of application code
/// </summary>
/// <remarks>
/// Sentry by default filters the stacktrace to display only application code.
/// A user can optionally click to see all which will include framework and libraries.
/// A <see cref="string.StartsWith(string)"/> is executed
/// </remarks>
/// <example>
/// 'System.', 'Microsoft.'
/// </example>
internal string[] InAppExclude { get; set; }
/// <summary>
/// A list of namespaces (or prefixes) considered part of application code
/// </summary>
/// <remarks>
/// Sentry by default filters the stacktrace to display only application code.
/// A user can optionally click to see all which will include framework and libraries.
/// A <see cref="string.StartsWith(string)"/> is executed
/// </remarks>
/// <example>
/// 'System.CustomNamespace', 'Microsoft.Azure.App'
/// </example>
/// <seealso href="https://docs.sentry.io/error-reporting/configuration/?platform=csharp#in-app-include"/>
internal string[] InAppInclude { get; set; }
/// <summary>
/// Whether to include default Personal Identifiable information
/// </summary>
/// <remarks>
/// By default PII data like Username and Client IP address are not sent to Sentry.
/// When this flag is turned on, default PII data like Cookies, Claims in Web applications
/// and user data read from the request are sent.
/// </remarks>
public bool SendDefaultPii { get; set; }
/// <summary>
/// Whether to report the <see cref="System.Environment.UserName"/> as the User affected in the event.
/// </summary>
/// <remarks>
/// This configuration is only relevant is <see cref="SendDefaultPii"/> is set to true.
/// In environments like server applications this is set to false in order to not report server account names as user names.
/// </remarks>
public bool IsEnvironmentUser { get; set; } = true;
/// <summary>
/// Gets or sets the name of the server running the application.
/// </summary>
/// <remarks>
/// When <see cref="SendDefaultPii"/> is set to <c>true</c>, <see cref="System.Environment.MachineName"/> is
/// automatically set as ServerName. This property can serve as an override.
/// This is relevant only to server applications.
/// </remarks>
public string ServerName { get; set; }
/// <summary>
/// Whether to send the stack trace of a event captured without an exception
/// </summary>
/// <remarks>
/// Append stack trace of the call to the SDK to capture a message or event without Exception
/// </remarks>
public bool AttachStacktrace { get; set; }
/// <summary>
/// Gets or sets the maximum breadcrumbs.
/// </summary>
/// <remarks>
/// When the number of events reach this configuration value,
/// older breadcrumbs start dropping to make room for new ones.
/// </remarks>
/// <value>
/// The maximum breadcrumbs per scope.
/// </value>
public int MaxBreadcrumbs { get; set; } = DefaultMaxBreadcrumbs;
/// <summary>
/// The rate to sample events
/// </summary>
/// <remarks>
/// Can be anything between 0.01 (1%) and 1.0 (99.9%) or null (default), to disable it.
/// </remarks>
/// <example>
/// 0.1 = 10% of events are sent
/// </example>
/// <see href="https://docs.sentry.io/clientdev/features/#event-sampling"/>
private float? _sampleRate;
/// <summary>
/// The optional sample rate.
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
public float? SampleRate
{
get => _sampleRate;
set
{
if (value > 1 || value <= 0)
{
throw new InvalidOperationException($"The value {value} is not valid. Use null to disable or values between 0.01 (inclusive) and 1.0 (exclusive) ");
}
_sampleRate = value;
}
}
/// <summary>
/// The release version of the application.
/// </summary>
/// <example>
/// 721e41770371db95eee98ca2707686226b993eda
/// 14.1.16.32451
/// </example>
/// <remarks>
/// This value will generally be something along the lines of the git SHA for the given project.
/// If not explicitly defined via configuration or environment variable (SENTRY_RELEASE).
/// It will attempt o read it from:
/// <see cref="System.Reflection.AssemblyInformationalVersionAttribute"/>
///
/// Don't rely on discovery if your release is: '1.0.0' or '0.0.0'. Since those are
/// default values for new projects, they are not considered valid by the discovery process.
/// </remarks>
/// <seealso href="https://docs.sentry.io/learn/releases/"/>
public string Release { get; set; }
/// <summary>
/// The environment the application is running
/// </summary>
/// <remarks>
/// This value can also be set via environment variable: SENTRY_ENVIRONMENT
/// In some cases you don't need to set this manually since integrations, when possible, automatically fill this value.
/// For ASP.NET Core which can read from IHostingEnvironment
/// </remarks>
/// <example>
/// Production, Staging
/// </example>
/// <seealso href="https://docs.sentry.io/learn/environments/"/>
public string Environment { get; set; }
/// <summary>
/// The Data Source Name of a given project in Sentry.
/// </summary>
public Dsn Dsn { get; set; }
/// <summary>
/// A callback to invoke before sending an event to Sentry
/// </summary>
/// <remarks>
/// The return of this event will be sent to Sentry. This allows the application
/// a chance to inspect and/or modify the event before it's sent. If the event
/// should not be sent at all, return null from the callback.
/// </remarks>
public Func<SentryEvent, SentryEvent> BeforeSend { get; set; }
/// <summary>
/// A callback invoked when a breadcrumb is about to be stored.
/// </summary>
/// <remarks>
/// Gives a chance to inspect and modify/reject a breadcrumb.
/// </remarks>
public Func<Breadcrumb, Breadcrumb> BeforeBreadcrumb { get; set; }
private int _maxQueueItems = 30;
/// <summary>
/// The maximum number of events to keep while the worker attempts to send them
/// </summary>
public int MaxQueueItems
{
get => _maxQueueItems;
set
{
if (value < 1)
{
throw new ArgumentOutOfRangeException(nameof(value), value, "At least 1 item must be allowed in the queue.");
}
_maxQueueItems = value;
}
}
/// <summary>
/// How long to wait for events to be sent before shutdown
/// </summary>
/// <remarks>
/// In case there are events queued when the SDK is closed, upper bound limit to wait
/// for the worker to send the events to Sentry.
/// </remarks>
/// <example>
/// The SDK is closed while the queue has 1 event queued.
/// The worker takes 50 milliseconds to send an event to Sentry.
/// Even though default settings say 2 seconds, closing the SDK would block for 50ms.
/// </example>
public TimeSpan ShutdownTimeout { get; set; } = TimeSpan.FromSeconds(2);
/// <summary>
/// Decompression methods accepted
/// </summary>
/// <remarks>
/// By default accepts all available compression methods supported by the platform
/// </remarks>
public DecompressionMethods DecompressionMethods { get; set; }
// Note the ~ enabling all bits
= ~DecompressionMethods.None;
/// <summary>
/// The level of which to compress the <see cref="SentryEvent"/> before sending to Sentry
/// </summary>
/// <remarks>
/// To disable request body compression, use <see cref="CompressionLevel.NoCompression"/>
/// </remarks>
public CompressionLevel RequestBodyCompressionLevel { get; set; } = CompressionLevel.Optimal;
/// <summary>
/// Whether the body compression is buffered and the request 'Content-Length' known in advance.
/// </summary>
/// <remarks>
/// Without reading through the Gzip stream to have its final size, it's no possible to use 'Content-Length'
/// header value. That means 'Content-Encoding: chunked' has to be used which is sometimes not supported.
/// Sentry on-premise without a reverse-proxy, for example, does not support 'chunked' requests.
/// </remarks>
/// <see href="https://github.com/getsentry/sentry-dotnet/issues/71"/>
public bool RequestBodyCompressionBuffered { get; set; } = true;
/// <summary>
/// An optional web proxy
/// </summary>
public IWebProxy HttpProxy { get; set; }
/// <summary>
/// A callback invoked when a <see cref="SentryClient"/> is created.
/// </summary>
/// <remarks>
/// This callback is invoked once configuration has been applied to the inner most <see cref="HttpClientHandler"/>.
/// </remarks>
[Obsolete("Please use '" + nameof(CreateHttpClientHandler) + "' instead. " +
"You can create an instance of '" + nameof(HttpClientHandler) + "' and modify it at once.")]
public Action<HttpClientHandler, Dsn> ConfigureHandler { get; set; }
/// <summary>
/// Creates the inner most <see cref="HttpClientHandler"/>.
/// </summary>
public Func<Dsn, HttpClientHandler> CreateHttpClientHandler { get; set; }
/// <summary>
/// A callback invoked when a <see cref="SentryClient"/> is created.
/// </summary>
public Action<HttpClient, Dsn> ConfigureClient { get; set; }
private volatile bool _debug;
/// <summary>
/// Whether to log diagnostics messages
/// </summary>
/// <remarks>
/// The verbosity can be controlled through <see cref="DiagnosticsLevel"/>
/// and the implementation via <see cref="DiagnosticLogger"/>.
/// </remarks>
public bool Debug
{
get => _debug;
set => _debug = value;
}
/// <summary>
/// The diagnostics level to be used
/// </summary>
/// <remarks>
/// The <see cref="Debug"/> flag has to be switched on for this setting to take effect.
/// </remarks>
public SentryLevel DiagnosticsLevel { get; set; } = SentryLevel.Debug;
private volatile IDiagnosticLogger _diagnosticLogger;
/// <summary>
/// The implementation of the logger.
/// </summary>
/// <remarks>
/// The <see cref="Debug"/> flag has to be switched on for this logger to be used at all.
/// When debugging is turned off, this property is made null and any internal logging results in a no-op.
/// </remarks>
public IDiagnosticLogger DiagnosticLogger
{
get => Debug ? _diagnosticLogger : null;
set
{
_diagnosticLogger?.LogInfo("Replacing current logger with: '{0}'.", value?.GetType().Name);
_diagnosticLogger = value;
}
}
/// <summary>
/// Whether or not to include referenced assemblies in each event sent to sentry. Defaults to <see langword="true"/>.
/// </summary>
public bool ReportAssemblies { get; set; } = true;
/// <summary>
/// What modes to use for event automatic deduplication
/// </summary>
/// <remarks>
/// By default will not drop an event solely for including an inner exception that was already captured.
/// </remarks>
public DeduplicateMode DeduplicateMode { get; set; } = DeduplicateMode.All ^ DeduplicateMode.InnerException;
#if SYSTEM_WEB
/// <summary>
/// Max request body to be captured when a Web request exists on a ASP.NET Application.
/// </summary>
/// <remarks>
/// This configuration is visible to any .NET Framework application but is only relevant when running ASP.NET.
/// When set to a value other than 'None', the body from `HttpContext.Current.Request` is read when available.
/// </remarks>
public RequestSize MaxRequestBodySize { get; set; }
#endif
/// <summary>
/// Creates a new instance of <see cref="SentryOptions"/>
/// </summary>
public SentryOptions()
{
EventProcessorsProviders = new Func<IEnumerable<ISentryEventProcessor>>[] {
() => EventProcessors
};
ExceptionProcessorsProviders = new Func<IEnumerable<ISentryEventExceptionProcessor>>[] {
() => ExceptionProcessors
};
SentryStackTraceFactory = new SentryStackTraceFactory(this);
_sentryStackTraceFactoryAccessor = () => SentryStackTraceFactory;
EventProcessors = new ISentryEventProcessor[] {
// de-dupe to be the first to run
new DuplicateEventDetectionEventProcessor(this),
new MainSentryEventProcessor(this, _sentryStackTraceFactoryAccessor),
};
#if SYSTEM_WEB
EventProcessors = EventProcessors.Concat(new ISentryEventProcessor[] {
new SystemWebRequestEventProcessor(
new RequestBodyExtractionDispatcher(
new IRequestPayloadExtractor[]
{
new FormRequestPayloadExtractor(),
new DefaultRequestPayloadExtractor()
},
this,
() => MaxRequestBodySize),
this) }).ToArray();
#endif
ExceptionProcessors = new ISentryEventExceptionProcessor[] {
new MainExceptionProcessor(this, _sentryStackTraceFactoryAccessor)
};
Integrations = new ISdkIntegration[] {
new AppDomainUnhandledExceptionIntegration(),
new AppDomainProcessExitIntegration(),
};
InAppExclude = new string[] {
"System.",
"Sentry.",
"Microsoft.",
"MS", // MS.Win32, MS.Internal, etc: Desktop apps
"Newtonsoft.Json",
"FSharp.",
"Serilog",
"Giraffe.",
"NLog",
};
InAppInclude = Array.Empty<string>();
}
}
}