-
Notifications
You must be signed in to change notification settings - Fork 44
/
telemetryReporter.ts
86 lines (82 loc) · 3.17 KB
/
telemetryReporter.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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import type { ApplicationInsights } from "@microsoft/applicationinsights-web";
import * as vscode from "vscode";
import { oneDataSystemClientFactory } from "../common/1dsClientFactory";
import { BaseTelemetryAppender, BaseTelemetryClient } from "../common/baseTelemetryAppender";
import { AppenderData, BaseTelemetryReporter, ReplacementOption } from "../common/baseTelemetryReporter";
import { TelemetryUtil } from "../common/util";
const webAppInsightsClientFactory = async (key: string, replacementOptions?: ReplacementOption[]): Promise<BaseTelemetryClient> => {
let appInsightsClient: ApplicationInsights | undefined;
try {
const web = await import/* webpackMode: "eager" */ ("@microsoft/applicationinsights-web");
appInsightsClient = new web.ApplicationInsights({
config: {
instrumentationKey: key,
disableAjaxTracking: true,
disableExceptionTracking: true,
disableFetchTracking: true,
disableCorrelationHeaders: true,
disableCookiesUsage: true,
autoTrackPageVisitTime: false,
emitLineDelimitedJson: true,
disableInstrumentationKeyValidation: true
},
});
appInsightsClient.loadAppInsights();
} catch (e) {
return Promise.reject(e);
}
// Sets the appinsights client into a standardized form
const telemetryClient: BaseTelemetryClient = {
logEvent: (eventName: string, data?: AppenderData) => {
const properties = { ...data?.properties, ...data?.measurements };
if (replacementOptions?.length) {
TelemetryUtil.applyReplacements(properties, replacementOptions);
}
appInsightsClient?.trackEvent(
{ name: eventName },
properties
);
},
logException: (exception: Error, data?: AppenderData) => {
const properties = { ...data?.properties, ...data?.measurements };
if (replacementOptions?.length) {
TelemetryUtil.applyReplacements(properties, replacementOptions);
}
appInsightsClient?.trackException(
{
exception,
properties
});
},
flush: async () => {
appInsightsClient?.flush();
}
};
return telemetryClient;
};
export default class TelemetryReporter extends BaseTelemetryReporter {
constructor(extensionId: string, extensionVersion: string, key: string, firstParty?: boolean, replacementOptions?: ReplacementOption[]) {
let clientFactory = (key: string) => webAppInsightsClientFactory(key, replacementOptions);
// If key is usable by 1DS use the 1DS SDk
if (TelemetryUtil.shouldUseOneDataSystemSDK(key)) {
clientFactory = (key: string) => oneDataSystemClientFactory(key, vscode);
}
const appender = new BaseTelemetryAppender(key, clientFactory);
// AIF is no longer supported
if (key && (key.indexOf("AIF") === 0)) {
throw new Error("AIF keys are no longer supported. Please switch to 1DS keys for 1st party extensions");
}
// If it's a 1DS key it is first party
if (TelemetryUtil.shouldUseOneDataSystemSDK(key)) {
firstParty = true;
}
super(extensionId, extensionVersion, appender, {
release: navigator.appVersion,
platform: "web",
architecture: "web",
}, vscode, firstParty);
}
}