Skip to content

Commit 7fd23b2

Browse files
committed
feat: enabled as a standlone export
1 parent 2eb717f commit 7fd23b2

5 files changed

+48
-28
lines changed

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"version": "4.0.16",
44
"description": "Library for application insights",
55
"main": "lib/exported",
6+
"exports": {
7+
"./enabledCheck": "./lib/enabledCheck.js",
8+
".": "./lib/exported.js"
9+
},
610
"repository": "forcedotcom/telemetry",
711
"author": "Salesforce",
812
"license": "BSD-3-Clause",

src/enabledCheck.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2023, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
8+
// deep imports to avoid requiring the ENTIRE package (which will also pull in jsforce) until we get ESM done
9+
import { ConfigAggregator } from '@salesforce/core/lib/config/configAggregator';
10+
import { SfConfigProperties } from '@salesforce/core/lib/config/config';
11+
12+
// store the result to reduce checks
13+
let enabled: boolean | undefined;
14+
15+
/**
16+
*
17+
* Check ConfigAggregator once for telemetry opt-out. Returns true unless config/env has opt-out
18+
* If you don't pass in a ConfigAggregator, one will be constructed for you
19+
* memoized: only runs once
20+
*
21+
* */
22+
export const isEnabled = async (configAggregator?: ConfigAggregator) => {
23+
if (enabled === undefined) {
24+
const agg = configAggregator ?? (await ConfigAggregator.create({}));
25+
enabled = agg.getPropertyValue<string>(SfConfigProperties.DISABLE_TELEMETRY) !== 'true';
26+
}
27+
return enabled;
28+
};

src/exported.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
import { TelemetryReporter } from './telemetryReporter';
1010

1111
export * from './telemetryReporter';
12+
export { isEnabled } from './enabledCheck';
1213
export default TelemetryReporter;

src/telemetryReporter.ts

+9-23
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,24 @@
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
import * as os from 'os';
8-
import { Logger, ConfigAggregator, SfConfigProperties } from '@salesforce/core';
8+
import { Logger, SfConfigProperties } from '@salesforce/core';
99
import { AsyncCreatable, env } from '@salesforce/kit';
1010

1111
import got from 'got';
1212
import { ProxyAgent } from 'proxy-agent';
13-
import { AppInsights, Attributes, Properties, TelemetryOptions } from './appInsights';
13+
import { AppInsights, type Attributes, type Properties, type TelemetryOptions } from './appInsights';
1414
import { TelemetryClient } from './exported';
15+
import { isEnabled } from './enabledCheck';
1516

1617
export { TelemetryOptions, Attributes, Properties, TelemetryClient } from './appInsights';
1718

