-
Notifications
You must be signed in to change notification settings - Fork 49
/
telemetryReporter.ts
76 lines (71 loc) · 2.81 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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import * as os from "os";
import * as vscode from "vscode";
import * as https from "https";
import { BaseTelemetryReporter, ReplacementOption } from "../common/baseTelemetryReporter";
import { BaseTelemetrySender } from "../common/baseTelemetrySender";
import { TelemetryUtil } from "../common/util";
import type { IXHROverride, IPayloadData } from "@microsoft/1ds-post-js";
import { oneDataSystemClientFactory } from "../common/1dsClientFactory";
import { appInsightsClientFactory } from "../common/appInsightsClientFactory";
/**
* Create a replacement for the XHTMLRequest object utilizing nodes HTTP module.
* @returns A XHR override object used to override the XHTMLRequest object in the 1DS SDK
*/
function getXHROverride() {
// Override the way events get sent since node doesn't have XHTMLRequest
const customHttpXHROverride: IXHROverride = {
sendPOST: (payload: IPayloadData, oncomplete) => {
const options = {
method: "POST",
headers: {
...payload.headers,
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(payload.data)
}
};
try {
const req = https.request(payload.urlString, options, res => {
res.on("data", function (responseData) {
oncomplete(res.statusCode ?? 200, res.headers as Record<string, any>, responseData.toString());
});
// On response with error send status of 0 and a blank response to oncomplete so we can retry events
res.on("error", function () {
oncomplete(0, {});
});
});
req.write(payload.data, (err) => {
if (err) {
oncomplete(0, {});
}
});
req.end();
} catch {
// If it errors out, send status of 0 and a blank response to oncomplete so we can retry events
oncomplete(0, {});
}
}
};
return customHttpXHROverride;
}
export default class TelemetryReporter extends BaseTelemetryReporter {
constructor(key: string, replacementOptions?: ReplacementOption[]) {
let clientFactory = (key: string) => appInsightsClientFactory(key, getXHROverride(), replacementOptions);
// If key is usable by 1DS use the 1DS SDk
if (TelemetryUtil.shouldUseOneDataSystemSDK(key)) {
clientFactory = (key: string) => oneDataSystemClientFactory(key, vscode, getXHROverride());
}
const osShim = {
release: os.release(),
platform: os.platform(),
architecture: os.arch(),
};
const sender = new BaseTelemetrySender(key, clientFactory,);
if (key && key.indexOf("AIF-") === 0) {
throw new Error("AIF keys are no longer supported. Please switch to 1DS keys for 1st party extensions");
}
super(sender, vscode, { additionalCommonProperties: TelemetryUtil.getAdditionalCommonProperties(osShim) });
}
}