Skip to content

Commit c31b67b

Browse files
committed
More disposal / flush logic
1 parent 03d2783 commit c31b67b

6 files changed

+64
-27
lines changed

src/browser/telemetryReporter.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,18 @@ const webAppInsightsClientFactory = async (key: string, replacementOptions?: Rep
4040
);
4141
},
4242
flush: async () => {
43-
appInsightsClient?.flush();
43+
appInsightsClient?.flush(false);
44+
},
45+
dispose: async () => {
46+
appInsightsClient?.flush(true);
47+
const unloadPromise = new Promise<void>((resolve) => {
48+
appInsightsClient?.unload(true, () => {
49+
resolve();
50+
appInsightsClient = undefined;
51+
});
52+
}
53+
);
54+
return unloadPromise;
4455
}
4556
};
4657
return telemetryClient;

src/common/1dsClientFactory.ts

+34-22
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,26 @@ const getAICore = async (key: string, vscodeAPI: typeof vscode, xhrOverride?: IX
6969
* @param xhrOverride An optional override to use for requests instead of the XHTMLRequest object. Useful for node environments
7070
*/
7171
export const oneDataSystemClientFactory = async (key: string, vscodeAPI: typeof vscode, xhrOverride?: IXHROverride): Promise<BaseTelemetryClient> => {
72-
const appInsightsCore = await getAICore(key, vscodeAPI, xhrOverride);
72+
let appInsightsCore: AppInsightsCore | undefined = await getAICore(key, vscodeAPI, xhrOverride);
73+
const flushOneDS = async () => {
74+
try {
75+
const flushPromise = new Promise<void>((resolve, reject) => {
76+
if (!appInsightsCore) {
77+
resolve();
78+
return;
79+
}
80+
appInsightsCore.flush(true, (completedFlush) => {
81+
if (!completedFlush) {
82+
reject("Failed to flush app 1DS!");
83+
return;
84+
}
85+
});
86+
});
87+
return flushPromise;
88+
} catch (e: any) {
89+
throw new Error("Failed to flush 1DS!\n" + e.message);
90+
}
91+
};
7392
// Shape the app insights core from 1DS into a standard format
7493
const telemetryClient: BaseTelemetryClient = {
7594
logEvent: (eventName: string, data?: SenderData) => {
@@ -82,28 +101,21 @@ export const oneDataSystemClientFactory = async (key: string, vscodeAPI: typeof
82101
throw new Error("Failed to log event to app insights!\n" + e.message);
83102
}
84103
},
85-
flush: async () => {
86-
try {
87-
const flushPromise = new Promise<void>((resolve, reject) => {
88-
if (!appInsightsCore) {
89-
resolve();
90-
return;
91-
}
92-
appInsightsCore.flush(true, (completedFlush) => {
93-
if (!completedFlush) {
94-
reject("Failed to flush app 1DS!");
95-
return;
96-
}
97-
appInsightsCore.unload(true, () => {
98-
resolve();
99-
return;
100-
});
101-
});
104+
flush: flushOneDS,
105+
dispose: async () => {
106+
await flushOneDS();
107+
const disposePromise = new Promise<void>((resolve) => {
108+
if (!appInsightsCore) {
109+
resolve();
110+
return;
111+
}
112+
appInsightsCore.unload(true, () => {
113+
resolve();
114+
appInsightsCore = undefined;
115+
return;
102116
});
103-
return flushPromise;
104-
} catch (e: any) {
105-
throw new Error("Failed to flush 1DS!\n" + e.message);
106-
}
117+
});
118+
return disposePromise;
107119
}
108120
};
109121
return telemetryClient;

src/common/baseTelemetryReporter.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ export class BaseTelemetryReporter {
167167
/**
168168
* Disposes of the telemetry reporter
169169
*/
170-
public dispose(): Promise<any> {
170+
public async dispose(): Promise<any> {
171+
await this.telemetrySender.dispose();
171172
this.telemetryLogger.dispose();
172173
return Promise.all(this.disposables.map(d => d.dispose()));
173174
}

src/common/baseTelemetrySender.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import { SenderData } from "./baseTelemetryReporter";
77

88
export interface BaseTelemetryClient {
99
logEvent(eventName: string, data?: SenderData): void;
10-
flush(): void | Promise<void>;
10+
flush(): Promise<void>;
11+
dispose(): Promise<void>;
1112
}
1213

1314
export interface ILazyTelemetrySender extends TelemetrySender {
1415
instantiateSender(): void
16+
dispose(): Promise<void>;
1517
}
1618

1719
enum InstantiationStatus {
@@ -83,8 +85,13 @@ export class BaseTelemetrySender implements ILazyTelemetrySender {
8385
* Flushes the buffered telemetry data
8486
*/
8587
async flush(): Promise<void> {
88+
return this._telemetryClient?.flush();
89+
}
90+
91+
async dispose(): Promise<void> {
92+
await this.flush();
8693
if (this._telemetryClient) {
87-
await this._telemetryClient.flush();
94+
await this._telemetryClient.dispose();
8895
this._telemetryClient = undefined;
8996
}
9097
return;

src/node/telemetryReporter.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import * as os from "os";
66
import * as vscode from "vscode";
77
import * as https from "https";
8-
import type { TelemetryClient } from "applicationinsights";
8+
import { type TelemetryClient } from "applicationinsights";
99
import { SenderData, BaseTelemetryReporter, ReplacementOption } from "../common/baseTelemetryReporter";
1010
import { BaseTelemetrySender, BaseTelemetryClient } from "../common/baseTelemetrySender";
1111
import { TelemetryUtil } from "../common/util";
@@ -39,6 +39,7 @@ const appInsightsClientFactory = async (key: string, replacementOptions?: Replac
3939
.setAutoDependencyCorrelation(false)
4040
.setAutoCollectConsole(false)
4141
.setAutoCollectHeartbeat(false)
42+
.setAutoCollectIncomingRequestAzureFunctions(false)
4243
.setUseDiskRetryCaching(true)
4344
.start();
4445
appInsightsClient = appInsights.defaultClient;
@@ -76,6 +77,10 @@ const appInsightsClientFactory = async (key: string, replacementOptions?: Replac
7677
} catch (e: any) {
7778
throw new Error("Failed to flush app insights!\n" + e.message);
7879
}
80+
},
81+
dispose: async () => {
82+
appInsightsClient?.flush();
83+
appInsightsClient = undefined;
7984
}
8085
};
8186
return telemetryClient;

test/baseTelemetrySender.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe("Base telemetry sender test suite", () => {
1111
const telemetryClient: BaseTelemetryClient = {
1212
logEvent: sinon.spy(),
1313
flush: sinon.spy(),
14+
dispose: sinon.spy(),
1415
};
1516
const telemetryClientFactory: (key: string) => Promise<BaseTelemetryClient> = async () => {
1617
return telemetryClient;

0 commit comments

Comments
 (0)