|
5 | 5 | import * as os from "os";
|
6 | 6 | import * as vscode from "vscode";
|
7 | 7 | import * as https from "https";
|
8 |
| -import { type TelemetryClient } from "applicationinsights"; |
9 |
| -import { SenderData, BaseTelemetryReporter, ReplacementOption } from "../common/baseTelemetryReporter"; |
10 |
| -import { BaseTelemetrySender, BaseTelemetryClient } from "../common/baseTelemetrySender"; |
| 8 | +import { BaseTelemetryReporter, ReplacementOption } from "../common/baseTelemetryReporter"; |
| 9 | +import { BaseTelemetrySender } from "../common/baseTelemetrySender"; |
11 | 10 | import { TelemetryUtil } from "../common/util";
|
12 | 11 | import type { IXHROverride, IPayloadData } from "@microsoft/1ds-post-js";
|
13 | 12 | import { oneDataSystemClientFactory } from "../common/1dsClientFactory";
|
14 |
| -/** |
15 |
| - * A factory function which creates a telemetry client to be used by an sender to send telemetry in a node application. |
16 |
| - * |
17 |
| - * @param key The app insights key |
18 |
| - * @param replacementOptions Optional list of {@link ReplacementOption replacements} to apply to the telemetry client. This allows |
19 |
| - * the sender to filter out any sensitive or unnecessary information from the telemetry server. |
20 |
| - * |
21 |
| - * @returns A promise which resolves to the telemetry client or rejects upon error |
22 |
| - */ |
23 |
| -const appInsightsClientFactory = async (key: string, replacementOptions?: ReplacementOption[]): Promise<BaseTelemetryClient> => { |
24 |
| - let appInsightsClient: TelemetryClient | undefined; |
25 |
| - try { |
26 |
| - process.env["APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL"] = "1"; |
27 |
| - const appInsights = await import(/* webpackMode: "eager" */ "applicationinsights"); |
28 |
| - //check if another instance is already initialized |
29 |
| - if (appInsights.defaultClient) { |
30 |
| - appInsightsClient = new appInsights.TelemetryClient(key); |
31 |
| - // no other way to enable offline mode |
32 |
| - appInsightsClient.channel.setUseDiskRetryCaching(true); |
33 |
| - } else { |
34 |
| - appInsights.setup(key) |
35 |
| - .setAutoCollectRequests(false) |
36 |
| - .setAutoCollectPerformance(false) |
37 |
| - .setAutoCollectExceptions(false) |
38 |
| - .setAutoCollectDependencies(false) |
39 |
| - .setAutoDependencyCorrelation(false) |
40 |
| - .setAutoCollectConsole(false) |
41 |
| - .setAutoCollectHeartbeat(false) |
42 |
| - .setAutoCollectIncomingRequestAzureFunctions(false) |
43 |
| - .setUseDiskRetryCaching(true) |
44 |
| - .start(); |
45 |
| - appInsightsClient = appInsights.defaultClient; |
46 |
| - } |
47 |
| - if (vscode && vscode.env) { |
48 |
| - appInsightsClient.context.tags[appInsightsClient.context.keys.userId] = vscode.env.machineId; |
49 |
| - appInsightsClient.context.tags[appInsightsClient.context.keys.sessionId] = vscode.env.sessionId; |
50 |
| - appInsightsClient.context.tags[appInsightsClient.context.keys.cloudRole] = vscode.env.appName; |
51 |
| - appInsightsClient.context.tags[appInsightsClient.context.keys.cloudRoleInstance] = vscode.env.appName; |
52 |
| - } |
53 |
| - } catch (e: any) { |
54 |
| - return Promise.reject("Failed to initialize app insights!\n" + e.message); |
55 |
| - } |
56 |
| - |
57 |
| - if (replacementOptions?.length) { |
58 |
| - addReplacementOptions(appInsightsClient, replacementOptions); |
59 |
| - } |
60 |
| - |
61 |
| - // Sets the appinsights client into a standardized form |
62 |
| - const telemetryClient: BaseTelemetryClient = { |
63 |
| - logEvent: (eventName: string, data?: SenderData) => { |
64 |
| - try { |
65 |
| - appInsightsClient?.trackEvent({ |
66 |
| - name: eventName, |
67 |
| - properties: data?.properties, |
68 |
| - measurements: data?.measurements |
69 |
| - }); |
70 |
| - } catch (e: any) { |
71 |
| - throw new Error("Failed to log event to app insights!\n" + e.message); |
72 |
| - } |
73 |
| - }, |
74 |
| - flush: async () => { |
75 |
| - try { |
76 |
| - appInsightsClient?.flush(); |
77 |
| - } catch (e: any) { |
78 |
| - throw new Error("Failed to flush app insights!\n" + e.message); |
79 |
| - } |
80 |
| - }, |
81 |
| - dispose: async () => { |
82 |
| - appInsightsClient?.flush({ isAppCrashing: true }); |
83 |
| - appInsightsClient = undefined; |
84 |
| - } |
85 |
| - }; |
86 |
| - return telemetryClient; |
87 |
| -}; |
88 |
| - |
89 |
| -/** |
90 |
| - * Adds replacement options to this {@link TelemetryClient}. |
91 |
| - * |
92 |
| - * If any replacement options are specified, this function will search through any event about to be |
93 |
| - * sent to the telemetry server and replace any matches with the specified replacement string. Both |
94 |
| - * the envelope and the base data will be searched. |
95 |
| - * |
96 |
| - * @param appInsightsClient The {@link TelemetryClient} to add the filters to. |
97 |
| - * @param replacementOptions The replacement options to add. |
98 |
| - */ |
99 |
| -function addReplacementOptions(appInsightsClient: TelemetryClient, replacementOptions: ReplacementOption[]) { |
100 |
| - appInsightsClient.addTelemetryProcessor((event) => { |
101 |
| - if (Array.isArray(event.tags)) { |
102 |
| - event.tags.forEach(tag => TelemetryUtil.applyReplacements(tag, replacementOptions)); |
103 |
| - } else if (event.tags) { |
104 |
| - TelemetryUtil.applyReplacements(event.tags, replacementOptions); |
105 |
| - } |
106 |
| - |
107 |
| - if (event.data.baseData) { |
108 |
| - TelemetryUtil.applyReplacements(event.data.baseData, replacementOptions); |
109 |
| - } |
110 |
| - return true; |
111 |
| - }); |
112 |
| -} |
| 13 | +import { appInsightsClientFactory } from "../common/appInsightsClientFactory"; |
113 | 14 |
|
114 | 15 | /**
|
115 | 16 | * Create a replacement for the XHTMLRequest object utilizing nodes HTTP module.
|
@@ -154,7 +55,7 @@ function getXHROverride() {
|
154 | 55 |
|
155 | 56 | export default class TelemetryReporter extends BaseTelemetryReporter {
|
156 | 57 | constructor(key: string, replacementOptions?: ReplacementOption[]) {
|
157 |
| - let clientFactory = (key: string) => appInsightsClientFactory(key, replacementOptions); |
| 58 | + let clientFactory = (key: string) => appInsightsClientFactory(key, getXHROverride(), replacementOptions); |
158 | 59 | // If key is usable by 1DS use the 1DS SDk
|
159 | 60 | if (TelemetryUtil.shouldUseOneDataSystemSDK(key)) {
|
160 | 61 | clientFactory = (key: string) => oneDataSystemClientFactory(key, vscode, getXHROverride());
|
|
0 commit comments