|
4 | 4 |
|
5 | 5 | import * as os from "os";
|
6 | 6 | import * as vscode from "vscode";
|
| 7 | +import * as https from "https"; |
7 | 8 | import type { TelemetryClient } from "applicationinsights";
|
8 | 9 | import { AppenderData, BaseTelemetryReporter, ReplacementOption } from "../common/baseTelemetryReporter";
|
9 | 10 | import { BaseTelemetryAppender, BaseTelemetryClient } from "../common/baseTelemetryAppender";
|
10 | 11 | import { TelemetryUtil } from "../common/util";
|
| 12 | +import type { IXHROverride, IPayloadData } from "@microsoft/1ds-post-js"; |
| 13 | +import { oneDataSystemClientFactory } from "../common/1dsClientFactory"; |
11 | 14 | /**
|
12 | 15 | * A factory function which creates a telemetry client to be used by an appender to send telemetry in a node application.
|
13 | 16 | *
|
@@ -118,10 +121,54 @@ function addReplacementOptions(appInsightsClient: TelemetryClient, replacementOp
|
118 | 121 | });
|
119 | 122 | }
|
120 | 123 |
|
| 124 | +/** |
| 125 | + * Create a replacement for the XHTMLRequest object utilizing nodes HTTP module. |
| 126 | + * @returns A XHR override object used to override the XHTMLRequest object in the 1DS SDK |
| 127 | + */ |
| 128 | +function getXHROverride() { |
| 129 | + // Override the way events get sent since node doesn't have XHTMLRequest |
| 130 | + const customHttpXHROverride: IXHROverride = { |
| 131 | + sendPOST: (payload: IPayloadData, oncomplete) => { |
| 132 | + const options = { |
| 133 | + method: "POST", |
| 134 | + headers: { |
| 135 | + ...payload.headers, |
| 136 | + "Content-Type": "application/json", |
| 137 | + "Content-Length": Buffer.byteLength(payload.data) |
| 138 | + } |
| 139 | + }; |
| 140 | + try { |
| 141 | + const req = https.request(payload.urlString, options, res => { |
| 142 | + res.on("data", function (responseData) { |
| 143 | + oncomplete(res.statusCode ?? 200, res.headers as Record<string, any>, responseData.toString()); |
| 144 | + }); |
| 145 | + // On response with error send status of 0 and a blank response to oncomplete so we can retry events |
| 146 | + res.on("error", function () { |
| 147 | + oncomplete(0, {}); |
| 148 | + }); |
| 149 | + }); |
| 150 | + req.write(payload.data); |
| 151 | + req.end(); |
| 152 | + } catch { |
| 153 | + // If it errors out, send status of 0 and a blank response to oncomplete so we can retry events |
| 154 | + oncomplete(0, {}); |
| 155 | + } |
| 156 | + } |
| 157 | + }; |
| 158 | + return customHttpXHROverride; |
| 159 | +} |
| 160 | + |
121 | 161 | export default class TelemetryReporter extends BaseTelemetryReporter {
|
122 | 162 | constructor(extensionId: string, extensionVersion: string, key: string, firstParty?: boolean, replacementOptions?: ReplacementOption[]) {
|
123 |
| - const appender = new BaseTelemetryAppender(key, (key) => appInsightsClientFactory(key, replacementOptions)); |
124 |
| - if (key && key.indexOf("AIF-") === 0) { |
| 163 | + let clientFactory = (key: string) => appInsightsClientFactory(key, replacementOptions); |
| 164 | + // If key is usable by 1DS use the 1DS SDk |
| 165 | + if (TelemetryUtil.shouldUseOneDataSystemSDK(key)) { |
| 166 | + clientFactory = (key: string) => oneDataSystemClientFactory(key, getXHROverride()); |
| 167 | + } |
| 168 | + |
| 169 | + const appender = new BaseTelemetryAppender(key, clientFactory); |
| 170 | + // If it's a specialized AIF app insights key or a 1DS key then it is first party |
| 171 | + if (key && (key.indexOf("AIF-") === 0 || TelemetryUtil.shouldUseOneDataSystemSDK(key))) { |
125 | 172 | firstParty = true;
|
126 | 173 | }
|
127 | 174 | super(extensionId, extensionVersion, appender, {
|
|
0 commit comments