@salesforce/telemetry 6.1.1 → 6.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -1
- package/lib/telemetryReporter.d.ts +2 -1
- package/lib/telemetryReporter.js +24 -11
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# sfdx-telemetry
|
|
2
2
|
|
|
3
|
-
This package serves an interface for [Microsoft's Application Insights npm module](https://www.npmjs.com/package/applicationinsights).
|
|
3
|
+
This package serves an interface for [Microsoft's Application Insights npm module](https://www.npmjs.com/package/applicationinsights) and supports O11y (Observability) telemetry.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -46,6 +46,36 @@ reporter.stop();
|
|
|
46
46
|
|
|
47
47
|
**Note:** For short lived processes, the telemetry can take 0-3 seconds to send all events to the server on stop, and even longer if there is a timeout. It is recommended to send telemetry in a detached spawned process. i.e. `spawn(..., { stdio: 'ignore'}).unref();`
|
|
48
48
|
|
|
49
|
+
### O11y (Observability) Telemetry
|
|
50
|
+
|
|
51
|
+
The telemetry reporter also supports O11y telemetry alongside Application Insights. To enable O11y telemetry, provide the `enableO11y` and `o11yUploadEndpoint` options:
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
const reporter = await TelemetryReporter.create({
|
|
55
|
+
project: 'my-project-name',
|
|
56
|
+
key: 'my-instrumentation-key', // Required for Application Insights
|
|
57
|
+
enableO11y: true,
|
|
58
|
+
o11yUploadEndpoint: 'https://your-o11y-endpoint.com/upload',
|
|
59
|
+
extensionName: 'my-extension' // Optional, defaults to project name
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
#### O11y-Only Mode
|
|
64
|
+
|
|
65
|
+
For O11y-only telemetry (without Application Insights), you can disable AppInsights explicitly:
|
|
66
|
+
|
|
67
|
+
```javascript
|
|
68
|
+
const reporter = await TelemetryReporter.create({
|
|
69
|
+
project: 'my-project-name',
|
|
70
|
+
enableO11y: true,
|
|
71
|
+
enableAppInsights: false, // Disable AppInsights
|
|
72
|
+
o11yUploadEndpoint: 'https://your-o11y-endpoint.com/upload',
|
|
73
|
+
extensionName: 'my-extension' // Optional, defaults to project name
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**Note:** O11y telemetry respects the same telemetry enablement settings as Application Insights. If telemetry is disabled via `SF_DISABLE_TELEMETRY` or other configuration, O11y events will not be sent.
|
|
78
|
+
|
|
49
79
|
## Env Variables
|
|
50
80
|
|
|
51
81
|
`SF_DISABLE_TELEMETRY`: Set to `true` if you want to disable telemetry.
|
|
@@ -9,8 +9,9 @@ export declare class TelemetryReporter extends AsyncCreatable<TelemetryOptions>
|
|
|
9
9
|
private enabled;
|
|
10
10
|
private options;
|
|
11
11
|
private logger;
|
|
12
|
-
private reporter
|
|
12
|
+
private reporter?;
|
|
13
13
|
private enableO11y;
|
|
14
|
+
private enableAppInsights;
|
|
14
15
|
private o11yReporter?;
|
|
15
16
|
constructor(options: TelemetryOptions);
|
|
16
17
|
/**
|
package/lib/telemetryReporter.js
CHANGED
|
@@ -61,11 +61,13 @@ class TelemetryReporter extends kit_1.AsyncCreatable {
|
|
|
61
61
|
logger;
|
|
62
62
|
reporter;
|
|
63
63
|
enableO11y;
|
|
64
|
+
enableAppInsights;
|
|
64
65
|
o11yReporter;
|
|
65
66
|
constructor(options) {
|
|
66
67
|
super(options);
|
|
67
68
|
this.options = options;
|
|
68
69
|
this.enableO11y = options.enableO11y ?? false; // default to false for backward compatibility
|
|
70
|
+
this.enableAppInsights = options.enableAppInsights ?? true; // default to true for backward compatibility
|
|
69
71
|
}
|
|
70
72
|
/**
|
|
71
73
|
* @deprecated Use the standalone function isEnabled() instead.
|
|
@@ -78,9 +80,17 @@ class TelemetryReporter extends kit_1.AsyncCreatable {
|
|
|
78
80
|
async init() {
|
|
79
81
|
this.enabled = await (0, enabledCheck_1.isEnabled)();
|
|
80
82
|
this.logger = await core_1.Logger.child('TelemetryReporter');
|
|
81
|
-
if
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
// Initialize AppInsights only if enabled and we have a valid key
|
|
84
|
+
if (this.enableAppInsights && this.options.key && this.options.key.trim() !== '') {
|
|
85
|
+
if (this.options.waitForConnection)
|
|
86
|
+
await this.waitForConnection();
|
|
87
|
+
this.reporter = await appInsights_1.AppInsights.create(this.options);
|
|
88
|
+
}
|
|
89
|
+
else if (this.enableAppInsights && (!this.options.key || this.options.key.trim() === '')) {
|
|
90
|
+
// If AppInsights is enabled but no key provided, log a warning and skip initialization
|
|
91
|
+
this.logger.warn('AppInsights is enabled but no valid key provided. Skipping AppInsights initialization.');
|
|
92
|
+
}
|
|
93
|
+
// If AppInsights is disabled, this.reporter remains undefined
|
|
84
94
|
// Only initialize O11yReporter if telemetry is enabled, enableO11y is true AND o11yUploadEndpoint is provided
|
|
85
95
|
if (this.isSfdxTelemetryEnabled() && this.enableO11y) {
|
|
86
96
|
if (this.options.o11yUploadEndpoint) {
|
|
@@ -103,14 +113,14 @@ class TelemetryReporter extends kit_1.AsyncCreatable {
|
|
|
103
113
|
* processes can call send*Event directly then finish it by TelemetryReporter.stop().
|
|
104
114
|
*/
|
|
105
115
|
start() {
|
|
106
|
-
this.reporter
|
|
116
|
+
this.reporter?.start();
|
|
107
117
|
}
|
|
108
118
|
/**
|
|
109
119
|
* Immediately flush and dispose of the reporter. This can usually take 1-3 seconds
|
|
110
120
|
* not counting timeouts.
|
|
111
121
|
*/
|
|
112
122
|
stop() {
|
|
113
|
-
this.reporter
|
|
123
|
+
this.reporter?.stop();
|
|
114
124
|
void this.o11yReporter?.flush();
|
|
115
125
|
}
|
|
116
126
|
async waitForConnection() {
|
|
@@ -155,8 +165,8 @@ class TelemetryReporter extends kit_1.AsyncCreatable {
|
|
|
155
165
|
* @param attributes {Attributes} - map of properties to publish alongside the event.
|
|
156
166
|
*/
|
|
157
167
|
sendTelemetryEvent(eventName, attributes = {}) {
|
|
158
|
-
// Send to AppInsights only if SFDX telemetry is enabled
|
|
159
|
-
if (this.isSfdxTelemetryEnabled()) {
|
|
168
|
+
// Send to AppInsights only if SFDX telemetry is enabled and AppInsights is enabled
|
|
169
|
+
if (this.isSfdxTelemetryEnabled() && this.enableAppInsights && this.reporter) {
|
|
160
170
|
this.reporter.sendTelemetryEvent(eventName, attributes);
|
|
161
171
|
}
|
|
162
172
|
// Send to O11y if telemetry is enabled and O11y is enabled
|
|
@@ -174,13 +184,13 @@ class TelemetryReporter extends kit_1.AsyncCreatable {
|
|
|
174
184
|
*/
|
|
175
185
|
sendTelemetryException(exception, attributes = {}) {
|
|
176
186
|
// Send to AppInsights only if SFDX telemetry is enabled
|
|
177
|
-
if (this.isSfdxTelemetryEnabled()) {
|
|
187
|
+
if (this.isSfdxTelemetryEnabled() && this.enableAppInsights && this.reporter) {
|
|
178
188
|
// Scrub stack for GDPR
|
|
179
189
|
const sanitizedException = new Error(exception.message);
|
|
180
190
|
sanitizedException.name = exception.name;
|
|
181
191
|
sanitizedException.stack = exception.stack?.replace(new RegExp(os.homedir(), 'g'), appInsights_1.AppInsights.GDPR_HIDDEN);
|
|
182
192
|
// Send to AppInsights
|
|
183
|
-
this.reporter
|
|
193
|
+
this.reporter?.sendTelemetryException(sanitizedException, attributes);
|
|
184
194
|
}
|
|
185
195
|
// Send to O11y if telemetry is enabled and O11y is enabled
|
|
186
196
|
if (this.isSfdxTelemetryEnabled() && this.enableO11y && this.o11yReporter) {
|
|
@@ -197,7 +207,7 @@ class TelemetryReporter extends kit_1.AsyncCreatable {
|
|
|
197
207
|
*/
|
|
198
208
|
sendTelemetryTrace(traceMessage, properties) {
|
|
199
209
|
// Send to AppInsights only if SFDX telemetry is enabled
|
|
200
|
-
if (this.isSfdxTelemetryEnabled()) {
|
|
210
|
+
if (this.isSfdxTelemetryEnabled() && this.enableAppInsights && this.reporter) {
|
|
201
211
|
// Send to AppInsights
|
|
202
212
|
this.reporter.sendTelemetryTrace(traceMessage, properties);
|
|
203
213
|
}
|
|
@@ -217,7 +227,7 @@ class TelemetryReporter extends kit_1.AsyncCreatable {
|
|
|
217
227
|
*/
|
|
218
228
|
sendTelemetryMetric(metricName, value, properties) {
|
|
219
229
|
// Send to AppInsights only if SFDX telemetry is enabled
|
|
220
|
-
if (this.isSfdxTelemetryEnabled()) {
|
|
230
|
+
if (this.isSfdxTelemetryEnabled() && this.enableAppInsights && this.reporter) {
|
|
221
231
|
// Send to AppInsights
|
|
222
232
|
this.reporter.sendTelemetryMetric(metricName, value, properties);
|
|
223
233
|
}
|
|
@@ -249,6 +259,9 @@ class TelemetryReporter extends kit_1.AsyncCreatable {
|
|
|
249
259
|
* NOT be used to send events as it will by pass disabled checks.
|
|
250
260
|
*/
|
|
251
261
|
getTelemetryClient() {
|
|
262
|
+
if (!this.reporter) {
|
|
263
|
+
throw new Error('AppInsights is not initialized. Check if enableAppInsights is true and a valid key is provided.');
|
|
264
|
+
}
|
|
252
265
|
return this.reporter.appInsightsClient;
|
|
253
266
|
}
|
|
254
267
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@salesforce/telemetry",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.2.0",
|
|
4
4
|
"description": "Library for telemetry reporting to Application Insights and O11y",
|
|
5
5
|
"main": "lib/exported",
|
|
6
6
|
"exports": {
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@salesforce/core": "^8.8.0",
|
|
45
45
|
"@salesforce/kit": "^3.2.3",
|
|
46
|
-
"@salesforce/o11y-reporter": "1.
|
|
46
|
+
"@salesforce/o11y-reporter": "1.3.0",
|
|
47
47
|
"applicationinsights": "^2.9.6",
|
|
48
48
|
"got": "^11",
|
|
49
49
|
"proxy-agent": "^6.5.0"
|