This repository has been archived by the owner on Jul 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
OperationCorrelationTelemetryInitializer.cs
91 lines (82 loc) · 3.53 KB
/
OperationCorrelationTelemetryInitializer.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
namespace Microsoft.ApplicationInsights.Web
{
using System.Web;
using Common;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.ApplicationInsights.Web.Implementation;
/// <summary>
/// A telemetry initializer that will set the correlation context for all telemetry items in web application.
/// </summary>
public class OperationCorrelationTelemetryInitializer : WebTelemetryInitializerBase
{
/// <summary>
/// Initializes a new instance of the <see cref="OperationCorrelationTelemetryInitializer"/> class.
/// </summary>
public OperationCorrelationTelemetryInitializer()
{
this.ParentOperationIdHeaderName = RequestResponseHeaders.StandardParentIdHeader;
this.RootOperationIdHeaderName = RequestResponseHeaders.StandardRootIdHeader;
}
/// <summary>
/// Gets or sets the name of the header to get parent operation Id from.
/// </summary>
public string ParentOperationIdHeaderName { get; set; }
/// <summary>
/// Gets or sets the name of the header to get root operation Id from.
/// </summary>
public string RootOperationIdHeaderName { get; set; }
/// <summary>
/// Implements initialization logic.
/// </summary>
/// <param name="platformContext">Http context.</param>
/// <param name="requestTelemetry">Request telemetry object associated with the current request.</param>
/// <param name="telemetry">Telemetry item to initialize.</param>
protected override void OnInitializeTelemetry(
HttpContext platformContext,
RequestTelemetry requestTelemetry,
ITelemetry telemetry)
{
OperationContext parentContext = requestTelemetry.Context.Operation;
// Make sure that RequestTelemetry is initialized.
if (string.IsNullOrEmpty(parentContext.ParentId))
{
if (!string.IsNullOrWhiteSpace(this.ParentOperationIdHeaderName))
{
var parentId = platformContext.Request.UnvalidatedGetHeader(this.ParentOperationIdHeaderName);
if (!string.IsNullOrEmpty(parentId))
{
parentContext.ParentId = parentId;
}
}
}
if (string.IsNullOrEmpty(parentContext.Id))
{
if (!string.IsNullOrWhiteSpace(this.RootOperationIdHeaderName))
{
var rootId = platformContext.Request.UnvalidatedGetHeader(this.RootOperationIdHeaderName);
if (!string.IsNullOrEmpty(rootId))
{
parentContext.Id = rootId;
}
}
if (string.IsNullOrEmpty(parentContext.Id))
{
parentContext.Id = requestTelemetry.Id;
}
}
if (telemetry != requestTelemetry)
{
if (string.IsNullOrEmpty(telemetry.Context.Operation.ParentId))
{
telemetry.Context.Operation.ParentId = requestTelemetry.Id;
}
if (string.IsNullOrEmpty(telemetry.Context.Operation.Id))
{
telemetry.Context.Operation.Id = parentContext.Id;
}
}
}
}
}