1819
/**
1920
* Reports telemetry events to app insights. We do not send if the config 'disableTelemetry' is set.
2021
*/
2122
export class TelemetryReporter extends AsyncCreatable<TelemetryOptions> {
22-
// Keep a cache of config aggregator so we aren't loading it every time.
23-
private static config: ConfigAggregator;
24-
23+
private enabled = false;
2524
private options: TelemetryOptions;
2625
private logger!: Logger;
27-
private config!: ConfigAggregator;
2826
private reporter!: AppInsights;
2927

3028
public constructor(options: TelemetryOptions) {
@@ -33,25 +31,17 @@ export class TelemetryReporter extends AsyncCreatable<TelemetryOptions> {
3331
}
3432

3533
/**
34+
* @deprecated Use the standalone function isEnabled() instead.
3635
* Determine if the telemetry event should be logged.
3736
* Setting the disableTelemetry config var to true will disable insights for errors and diagnostics.
3837
*/
3938
public static async determineSfdxTelemetryEnabled(): Promise<boolean> {
40-
if (!TelemetryReporter.config) {
41-
TelemetryReporter.config = await ConfigAggregator.create({});
42-
}
43-
const configValue = TelemetryReporter.config.getPropertyValue(SfConfigProperties.DISABLE_TELEMETRY);
44-
// SF_DISABLE_TELEMETRY is the proper name for this env that will be cheked by config.getPropertyValue. SFDX_DISABLE_INSIGHTS is present for backwards compatibility
45-
const sfdxDisableInsights = configValue === 'true' || env.getBoolean('SFDX_DISABLE_INSIGHTS');
46-
return !sfdxDisableInsights;
39+
return isEnabled();
4740
}
4841

4942
public async init(): Promise<void> {
43+
this.enabled = await isEnabled();
5044
this.logger = await Logger.child('TelemetryReporter');
51-
if (!TelemetryReporter.config) {
52-
TelemetryReporter.config = await ConfigAggregator.create({});
53-
}
54-
this.config = TelemetryReporter.config;
5545
if (this.options.waitForConnection) await this.waitForConnection();
5646
this.reporter = await AppInsights.create(this.options);
5747
}
@@ -165,15 +155,11 @@ export class TelemetryReporter extends AsyncCreatable<TelemetryOptions> {
165155
* Setting the disableTelemetry config var to true will disable insights for errors and diagnostics.
166156
*/
167157
public isSfdxTelemetryEnabled(): boolean {
168-
const configValue = this.config.getPropertyValue(SfConfigProperties.DISABLE_TELEMETRY);
169-
const sfdxDisableInsights = configValue === 'true' || env.getBoolean('SFDX_DISABLE_INSIGHTS');
170-
// isEnabled = !sfdxDisableInsights
171-
return !sfdxDisableInsights;
158+
return this.enabled;
172159
}
173160

174161
public logTelemetryStatus(): void {
175-
const isEnabled = this.isSfdxTelemetryEnabled();
176-
if (isEnabled) {
162+
if (this.enabled) {
177163
this.logger.warn(
178164
`Telemetry is enabled. This can be disabled by running sfdx force:config:set ${SfConfigProperties.DISABLE_TELEMETRY}=true`
179165
);

test/unit/telemetryReporter.test.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { expect } from 'chai';
1111
import * as sinon from 'sinon';
1212
import { AppInsights } from '../../src/appInsights';
1313
import { TelemetryReporter } from '../../src/telemetryReporter';
14+
import * as enabledStubs from '../../src/enabledCheck';
1415

1516
describe('TelemetryReporter', () => {
1617
const key = 'foo-bar-123';
@@ -75,7 +76,7 @@ describe('TelemetryReporter', () => {
7576
});
7677

7778
it('should not send a telemetry event when disabled', async () => {
78-
sandbox.stub(ConfigAggregator.prototype, 'getPropertyValue').returns('true');
79+
sandbox.stub(enabledStubs, 'isEnabled').resolves(false);
7980
const options = { project, key };
8081
const reporter = await TelemetryReporter.create(options);
8182
const sendStub = sandbox.stub(reporter.getTelemetryClient(), 'trackEvent').callsFake(() => {});
@@ -85,7 +86,7 @@ describe('TelemetryReporter', () => {
8586
});
8687

8788
it('should not send a telemetry exception when disabled', async () => {
88-
sandbox.stub(ConfigAggregator.prototype, 'getPropertyValue').returns('true');
89+
sandbox.stub(enabledStubs, 'isEnabled').resolves(false);
8990
const options = { project, key };
9091
const reporter = await TelemetryReporter.create(options);
9192
const sendStub = sandbox.stub(reporter.getTelemetryClient(), 'trackException').callsFake(() => {});
@@ -95,7 +96,7 @@ describe('TelemetryReporter', () => {
9596
});
9697

9798
it('should not send a telemetry trace when disabled', async () => {
98-
sandbox.stub(ConfigAggregator.prototype, 'getPropertyValue').returns('true');
99+
sandbox.stub(enabledStubs, 'isEnabled').resolves(false);
99100
const options = { project, key };
100101
const reporter = await TelemetryReporter.create(options);
101102
const sendStub = sandbox.stub(reporter.getTelemetryClient(), 'trackTrace').callsFake(() => {});
@@ -105,7 +106,7 @@ describe('TelemetryReporter', () => {
105106
});
106107

107108
it('should not send a telemetry metric when disabled', async () => {
108-
sandbox.stub(ConfigAggregator.prototype, 'getPropertyValue').returns('true');
109+
sandbox.stub(enabledStubs, 'isEnabled').resolves(false);
109110
const options = { project, key };
110111
const reporter = await TelemetryReporter.create(options);
111112
const sendStub = sandbox.stub(reporter.getTelemetryClient(), 'trackMetric').callsFake(() => {});
@@ -115,7 +116,7 @@ describe('TelemetryReporter', () => {
115116
});
116117

117118
it('should log to enable telemetry metric when disabled', async () => {
118-
sandbox.stub(ConfigAggregator.prototype, 'getPropertyValue').returns('true');
119+
sandbox.stub(enabledStubs, 'isEnabled').resolves(false);
119120
const warn = sandbox.stub();
120121
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
121122
sandbox.stub(Logger, 'child').resolves({ warn, debug: sandbox.stub() } as any);

0 commit comments

Comments
 (0)