-
Notifications
You must be signed in to change notification settings - Fork 286
/
ExceptionTelemetry.cs
502 lines (429 loc) · 18.8 KB
/
ExceptionTelemetry.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
namespace Microsoft.ApplicationInsights.DataContracts
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.ApplicationInsights.Extensibility.Implementation.External;
using Microsoft.ApplicationInsights.Extensibility.Implementation.Metrics;
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing;
/// <summary>
/// Telemetry type used to track exceptions. This will capture TypeName, Message, and CallStack.
/// <a href="https://go.microsoft.com/fwlink/?linkid=723596">Learn more</a>
/// </summary>
/// <remarks>
/// Additional exception details will need to be tracked manually.
/// </remarks>
public sealed class ExceptionTelemetry : ITelemetry, ISupportProperties, ISupportAdvancedSampling, ISupportMetrics, IAiSerializableTelemetry
{
internal const string EtwEnvelopeName = "Exception";
internal string EnvelopeName = "AppExceptions";
internal ExceptionInfo Data = null;
private readonly bool isCreatedFromExceptionInfo = false;
private TelemetryContext context;
private IExtension extension;
private Exception exception;
private string message;
private double? samplingPercentage;
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionTelemetry"/> class with empty properties.
/// </summary>
public ExceptionTelemetry()
{
this.Data = new ExceptionInfo(new ExceptionData());
this.context = new TelemetryContext(this.Data.Properties);
}
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionTelemetry"/> class with empty properties.
/// </summary>
/// <param name="exception">Exception instance.</param>
public ExceptionTelemetry(Exception exception)
: this()
{
if (exception == null)
{
exception = new Exception(Utils.PopulateRequiredStringValue(null, "message", typeof(ExceptionTelemetry).FullName));
}
this.Exception = exception;
}
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionTelemetry"/> class.
/// </summary>
/// <param name="exceptionDetailsInfoList">Exception info.</param>
/// <param name="severityLevel">Severity level.</param>
/// <param name="problemId">Problem id.</param>
/// <param name="properties">Properties.</param>
/// <param name="measurements">Measurements.</param>
public ExceptionTelemetry(IEnumerable<ExceptionDetailsInfo> exceptionDetailsInfoList, SeverityLevel? severityLevel, string problemId,
IDictionary<string, string> properties, IDictionary<string, double> measurements)
{
this.isCreatedFromExceptionInfo = true;
ExceptionInfo exceptionInfo = new ExceptionInfo(exceptionDetailsInfoList, severityLevel, problemId, properties, measurements);
this.Data = exceptionInfo;
this.context = new TelemetryContext(this.Data.Properties);
this.UpdateData(exceptionInfo);
}
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionTelemetry"/> class by cloning an existing instance.
/// </summary>
/// <param name="source">Source instance of <see cref="ExceptionTelemetry"/> to clone from.</param>
private ExceptionTelemetry(ExceptionTelemetry source)
{
this.isCreatedFromExceptionInfo = source.isCreatedFromExceptionInfo;
this.Data = source.Data.DeepClone();
this.context = source.context.DeepClone(this.Data.Properties);
this.Sequence = source.Sequence;
this.Timestamp = source.Timestamp;
this.samplingPercentage = source.samplingPercentage;
this.ProactiveSamplingDecision = source.ProactiveSamplingDecision;
if (!this.isCreatedFromExceptionInfo)
{
this.exception = source.Exception;
}
this.extension = source.extension?.DeepClone();
}
/// <inheritdoc />
string IAiSerializableTelemetry.TelemetryName
{
get
{
return this.EnvelopeName;
}
set
{
this.EnvelopeName = value;
}
}
/// <inheritdoc />
string IAiSerializableTelemetry.BaseType => nameof(ExceptionData);
/// <summary>
/// Gets or sets date and time when telemetry was recorded.
/// </summary>
public DateTimeOffset Timestamp { get; set; }
/// <summary>
/// Gets or sets the value that defines absolute order of the telemetry item.
/// </summary>
public string Sequence { get; set; }
/// <summary>
/// Gets the context associated with the current telemetry item.
/// </summary>
public TelemetryContext Context
{
get { return this.context; }
}
/// <summary>
/// Gets or sets gets the extension used to extend this telemetry instance using new strong typed object.
/// </summary>
public IExtension Extension
{
get { return this.extension; }
set { this.extension = value; }
}
/// <summary>
/// Gets or sets the problemId.
/// </summary>
public string ProblemId
{
get
{
return this.Data.ProblemId;
}
set
{
this.Data.ProblemId = value;
}
}
/// <summary>
/// Gets or sets the value indicated where the exception was handled.
/// </summary>
[Obsolete("Use custom properties to report exception handling layer")]
public ExceptionHandledAt HandledAt
{
get
{
if (this.Properties.ContainsKey("handledAt") && Enum.TryParse(this.Properties["handledAt"], out ExceptionHandledAt result))
{
return result;
}
else
{
return default(ExceptionHandledAt);
}
}
set
{
this.Properties["handledAt"] = value.ToString();
}
}
/// <summary>
/// Gets or sets the original exception tracked by this <see cref="ITelemetry"/>.
/// </summary>
public Exception Exception
{
get
{
return this.isCreatedFromExceptionInfo
? this.ConstructExceptionFromDetailsInfo(this.Data.ExceptionDetailsInfoList ?? new List<ExceptionDetailsInfo>().AsReadOnly())
: this.exception;
}
set
{
if (this.isCreatedFromExceptionInfo)
{
throw new InvalidOperationException(
"The property is unavailable to be set on an instance created with the ExceptionDetailsInfo-based constructor");
}
this.exception = value;
this.UpdateData(value);
}
}
/// <summary>
/// Gets or sets ExceptionTelemetry message.
/// </summary>
public string Message
{
get
{
const string ExceptionMessageSeparator = " <--- ";
return this.isCreatedFromExceptionInfo
? (this.Data.ExceptionDetailsInfoList != null ? string.Join(ExceptionMessageSeparator, this.Data.ExceptionDetailsInfoList.Select(info => info.Message)) : string.Empty)
: this.message;
}
set
{
if (this.isCreatedFromExceptionInfo)
{
throw new InvalidOperationException(
"The property is unavailable to be set on an instance created with the ExceptionDetailsInfo-based constructor");
}
this.message = value;
if (this.Data.ExceptionDetailsInfoList != null && this.Data.ExceptionDetailsInfoList.Count > 0)
{
this.Data.ExceptionDetailsInfoList[0].Message = value;
}
else
{
this.UpdateData(this.Exception);
}
}
}
/// <summary>
/// Gets a dictionary of application-defined exception metrics.
/// <a href="https://go.microsoft.com/fwlink/?linkid=525722#properties">Learn more</a>
/// </summary>
public IDictionary<string, double> Metrics
{
get { return this.Data.Measurements; }
}
/// <summary>
/// Gets the list of <see cref="ExceptionDetailsInfo"/>. User can modify the contents of individual object, but
/// not the list itself.
/// </summary>
public IReadOnlyList<ExceptionDetailsInfo> ExceptionDetailsInfoList => this.Data.ExceptionDetailsInfoList;
/// <summary>
/// Gets a dictionary of application-defined property names and values providing additional information about this exception.
/// <a href="https://go.microsoft.com/fwlink/?linkid=525722#properties">Learn more</a>
/// </summary>
public IDictionary<string, string> Properties
{
#pragma warning disable CS0618 // Type or member is obsolete
get
{
if (!string.IsNullOrEmpty(this.MetricExtractorInfo) && !this.Context.Properties.ContainsKey(MetricTerms.Extraction.ProcessedByExtractors.Moniker.Key))
{
this.Context.Properties[MetricTerms.Extraction.ProcessedByExtractors.Moniker.Key] = this.MetricExtractorInfo;
}
return this.Context.Properties;
#pragma warning restore CS0618 // Type or member is obsolete
}
}
/// <summary>
/// Gets or sets Exception severity level.
/// </summary>
public SeverityLevel? SeverityLevel
{
get => this.Data.SeverityLevel;
set => this.Data.SeverityLevel = value;
}
/// <summary>
/// Gets or sets data sampling percentage (between 0 and 100).
/// Should be 100/n where n is an integer. <a href="https://go.microsoft.com/fwlink/?linkid=832969">Learn more</a>
/// </summary>
double? ISupportSampling.SamplingPercentage
{
get { return this.samplingPercentage; }
set { this.samplingPercentage = value; }
}
/// <summary>
/// Gets item type for sampling evaluation.
/// </summary>
public SamplingTelemetryItemTypes ItemTypeFlag => SamplingTelemetryItemTypes.Exception;
/// <inheritdoc/>
public SamplingDecision ProactiveSamplingDecision { get; set; }
internal IList<ExceptionDetails> Exceptions
{
get { return this.Data.Data.exceptions; }
}
/// <summary>
/// Gets or sets the MetricExtractorInfo.
/// </summary>
internal string MetricExtractorInfo
{
get;
set;
}
/// <summary>
/// Deeply clones a <see cref="ExceptionTelemetry"/> object.
/// </summary>
/// <returns>A cloned instance.</returns>
public ITelemetry DeepClone()
{
return new ExceptionTelemetry(this);
}
/// <inheritdoc/>
public void SerializeData(ISerializationWriter serializationWriter)
{
if (serializationWriter == null)
{
throw new ArgumentNullException(nameof(serializationWriter));
}
serializationWriter.WriteProperty(this.Data.Data);
}
/// <summary>
/// Set parsedStack from an array of StackFrame objects.
/// </summary>
public void SetParsedStack(System.Diagnostics.StackFrame[] frames)
{
if (this.Exceptions != null && this.Exceptions.Count > 0)
{
if (frames != null && frames.Length > 0)
{
int stackLength = 0;
this.Exceptions[0].parsedStack = new List<Extensibility.Implementation.External.StackFrame>();
this.Exceptions[0].hasFullStack = true;
for (int level = 0; level < frames.Length; level++)
{
var sf = ExceptionConverter.GetStackFrame(frames[level], level);
stackLength += ExceptionConverter.GetStackFrameLength(sf);
if (stackLength > ExceptionConverter.MaxParsedStackLength)
{
this.Exceptions[0].hasFullStack = false;
break;
}
this.Exceptions[0].parsedStack.Add(sf);
}
}
}
}
/// <summary>
/// Sanitizes the properties based on constraints.
/// </summary>
void ITelemetry.Sanitize()
{
// Sanitize on the ExceptionDetails stack information for raw stack and parsed stack is done while creating the object in ExceptionConverter.cs
this.message = this.message.SanitizeMessage();
this.Properties.SanitizeProperties();
this.Metrics.SanitizeMeasurements();
}
private void ConvertExceptionTree(Exception exception, ExceptionDetails parentExceptionDetails, List<ExceptionDetails> exceptions)
{
if (exception == null)
{
exception = new Exception(Utils.PopulateRequiredStringValue(null, "message", typeof(ExceptionTelemetry).FullName));
}
ExceptionDetails exceptionDetails = ExceptionConverter.ConvertToExceptionDetails(exception, parentExceptionDetails);
// For upper level exception see if Message was provided and do not use exceptiom.message in that case
if (parentExceptionDetails == null && !string.IsNullOrWhiteSpace(this.Message))
{
exceptionDetails.message = this.Message;
}
exceptions.Add(exceptionDetails);
if (exception is AggregateException aggregate)
{
foreach (Exception inner in aggregate.InnerExceptions)
{
this.ConvertExceptionTree(inner, exceptionDetails, exceptions);
}
}
else if (exception.InnerException != null)
{
this.ConvertExceptionTree(exception.InnerException, exceptionDetails, exceptions);
}
}
private void UpdateData(Exception exception)
{
if (this.isCreatedFromExceptionInfo)
{
throw new InvalidOperationException("Operation is not supported given the state of the object.");
}
try
{
// collect the set of exceptions detail info from the passed in exception
List<ExceptionDetails> exceptions = new List<ExceptionDetails>();
this.ConvertExceptionTree(exception, null, exceptions);
// trim if we have too many, also add a custom exception to let the user know we're trimmed
if (exceptions.Count > Constants.MaxExceptionCountToSave)
{
// TODO: when we localize these messages, we should consider not using InvariantCulture
// create our "message" exception.
InnerExceptionCountExceededException countExceededException =
new InnerExceptionCountExceededException(
string.Format(
CultureInfo.InvariantCulture,
"The number of inner exceptions was {0} which is larger than {1}, the maximum number allowed during transmission. All but the first {1} have been dropped.",
exceptions.Count,
Constants.MaxExceptionCountToSave));
// remove all but the first N exceptions
exceptions.RemoveRange(Constants.MaxExceptionCountToSave,
exceptions.Count - Constants.MaxExceptionCountToSave);
// we'll add our new exception and parent it to the root exception (first one in the list)
exceptions.Add(ExceptionConverter.ConvertToExceptionDetails(countExceededException, exceptions[0]));
}
this.Data = new ExceptionInfo(exceptions.Select(ex => new ExceptionDetailsInfo(ex)), this.SeverityLevel,
this.ProblemId, this.Properties, this.Metrics);
this.context = new TelemetryContext(this.Data.Properties);
}
catch (Exception ex)
{
CoreEventSource.Log.UpdateDataFailed(ex.ToInvariantString());
}
}
private void UpdateData(ExceptionInfo exceptionInfo)
{
if (!this.isCreatedFromExceptionInfo)
{
throw new InvalidOperationException("Operation is not supported given the state of the object.");
}
this.Data = exceptionInfo ?? throw new ArgumentNullException(nameof(exceptionInfo));
this.context = new TelemetryContext(this.Data.Properties);
}
private Exception ConstructExceptionFromDetailsInfo(IReadOnlyList<ExceptionDetailsInfo> exceptionInfos)
{
if (!this.isCreatedFromExceptionInfo)
{
throw new InvalidOperationException("Operation is not supported given the state of the object.");
}
// construct a fake Exception object based on provided information
if (!exceptionInfos.Any())
{
return new Exception(string.Empty);
}
return new Exception(exceptionInfos[0].Message, this.ConstructInnerException(exceptionInfos, 0));
}
private Exception ConstructInnerException(IReadOnlyList<ExceptionDetailsInfo> exceptionInfos, int parentExceptionIndex)
{
// inner exception is the next one after the parent
int index = parentExceptionIndex + 1;
if (index < exceptionInfos.Count)
{
// inner exception exists
return new Exception(exceptionInfos[index].Message, this.ConstructInnerException(exceptionInfos, index));
}
// inner exception doesn't exist
return null;
}
}
}