/
DependencyTelemetry.cs
193 lines (172 loc) · 6.64 KB
/
DependencyTelemetry.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
namespace Microsoft.ApplicationInsights.DataContracts
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.ApplicationInsights.Extensibility.Implementation.External;
using BondDependencyKind = Extensibility.Implementation.External.DependencyKind;
/// <summary>
/// The class that represents information about the collected dependency.
/// </summary>
[DebuggerDisplay(@"Value={Value}; Name={Name}; Count={Count}; Success={Success}; Async={Async}; Timestamp={Timestamp}")]
public sealed class DependencyTelemetry : ITelemetry, ISupportProperties
{
internal const string TelemetryName = "RemoteDependency";
internal readonly string BaseType = typeof(RemoteDependencyData).Name;
internal readonly RemoteDependencyData Data;
private readonly TelemetryContext context;
/// <summary>
/// Initializes a new instance of the <see cref="DependencyTelemetry"/> class.
/// </summary>
public DependencyTelemetry()
{
this.Data = new RemoteDependencyData() { kind = DataPointType.Aggregation };
this.context = new TelemetryContext(this.Data.properties, new Dictionary<string, string>());
this.Data.dependencyKind = BondDependencyKind.Other;
}
/// <summary>
/// Initializes a new instance of the <see cref="DependencyTelemetry"/> class with the given <paramref name="dependencyName"/>, <paramref name="commandName"/>,
/// <paramref name="startTime"/>, <paramref name="duration"/> and <paramref name="success"/> property values.
/// </summary>
public DependencyTelemetry(string dependencyName, string commandName, DateTimeOffset startTime, TimeSpan duration, bool success)
: this()
{
this.Name = dependencyName;
this.CommandName = commandName;
this.Duration = duration;
this.Success = success;
this.StartTime = startTime;
}
/// <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 resource name.
/// </summary>
public string Name
{
get { return this.Data.name; }
set { this.Data.name = value; }
}
/// <summary>
/// Gets or sets text of SQL command or empty it not applicable.
/// </summary>
public string CommandName
{
get { return this.Data.commandName; }
set { this.Data.commandName = value; }
}
/// <summary>
/// Gets or sets the dependency type name.
/// </summary>
public string DependencyTypeName
{
get { return this.Data.dependencyTypeName; }
set { this.Data.dependencyTypeName = value; }
}
/// <summary>
/// Gets or sets the date and time when dependency was called by the application.
/// </summary>
public DateTimeOffset StartTime
{
get { return this.Timestamp; }
set { this.Timestamp = value; }
}
/// <summary>
/// Gets or sets dependency call duration.
/// </summary>
public TimeSpan Duration
{
get { return TimeSpan.FromMilliseconds(this.Data.value); }
set { this.Data.value = value.TotalMilliseconds; }
}
/// <summary>
/// Gets or sets request count.
/// </summary>
public int? Count
{
get { return this.Data.count; }
set { this.Data.count = value; }
}
/// <summary>
/// Gets or sets a value indicating whether the dependency call was successful or not.
/// </summary>
public bool? Success
{
get { return this.Data.success; }
set { this.Data.success = value; }
}
/// <summary>
/// Gets or sets a value indicating whether the dependency call was made asynchronously or not.
/// </summary>
public bool? Async
{
get { return this.Data.async; }
set { this.Data.async = value; }
}
/// <summary>
/// Gets a dictionary of application-defined property names and values providing additional information about this remote dependency.
/// </summary>
public IDictionary<string, string> Properties
{
get { return this.Data.properties; }
}
/// <summary>
/// Gets or sets the dependency kind, like SQL, HTTP, Azure, etc.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public string DependencyKind
{
get
{
return this.Data.dependencyKind.ToString();
}
set
{
if (string.IsNullOrEmpty(value))
{
this.Data.dependencyKind = BondDependencyKind.Other;
}
else
{
// There is no TryParse in .Net 3.5
if (Enum.GetNames(typeof(BondDependencyKind)).Contains(value))
{
this.Data.dependencyKind = (BondDependencyKind)Enum.Parse(typeof(BondDependencyKind), value);
}
else
{
this.Data.dependencyKind = BondDependencyKind.Other;
}
}
}
}
/// <summary>
/// Sanitizes the properties based on constraints.
/// </summary>
void ITelemetry.Sanitize()
{
this.Name = this.Name.SanitizeName();
this.Name = Utils.PopulateRequiredStringValue(this.Name, "name", typeof(EventTelemetry).FullName);
this.DependencyTypeName = this.DependencyTypeName.SanitizeValue();
this.CommandName = this.CommandName.SanitizeCommandName();
this.Properties.SanitizeProperties();
}
}
}