-
Notifications
You must be signed in to change notification settings - Fork 235
/
TelemetryContext.ts
361 lines (316 loc) · 15.7 KB
/
TelemetryContext.ts
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
/// <reference path="Sender.ts"/>
/// <reference path="Telemetry/Trace.ts" />
/// <reference path="Telemetry/Event.ts" />
/// <reference path="Telemetry/Exception.ts" />
/// <reference path="Telemetry/Metric.ts" />
/// <reference path="Telemetry/PageView.ts" />
/// <reference path="Telemetry/PageViewPerformance.ts" />
/// <reference path="./Util.ts"/>
/// <reference path="../JavaScriptSDK.Interfaces/ITelemetryContext.ts" />
module Microsoft.ApplicationInsights {
"use strict";
export interface ITelemetryConfig extends ISenderConfig {
instrumentationKey: () => string;
accountId: () => string;
sessionRenewalMs: () => number;
sessionExpirationMs: () => number;
sampleRate: () => number;
cookieDomain: () => string;
sdkExtension: () => string;
isBrowserLinkTrackingEnabled: () => boolean;
appId: () => string;
}
export class TelemetryContext implements ITelemetryContext {
/**
* The configuration for this telemetry context
*/
public _config: ITelemetryConfig;
/**
* The sender instance for this context
*/
public _sender: Sender;
/**
* The object describing a component tracked by this object.
*/
public application: Context.Application;
/**
* The object describing a device tracked by this object.
*/
public device: Context.Device;
public internal: Context.Internal;
/**
* The object describing a location tracked by this object.
*/
public location: Context.Location;
/**
* The object describing a operation tracked by this object.
*/
public operation: Context.Operation;
public sample: Context.Sample;
/**
* The object describing a user tracked by this object.
*/
public user: Context.User;
/**
* The object describing a session tracked by this object.
*/
public session: Context.Session;
/**
* AppId of this component if returned by the backend.
*/
public appId: () => string;
/**
* The array of telemetry initializers to call before sending each telemetry item.
*/
private telemetryInitializers: { (envelope: Microsoft.ApplicationInsights.IEnvelope): boolean | void; }[];
/**
* The session manager that manages session on the base of cookies.
*/
public _sessionManager: Microsoft.ApplicationInsights.Context._SessionManager;
constructor(config: ITelemetryConfig) {
this._config = config;
this._sender = new Sender(config);
this.appId = () => this._sender._appId;
// use appId set in config instead of getting it from the backend
if (config.appId()) {
this._sender._appId = config.appId();
}
this.telemetryInitializers = [];
// window will be undefined in node.js where we do not want to initialize contexts
if (typeof window !== 'undefined') {
this._sessionManager = new ApplicationInsights.Context._SessionManager(config);
this.application = new Context.Application();
this.device = new Context.Device();
this.internal = new Context.Internal(config);
this.location = new Context.Location();
this.user = new Context.User(config);
this.operation = new Context.Operation();
this.session = new Context.Session();
this.sample = new Context.Sample(config.sampleRate());
}
this._addDefaultTelemetryInitializers();
}
/**
* Adds telemetry initializer to the collection. Telemetry initializers will be called one by one
* before telemetry item is pushed for sending and in the order they were added.
*/
public addTelemetryInitializer(telemetryInitializer: (envelope: Microsoft.ApplicationInsights.IEnvelope) => boolean | void) {
this.telemetryInitializers.push(telemetryInitializer);
}
/**
* Use Sender.ts to send telemetry object to the endpoint
*/
public track(envelope: Microsoft.ApplicationInsights.IEnvelope) {
if (!envelope) {
_InternalLogging.throwInternal(
LoggingSeverity.CRITICAL,
_InternalMessageId.TrackArgumentsNotSpecified,
"cannot call .track() with a null or undefined argument", null, true);
} else {
// If the envelope is PageView, reset the internal message count so that we can send internal telemetry for the new page.
if (envelope.name === Telemetry.PageView.envelopeType) {
_InternalLogging.resetInternalMessageCount();
}
if (this.session) {
// If customer did not provide custom session id update sessionmanager
if (typeof this.session.id !== "string") {
this._sessionManager.update();
}
}
this._track(envelope);
}
return envelope;
}
private _addDefaultTelemetryInitializers() {
if (!this._config.isBrowserLinkTrackingEnabled()) {
const browserLinkPaths = ['/browserLinkSignalR/', '/__browserLink/'];
let dropBrowserLinkRequests = (envelope: Microsoft.ApplicationInsights.IEnvelope) => {
if (envelope.name === Microsoft.ApplicationInsights.Telemetry.RemoteDependencyData.envelopeType) {
let remoteData = envelope.data as Telemetry.Common.Data<Microsoft.ApplicationInsights.Telemetry.RemoteDependencyData>;
if (remoteData && remoteData.baseData) {
for (let i = 0; i < browserLinkPaths.length; i++) {
if (remoteData.baseData.name.indexOf(browserLinkPaths[i]) >= 0) {
return false;
}
}
}
}
return true;
}
this.addTelemetryInitializer(dropBrowserLinkRequests)
}
}
private _track(envelope: Microsoft.ApplicationInsights.IEnvelope) {
if (this.session) {
// If customer set id, apply his context; otherwise apply context generated from cookies
if (typeof this.session.id === "string") {
this._applySessionContext(envelope, this.session);
} else {
this._applySessionContext(envelope, this._sessionManager.automaticSession);
}
}
this._applyApplicationContext(envelope, this.application);
this._applyDeviceContext(envelope, this.device);
this._applyInternalContext(envelope, this.internal);
this._applyLocationContext(envelope, this.location);
this._applySampleContext(envelope, this.sample);
this._applyUserContext(envelope, this.user);
this._applyOperationContext(envelope, this.operation);
envelope.iKey = this._config.instrumentationKey();
var doNotSendItem = false;
try {
var telemetryInitializersCount = this.telemetryInitializers.length;
for (var i = 0; i < telemetryInitializersCount; ++i) {
var telemetryInitializer = this.telemetryInitializers[i];
if (telemetryInitializer) {
if (telemetryInitializer.apply(null, [envelope]) === false) {
doNotSendItem = true;
break;
}
}
}
} catch (e) {
doNotSendItem = true;
_InternalLogging.throwInternal(
LoggingSeverity.CRITICAL, _InternalMessageId.TelemetryInitializerFailed, "One of telemetry initializers failed, telemetry item will not be sent: " + Util.getExceptionName(e),
{ exception: Util.dump(e) }, true);
}
if (!doNotSendItem) {
if (envelope.name === Telemetry.Metric.envelopeType ||
this.sample.isSampledIn(envelope)) {
var iKeyNoDashes = this._config.instrumentationKey().replace(/-/g, "");
envelope.name = envelope.name.replace("{0}", iKeyNoDashes);
this._sender.send(envelope);
} else {
_InternalLogging.throwInternal(LoggingSeverity.WARNING, _InternalMessageId.TelemetrySampledAndNotSent,
"Telemetry is sampled and not sent to the AI service.", { SampleRate: this.sample.sampleRate }, true);
}
}
return envelope;
}
private _applyApplicationContext(envelope: Microsoft.ApplicationInsights.IEnvelope, appContext: Microsoft.ApplicationInsights.Context.Application) {
if (appContext) {
var tagKeys: AI.ContextTagKeys = new AI.ContextTagKeys();
if (typeof appContext.ver === "string") {
envelope.tags[tagKeys.applicationVersion] = appContext.ver;
}
if (typeof appContext.build === "string") {
envelope.tags[tagKeys.applicationBuild] = appContext.build;
}
}
}
private _applyDeviceContext(envelope: Microsoft.ApplicationInsights.IEnvelope, deviceContext: Microsoft.ApplicationInsights.Context.Device) {
var tagKeys: AI.ContextTagKeys = new AI.ContextTagKeys();
if (deviceContext) {
if (typeof deviceContext.id === "string") {
envelope.tags[tagKeys.deviceId] = deviceContext.id;
}
if (typeof deviceContext.ip === "string") {
envelope.tags[tagKeys.deviceIp] = deviceContext.ip;
}
if (typeof deviceContext.language === "string") {
envelope.tags[tagKeys.deviceLanguage] = deviceContext.language;
}
if (typeof deviceContext.locale === "string") {
envelope.tags[tagKeys.deviceLocale] = deviceContext.locale;
}
if (typeof deviceContext.model === "string") {
envelope.tags[tagKeys.deviceModel] = deviceContext.model;
}
if (typeof deviceContext.network !== "undefined") {
envelope.tags[tagKeys.deviceNetwork] = deviceContext.network;
}
if (typeof deviceContext.oemName === "string") {
envelope.tags[tagKeys.deviceOEMName] = deviceContext.oemName;
}
if (typeof deviceContext.os === "string") {
envelope.tags[tagKeys.deviceOS] = deviceContext.os;
}
if (typeof deviceContext.osversion === "string") {
envelope.tags[tagKeys.deviceOSVersion] = deviceContext.osversion;
}
if (typeof deviceContext.resolution === "string") {
envelope.tags[tagKeys.deviceScreenResolution] = deviceContext.resolution;
}
if (typeof deviceContext.type === "string") {
envelope.tags[tagKeys.deviceType] = deviceContext.type;
}
}
}
private _applyInternalContext(envelope: Microsoft.ApplicationInsights.IEnvelope, internalContext: Microsoft.ApplicationInsights.Context.Internal) {
if (internalContext) {
var tagKeys: AI.ContextTagKeys = new AI.ContextTagKeys();
if (typeof internalContext.agentVersion === "string") {
envelope.tags[tagKeys.internalAgentVersion] = internalContext.agentVersion;
}
if (typeof internalContext.sdkVersion === "string") {
envelope.tags[tagKeys.internalSdkVersion] = internalContext.sdkVersion;
}
}
}
private _applyLocationContext(envelope: Microsoft.ApplicationInsights.IEnvelope, locationContext: Microsoft.ApplicationInsights.Context.Location) {
if (locationContext) {
var tagKeys: AI.ContextTagKeys = new AI.ContextTagKeys();
if (typeof locationContext.ip === "string") {
envelope.tags[tagKeys.locationIp] = locationContext.ip;
}
}
}
private _applyOperationContext(envelope: Microsoft.ApplicationInsights.IEnvelope, operationContext: Microsoft.ApplicationInsights.Context.Operation) {
if (operationContext) {
var tagKeys: AI.ContextTagKeys = new AI.ContextTagKeys();
if (typeof operationContext.id === "string") {
envelope.tags[tagKeys.operationId] = operationContext.id;
}
if (typeof operationContext.name === "string") {
envelope.tags[tagKeys.operationName] = operationContext.name;
}
if (typeof operationContext.parentId === "string") {
envelope.tags[tagKeys.operationParentId] = operationContext.parentId;
}
if (typeof operationContext.rootId === "string") {
envelope.tags[tagKeys.operationRootId] = operationContext.rootId;
}
if (typeof operationContext.syntheticSource === "string") {
envelope.tags[tagKeys.operationSyntheticSource] = operationContext.syntheticSource;
}
}
}
private _applySampleContext(envelope: Microsoft.ApplicationInsights.IEnvelope, sampleContext: Microsoft.ApplicationInsights.Context.Sample) {
if (sampleContext) {
envelope.sampleRate = sampleContext.sampleRate;
}
}
private _applySessionContext(envelope: Microsoft.ApplicationInsights.IEnvelope, sessionContext: Microsoft.ApplicationInsights.Context.Session) {
if (sessionContext) {
var tagKeys: AI.ContextTagKeys = new AI.ContextTagKeys();
if (typeof sessionContext.id === "string") {
envelope.tags[tagKeys.sessionId] = sessionContext.id;
}
if (typeof sessionContext.isFirst !== "undefined") {
envelope.tags[tagKeys.sessionIsFirst] = sessionContext.isFirst;
}
}
}
private _applyUserContext(envelope: Microsoft.ApplicationInsights.IEnvelope, userContext: Microsoft.ApplicationInsights.Context.User) {
if (userContext) {
var tagKeys: AI.ContextTagKeys = new AI.ContextTagKeys();
if (typeof userContext.accountId === "string") {
envelope.tags[tagKeys.userAccountId] = userContext.accountId;
}
if (typeof userContext.agent === "string") {
envelope.tags[tagKeys.userAgent] = userContext.agent;
}
if (typeof userContext.id === "string") {
envelope.tags[tagKeys.userId] = userContext.id;
}
if (typeof userContext.authenticatedId === "string") {
envelope.tags[tagKeys.userAuthUserId] = userContext.authenticatedId;
}
if (typeof userContext.storeRegion === "string") {
envelope.tags[tagKeys.userStoreRegion] = userContext.storeRegion;
}
}
}
}
}