-
Notifications
You must be signed in to change notification settings - Fork 237
/
TelemetryContext.ts
229 lines (193 loc) · 10.8 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
/**
* TelemetryContext.ts
* @copyright Microsoft 2018
*/
import dynamicProto from "@microsoft/dynamicproto-js";
import {
CtxTagKeys, Extensions, IApplication, IDevice, IInternal, ILocation, IOperatingSystem, ISession, ITelemetryTrace, IUserContext, IWeb,
PageView, dataSanitizeString
} from "@microsoft/applicationinsights-common";
import {
IAppInsightsCore, IDistributedTraceContext, IProcessTelemetryContext, ITelemetryItem, _InternalLogMessage, getSetValue, hasWindow,
isNullOrUndefined, isString, objKeys, setValue
} from "@microsoft/applicationinsights-core-js";
import { Application } from "./Context/Application";
import { Device } from "./Context/Device";
import { Internal } from "./Context/Internal";
import { Location } from "./Context/Location";
import { Session, _SessionManager } from "./Context/Session";
import { TelemetryTrace } from "./Context/TelemetryTrace";
import { User } from "./Context/User";
import { IPropTelemetryContext } from "./Interfaces/IPropTelemetryContext";
import { ITelemetryConfig } from "./Interfaces/ITelemetryConfig";
const strExt = "ext";
const strTags = "tags";
function _removeEmpty(target: any, name: string) {
if (target && target[name] && objKeys(target[name]).length === 0) {
delete target[name];
}
}
export class TelemetryContext implements IPropTelemetryContext {
public application: IApplication; // The object describing a component tracked by this object - legacy
public device: IDevice; // The object describing a device tracked by this object.
public location: ILocation; // The object describing a location tracked by this object -legacy
public telemetryTrace: ITelemetryTrace; // The object describing a operation tracked by this object.
public user: IUserContext; // The object describing a user tracked by this object.
public internal: IInternal; // legacy
public session: ISession; // The object describing a session tracked by this object.
public sessionManager: _SessionManager; // The session manager that manages session on the base of cookies.
public os: IOperatingSystem;
public web: IWeb;
public appId: () => string;
public getSessionId: () => string;
constructor(core: IAppInsightsCore, defaultConfig: ITelemetryConfig, previousTraceCtx?: IDistributedTraceContext) {
let logger = core.logger
this.appId = () => null;
this.getSessionId = () => null;
dynamicProto(TelemetryContext, this, (_self) => {
_self.application = new Application();
_self.internal = new Internal(defaultConfig);
if (hasWindow()) {
_self.sessionManager = new _SessionManager(defaultConfig, core);
_self.device = new Device();
_self.location = new Location();
_self.user = new User(defaultConfig, core);
let traceId: string;
let parentId: string;
let name: string;
if (previousTraceCtx) {
traceId = previousTraceCtx.getTraceId();
parentId = previousTraceCtx.getSpanId();
name = previousTraceCtx.getName();
}
_self.telemetryTrace = new TelemetryTrace(traceId, parentId, name, logger);
_self.session = new Session();
}
_self.getSessionId = () => {
let session = _self.session;
let sesId = null;
// If customer set session info, apply their context; otherwise apply context automatically generated
if (session && isString(session.id)) {
sesId = session.id;
} else {
// Gets the automatic session if it exists or an empty object
let autoSession = (_self.sessionManager || {} as _SessionManager).automaticSession;
sesId = autoSession && isString(autoSession.id) ? autoSession.id : null;
}
return sesId;
}
_self.applySessionContext = (evt: ITelemetryItem, itemCtx?: IProcessTelemetryContext) => {
setValue(getSetValue(evt.ext, Extensions.AppExt), "sesId", _self.getSessionId(), isString);
}
_self.applyOperatingSystemContxt = (evt: ITelemetryItem, itemCtx?: IProcessTelemetryContext) => {
setValue(evt.ext, Extensions.OSExt, _self.os);
};
_self.applyApplicationContext = (evt: ITelemetryItem, itemCtx?: IProcessTelemetryContext) => {
let application = _self.application;
if (application) {
// evt.ext.app
let tags = getSetValue(evt, strTags);
setValue(tags, CtxTagKeys.applicationVersion, application.ver, isString);
setValue(tags, CtxTagKeys.applicationBuild, application.build, isString)
}
};
_self.applyDeviceContext = (evt: ITelemetryItem, itemCtx?: IProcessTelemetryContext) => {
let device = _self.device;
if (device) {
// evt.ext.device
let extDevice = getSetValue(getSetValue(evt, strExt), Extensions.DeviceExt);
setValue(extDevice, "localId", device.id, isString);
setValue(extDevice, "ip", device.ip, isString);
setValue(extDevice, "model", device.model, isString);
setValue(extDevice, "deviceClass", device.deviceClass, isString);
}
};
_self.applyInternalContext = (evt: ITelemetryItem, itemCtx?: IProcessTelemetryContext) => {
let internal = _self.internal;
if (internal) {
let tags = getSetValue(evt, strTags);
setValue(tags, CtxTagKeys.internalAgentVersion, internal.agentVersion, isString); // not mapped in CS 4.0
setValue(tags, CtxTagKeys.internalSdkVersion, dataSanitizeString(logger, internal.sdkVersion, 64), isString);
if (evt.baseType === _InternalLogMessage.dataType || evt.baseType === PageView.dataType) {
setValue(tags, CtxTagKeys.internalSnippet, internal.snippetVer, isString);
setValue(tags, CtxTagKeys.internalSdkSrc, internal.sdkSrc, isString);
}
}
};
_self.applyLocationContext = (evt: ITelemetryItem, itemCtx?: IProcessTelemetryContext) => {
let location = this.location;
if (location) {
setValue(getSetValue(evt, strTags, []), CtxTagKeys.locationIp, location.ip, isString);
}
};
_self.applyOperationContext = (evt: ITelemetryItem, itemCtx?: IProcessTelemetryContext) => {
let telemetryTrace = _self.telemetryTrace;
if (telemetryTrace) {
const extTrace = getSetValue(getSetValue(evt, strExt), Extensions.TraceExt, { traceID: undefined, parentID: undefined } as ITelemetryTrace);
setValue(extTrace, "traceID", telemetryTrace.traceID, isString, isNullOrUndefined);
setValue(extTrace, "name", telemetryTrace.name, isString, isNullOrUndefined);
setValue(extTrace, "parentID", telemetryTrace.parentID, isString, isNullOrUndefined);
}
};
_self.applyWebContext = (evt: ITelemetryItem, itemCtx?: IProcessTelemetryContext) => {
let web = this.web;
if (web) {
setValue(getSetValue(evt, strExt), Extensions.WebExt, web);
}
}
_self.applyUserContext = (evt: ITelemetryItem, itemCtx?: IProcessTelemetryContext) => {
let user = _self.user;
if (user) {
let tags = getSetValue(evt, strTags, []);
// stays in tags
setValue(tags, CtxTagKeys.userAccountId, user.accountId, isString);
// CS 4.0
let extUser = getSetValue(getSetValue(evt, strExt), Extensions.UserExt);
setValue(extUser, "id", user.id, isString);
setValue(extUser, "authId", user.authenticatedId, isString);
}
}
_self.cleanUp = (evt:ITelemetryItem, itemCtx?: IProcessTelemetryContext) => {
let ext = evt.ext;
if (ext) {
_removeEmpty(ext, Extensions.DeviceExt);
_removeEmpty(ext, Extensions.UserExt);
_removeEmpty(ext, Extensions.WebExt);
_removeEmpty(ext, Extensions.OSExt);
_removeEmpty(ext, Extensions.AppExt);
_removeEmpty(ext, Extensions.TraceExt);
}
}
});
}
public applySessionContext(evt: ITelemetryItem, itemCtx?: IProcessTelemetryContext) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
public applyOperatingSystemContxt(event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
public applyApplicationContext(event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
public applyDeviceContext(event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
public applyInternalContext(event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
public applyLocationContext(event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
public applyOperationContext(event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
public applyWebContext(event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
public applyUserContext(event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
public cleanUp(event:ITelemetryItem, itemCtx?: IProcessTelemetryContext): void {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
}