Skip to content

Commit 68a5cdd

Browse files
committed
fix: telemetry via proxy
1 parent cab02f7 commit 68a5cdd

File tree

5 files changed

+572
-240
lines changed

5 files changed

+572
-240
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@
3535
"!lib/**/*.map"
3636
],
3737
"dependencies": {
38-
"@salesforce/core": "^4.3.11",
38+
"@salesforce/core": "^5.1.4",
3939
"@salesforce/ts-types": "^2.0.5",
4040
"applicationinsights": "^2.7.0",
41-
"axios": "^1.4.0"
41+
"got": "^11",
42+
"proxy-agent": "^6.3.0"
4243
},
4344
"devDependencies": {
4445
"@salesforce/dev-config": "^4.0.1",

src/telemetryReporter.ts

+22-23
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import * as os from 'os';
88
import { Logger, ConfigAggregator, SfConfigProperties } from '@salesforce/core';
99
import { AsyncCreatable, env } from '@salesforce/kit';
1010

11-
import axios from 'axios';
11+
import got from 'got';
12+
import { ProxyAgent } from 'proxy-agent';
1213
import { AppInsights, Attributes, Properties, TelemetryOptions } from './appInsights';
1314
import { TelemetryClient } from './exported';
1415

@@ -82,32 +83,30 @@ export class TelemetryReporter extends AsyncCreatable<TelemetryOptions> {
8283
const timeout = parseInt(env.getString('SFDX_TELEMETRY_TIMEOUT', '1000'), 10);
8384
this.logger.debug(`Testing connection to ${AppInsights.APP_INSIGHTS_SERVER} with timeout of ${timeout} ms`);
8485

85-
// set up a CancelToken to handle connection timeouts because
86-
// the built in timeout functionality only handles response timeouts
87-
// see here: https://github.com/axios/axios/issues/647#issuecomment-322209906
88-
const cancelRequest = axios.CancelToken.source();
89-
setTimeout(() => cancelRequest.cancel('connection timeout'), timeout);
90-
91-
let canConnect: boolean;
9286
try {
93-
const options = {
94-
timeout,
95-
cancelToken: cancelRequest.token,
96-
// We want any status less than 500 to be resolved (not rejected)
97-
validateStatus: (status: number): boolean => status < 500,
98-
};
99-
await axios.get(AppInsights.APP_INSIGHTS_SERVER, options);
100-
canConnect = true;
87+
const resp = await got.get(AppInsights.APP_INSIGHTS_SERVER, {
88+
throwHttpErrors: false,
89+
agent: { https: new ProxyAgent() },
90+
retry: {
91+
methods: ['GET'],
92+
errorCodes: ['ECONNRESET', 'EADDRINUSE', 'ECONNREFUSED', 'EPIPE'],
93+
},
94+
timeout: {
95+
lookup: 100,
96+
send: 10000,
97+
response: 1000,
98+
},
99+
});
100+
if (resp.statusCode < 500) {
101+
this.logger.debug(`Successfully made a connection to ${AppInsights.APP_INSIGHTS_SERVER}`);
102+
return true;
103+
}
104+
this.logger.error(`${AppInsights.APP_INSIGHTS_SERVER} responded with ${resp.statusCode}`);
105+
throw new Error(resp.statusCode.toString());
101106
} catch (err) {
102-
canConnect = false;
103-
}
104-
105-
if (canConnect) {
106-
this.logger.debug(`Successfully made a connection to ${AppInsights.APP_INSIGHTS_SERVER}`);
107-
} else {
108107
this.logger.warn(`Connection to ${AppInsights.APP_INSIGHTS_SERVER} timed out after ${timeout} ms`);
108+
return false;
109109
}
110-
return canConnect;
111110
}
112111

113112
/**

test/unit/telemetryReporter.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
import * as os from 'os';
88
import { ConfigAggregator, Logger } from '@salesforce/core';
9-
import axios from 'axios';
9+
import got from 'got';
1010
import { expect } from 'chai';
1111
import * as sinon from 'sinon';
1212
import { AppInsights } from '../../src/appInsights';
@@ -161,7 +161,7 @@ describe('TelemetryReporter', () => {
161161
});
162162

163163
it('should throw an error if it cannot connect to app insights', async () => {
164-
sandbox.stub(axios, 'get').throws(() => ({ code: 'TIMEOUT!' }));
164+
sandbox.stub(got, 'get').throws(() => ({ code: 'TIMEOUT!' }));
165165
const options = { project, key, waitForConnection: true };
166166
try {
167167
await TelemetryReporter.create(options);

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"extends": "@salesforce/dev-config/tsconfig-strict",
33
"compilerOptions": {
4+
"esModuleInterop": true,
45
"outDir": "./lib",
56
"rootDir": "src",
67
"baseUrl": "."
78
},
89
"include": ["src/**/*.ts"]
9-
}
10+
}

0 commit comments

Comments
 (0)