@salesforce/telemetry 6.3.0 → 6.4.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 CHANGED
@@ -113,6 +113,51 @@ reporter.sendTelemetryEvent('event-name', { foo: 'bar' });
113
113
 
114
114
  **Note:** The `o11y_schema` package doesn't provide TypeScript declarations. If your TypeScript configuration doesn't include `skipLibCheck: true`, you may need to add type declarations or use `@ts-expect-error` comments. However, with proper TypeScript configuration, the import should work without additional annotations.
115
115
 
116
+ #### O11y Telemetry Batching
117
+
118
+ O11y telemetry supports automatic batching of events to improve performance and reduce network overhead. By default, batching is enabled with a 30-second flush interval. Events are automatically uploaded when:
119
+ - The buffer reaches 50KB in size, or
120
+ - The flush interval (default: 30 seconds) elapses, or
121
+ - The process exits (via shutdown hooks)
122
+
123
+ You can configure batching options during initialization:
124
+
125
+ ```javascript
126
+ const reporter = await TelemetryReporter.create({
127
+ project: 'my-project-name',
128
+ enableO11y: true,
129
+ o11yUploadEndpoint: 'https://your-o11y-endpoint.com/upload',
130
+ o11yBatching: {
131
+ enableAutoBatching: true, // Default: true
132
+ flushInterval: 60_000, // 60 seconds (default: 30_000)
133
+ enableShutdownHook: true, // Default: true
134
+ enableBeforeExitHook: true // Default: true (may not fire for STDIO servers)
135
+ }
136
+ });
137
+ ```
138
+
139
+ To disable batching and upload events immediately after each event:
140
+
141
+ ```javascript
142
+ const reporter = await TelemetryReporter.create({
143
+ project: 'my-project-name',
144
+ enableO11y: true,
145
+ o11yUploadEndpoint: 'https://your-o11y-endpoint.com/upload',
146
+ o11yBatching: {
147
+ enableAutoBatching: false // Events will be uploaded immediately
148
+ }
149
+ });
150
+ ```
151
+
152
+ You can also manually flush buffered events when needed (e.g., before critical operations or shutdown):
153
+
154
+ ```javascript
155
+ // Manually flush buffered events
156
+ await reporter.flush();
157
+ ```
158
+
159
+ **Note:** When batching is disabled, events are uploaded immediately after each `sendTelemetryEvent()`, `sendTelemetryException()`, `sendTelemetryTrace()`, or `sendTelemetryMetric()` call for backward compatibility.
160
+
116
161
  ## Env Variables
117
162
 
118
163
  `SF_DISABLE_TELEMETRY`: Set to `true` if you want to disable telemetry.
package/lib/exported.d.ts CHANGED
@@ -2,4 +2,4 @@ import { TelemetryReporter } from './telemetryReporter';
2
2
  export * from './telemetryReporter';
3
3
  export { isEnabled } from './enabledCheck';
4
4
  export default TelemetryReporter;
5
- export type { Attributes, O11ySchema } from './types';
5
+ export type { Attributes, O11ySchema, O11yBatchingConfig } from './types';
@@ -1,3 +1,4 @@
1
+ import { type BatchingOptions } from '@salesforce/o11y-reporter';
1
2
  import { Attributes, O11ySchema, Properties, TelemetryOptions } from './types';
2
3
  import { BaseReporter } from './baseReporter';
3
4
  export declare class O11yReporter extends BaseReporter {
@@ -6,8 +7,34 @@ export declare class O11yReporter extends BaseReporter {
6
7
  private commonProperties;
7
8
  private extensionName;
8
9
  private customSchema;
10
+ private _batchingCleanup;
11
+ private _batchingEnabled;
9
12
  constructor(options: TelemetryOptions);
10
13
  init(): Promise<void>;
14
+ /**
15
+ * Enable automatic batching with periodic flush and shutdown hooks
16
+ *
17
+ * Consumers can call this method to enable batching with custom options.
18
+ * If batching is not enabled, events will be buffered but not automatically uploaded.
19
+ * Use flush() to manually upload events when batching is disabled.
20
+ *
21
+ * @param options - Batching configuration options
22
+ * @returns Cleanup function to stop batching and remove hooks
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * await reporter.init();
27
+ * const cleanup = reporter.enableAutoBatching({
28
+ * flushInterval: 30_000, // 30 seconds
29
+ * enableShutdownHook: true,
30
+ * });
31
+ * ```
32
+ */
33
+ enableAutoBatching(options?: BatchingOptions): () => void;
34
+ /**
35
+ * Check if auto-batching is currently enabled
36
+ */
37
+ isBatchingEnabled(): boolean;
11
38
  sendTelemetryEvent(eventName: string, attributes?: Attributes): Promise<void>;
12
39
  flush(): Promise<void>;
13
40
  /**
@@ -25,6 +25,8 @@ class O11yReporter extends baseReporter_1.BaseReporter {
25
25
  commonProperties;
26
26
  extensionName = '';
27
27
  customSchema = null; // Schema object provided by consumer
28
+ _batchingCleanup = null; // Cleanup function for auto-batching
29
+ _batchingEnabled = false; // Track if batching is enabled
28
30
  constructor(options) {
29
31
  super(options);
30
32
  this.extensionName = options.extensionName ?? options.project;
@@ -37,6 +39,36 @@ class O11yReporter extends baseReporter_1.BaseReporter {
37
39
  async init() {
38
40
  await this.initialized;
39
41
  }
42
+ /**
43
+ * Enable automatic batching with periodic flush and shutdown hooks
44
+ *
45
+ * Consumers can call this method to enable batching with custom options.
46
+ * If batching is not enabled, events will be buffered but not automatically uploaded.
47
+ * Use flush() to manually upload events when batching is disabled.
48
+ *
49
+ * @param options - Batching configuration options
50
+ * @returns Cleanup function to stop batching and remove hooks
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * await reporter.init();
55
+ * const cleanup = reporter.enableAutoBatching({
56
+ * flushInterval: 30_000, // 30 seconds
57
+ * enableShutdownHook: true,
58
+ * });
59
+ * ```
60
+ */
61
+ enableAutoBatching(options) {
62
+ this._batchingEnabled = true;
63
+ this._batchingCleanup = this.service.enableAutoBatching(options);
64
+ return this._batchingCleanup;
65
+ }
66
+ /**
67
+ * Check if auto-batching is currently enabled
68
+ */
69
+ isBatchingEnabled() {
70
+ return this._batchingEnabled;
71
+ }
40
72
  async sendTelemetryEvent(eventName, attributes = {}) {
41
73
  // Wait for initialization to complete before using the service
42
74
  await this.initialized;
@@ -53,12 +85,18 @@ class O11yReporter extends baseReporter_1.BaseReporter {
53
85
  else {
54
86
  this.service.logEvent(eventData);
55
87
  }
56
- await this.service.upload();
88
+ // If batching is not enabled, upload immediately for backward compatibility
89
+ if (!this._batchingEnabled) {
90
+ await this.service.forceFlush();
91
+ }
92
+ // If batching is enabled, events will be automatically uploaded based on
93
+ // threshold (50KB) or periodic flush interval.
57
94
  }
58
95
  async flush() {
59
96
  // Wait for initialization to complete before using the service
60
97
  await this.initialized;
61
- await this.service.upload();
98
+ // Use forceFlush for explicit manual flush
99
+ await this.service.forceFlush();
62
100
  }
63
101
  /**
64
102
  * Publishes exception to O11y service
@@ -87,7 +125,12 @@ class O11yReporter extends baseReporter_1.BaseReporter {
87
125
  else {
88
126
  this.service.logEvent(exceptionEvent);
89
127
  }
90
- await this.service.upload();
128
+ // If batching is not enabled, upload immediately for backward compatibility
129
+ if (!this._batchingEnabled) {
130
+ await this.service.forceFlush();
131
+ }
132
+ // If batching is enabled, events will be automatically uploaded based on
133
+ // threshold (50KB) or periodic flush interval.
91
134
  }
92
135
  /**
93
136
  * Publishes diagnostic information to O11y service
@@ -98,18 +141,21 @@ class O11yReporter extends baseReporter_1.BaseReporter {
98
141
  async sendTelemetryTrace(traceMessage, properties = {}) {
99
142
  await this.initialized;
100
143
  const merged = { ...this.commonProperties, ...properties };
101
- const eventData = {
144
+ const traceEvent = {
102
145
  eventName: `${this.extensionName}/trace`,
103
146
  message: traceMessage,
104
147
  ...merged,
105
148
  };
106
149
  if (this.customSchema) {
107
- this.service.logEventWithSchema(eventData, this.customSchema);
150
+ this.service.logEventWithSchema(traceEvent, this.customSchema);
108
151
  }
109
152
  else {
110
- this.service.logEvent(eventData);
153
+ this.service.logEvent(traceEvent);
154
+ }
155
+ // If batching is not enabled, upload immediately for backward compatibility
156
+ if (!this._batchingEnabled) {
157
+ await this.service.forceFlush();
111
158
  }
112
- await this.service.upload();
113
159
  }
114
160
  /**
115
161
  * Publishes metric to O11y service
@@ -121,19 +167,22 @@ class O11yReporter extends baseReporter_1.BaseReporter {
121
167
  async sendTelemetryMetric(metricName, value, properties = {}) {
122
168
  await this.initialized;
123
169
  const merged = { ...this.commonProperties, ...properties };
124
- const eventData = {
170
+ const metricEvent = {
125
171
  eventName: `${this.extensionName}/metric`,
126
172
  metricName,
127
173
  value,
128
174
  ...merged,
129
175
  };
130
176
  if (this.customSchema) {
131
- this.service.logEventWithSchema(eventData, this.customSchema);
177
+ this.service.logEventWithSchema(metricEvent, this.customSchema);
132
178
  }
133
179
  else {
134
- this.service.logEvent(eventData);
180
+ this.service.logEvent(metricEvent);
181
+ }
182
+ // If batching is not enabled, upload immediately for backward compatibility
183
+ if (!this._batchingEnabled) {
184
+ await this.service.forceFlush();
135
185
  }
136
- await this.service.upload();
137
186
  }
138
187
  /**
139
188
  * Gets the currently loaded schema object
@@ -1,4 +1,5 @@
1
1
  import { AsyncCreatable } from '@salesforce/kit';
2
+ import type { BatchingOptions } from '@salesforce/o11y-reporter';
2
3
  import { TelemetryClient } from './appInsights';
3
4
  import { Attributes, Properties, TelemetryOptions } from './types';
4
5
  /**
@@ -74,4 +75,42 @@ export declare class TelemetryReporter extends AsyncCreatable<TelemetryOptions>
74
75
  * NOT be used to send events as it will by pass disabled checks.
75
76
  */
76
77
  getTelemetryClient(): TelemetryClient;
78
+ /**
79
+ * Enable automatic batching for O11y telemetry events.
80
+ *
81
+ * This method allows consumers to configure batching options for O11y telemetry.
82
+ * If batching is not enabled, events will be buffered but not automatically uploaded.
83
+ * Use flush() to manually upload events when batching is disabled.
84
+ *
85
+ * Note: Auto-batching is enabled by default with 30-second flush interval during init().
86
+ * Calling this method will override the default batching configuration.
87
+ *
88
+ * @param options - Batching configuration options
89
+ * @returns Cleanup function to stop batching and remove hooks, or undefined if O11y is not enabled
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * const cleanup = reporter.enableAutoBatching({
94
+ * flushInterval: 60_000, // 60 seconds
95
+ * enableShutdownHook: true,
96
+ * });
97
+ * ```
98
+ */
99
+ enableAutoBatching(options?: BatchingOptions): (() => void) | undefined;
100
+ /**
101
+ * Force an immediate flush of buffered O11y telemetry events.
102
+ *
103
+ * This method triggers an immediate upload of all currently buffered telemetry events.
104
+ * It's useful for ensuring critical events are sent immediately, or for
105
+ * flushing remaining events before an application exits.
106
+ *
107
+ * @returns Promise that resolves when the upload completes, or undefined if O11y is not enabled
108
+ *
109
+ * @example
110
+ * ```typescript
111
+ * // Flush events before critical operation
112
+ * await reporter.flush();
113
+ * ```
114
+ */
115
+ flush(): Promise<void>;
77
116
  }
@@ -105,7 +105,25 @@ class TelemetryReporter extends kit_1.AsyncCreatable {
105
105
  if (this.options.o11yUploadEndpoint) {
106
106
  try {
107
107
  this.o11yReporter = new o11yReporter_1.O11yReporter(this.options);
108
- this.logger.debug('O11y reporter initialized successfully');
108
+ await this.o11yReporter.init();
109
+ // Configure batching - enabled by default unless explicitly disabled
110
+ const batchingConfig = this.options.o11yBatching;
111
+ // Batching is enabled by default. Only disable if explicitly set to false
112
+ const enableAutoBatching = batchingConfig?.enableAutoBatching !== false;
113
+ if (enableAutoBatching) {
114
+ // Enable auto-batching with provided options or defaults
115
+ const batchingOptions = {
116
+ flushInterval: batchingConfig?.flushInterval ?? 30_000, // 30 seconds default
117
+ enableShutdownHook: batchingConfig?.enableShutdownHook ?? true,
118
+ enableBeforeExitHook: batchingConfig?.enableBeforeExitHook,
119
+ };
120
+ this.o11yReporter.enableAutoBatching(batchingOptions);
121
+ this.logger.debug('O11y reporter initialized with auto-batching enabled (default)');
122
+ }
123
+ else {
124
+ // Batching explicitly disabled - events will be uploaded immediately after each event
125
+ this.logger.debug('O11y reporter initialized with auto-batching disabled (immediate uploads)');
126
+ }
109
127
  }
110
128
  catch (error) {
111
129
  this.logger.warn('Failed to initialize O11y reporter:', error);
@@ -273,6 +291,56 @@ class TelemetryReporter extends kit_1.AsyncCreatable {
273
291
  }
274
292
  return this.reporter.appInsightsClient;
275
293
  }
294
+ /**
295
+ * Enable automatic batching for O11y telemetry events.
296
+ *
297
+ * This method allows consumers to configure batching options for O11y telemetry.
298
+ * If batching is not enabled, events will be buffered but not automatically uploaded.
299
+ * Use flush() to manually upload events when batching is disabled.
300
+ *
301
+ * Note: Auto-batching is enabled by default with 30-second flush interval during init().
302
+ * Calling this method will override the default batching configuration.
303
+ *
304
+ * @param options - Batching configuration options
305
+ * @returns Cleanup function to stop batching and remove hooks, or undefined if O11y is not enabled
306
+ *
307
+ * @example
308
+ * ```typescript
309
+ * const cleanup = reporter.enableAutoBatching({
310
+ * flushInterval: 60_000, // 60 seconds
311
+ * enableShutdownHook: true,
312
+ * });
313
+ * ```
314
+ */
315
+ enableAutoBatching(options) {
316
+ if (!this.o11yReporter) {
317
+ this.logger.debug('O11y reporter is not initialized. enableAutoBatching() has no effect.');
318
+ return undefined;
319
+ }
320
+ return this.o11yReporter.enableAutoBatching(options);
321
+ }
322
+ /**
323
+ * Force an immediate flush of buffered O11y telemetry events.
324
+ *
325
+ * This method triggers an immediate upload of all currently buffered telemetry events.
326
+ * It's useful for ensuring critical events are sent immediately, or for
327
+ * flushing remaining events before an application exits.
328
+ *
329
+ * @returns Promise that resolves when the upload completes, or undefined if O11y is not enabled
330
+ *
331
+ * @example
332
+ * ```typescript
333
+ * // Flush events before critical operation
334
+ * await reporter.flush();
335
+ * ```
336
+ */
337
+ async flush() {
338
+ if (!this.o11yReporter) {
339
+ this.logger.debug('O11y reporter is not initialized. flush() has no effect.');
340
+ return;
341
+ }
342
+ await this.o11yReporter.flush();
343
+ }
276
344
  }
277
345
  exports.TelemetryReporter = TelemetryReporter;
278
346
  //# sourceMappingURL=telemetryReporter.js.map
package/lib/types.d.ts CHANGED
@@ -15,6 +15,32 @@ export type Attributes = {
15
15
  * (not a primitive, null, or undefined).
16
16
  */
17
17
  export type O11ySchema = Record<string, unknown>;
18
+ /**
19
+ * Batching configuration for O11y telemetry
20
+ *
21
+ * Batching is enabled by default. Set enableAutoBatching to false to disable batching
22
+ * and upload events immediately after each event.
23
+ */
24
+ export type O11yBatchingConfig = {
25
+ /**
26
+ * Enable automatic batching of events (default: true)
27
+ * Set to false to disable batching and upload events immediately after each event.
28
+ * If not specified, batching is enabled by default.
29
+ */
30
+ enableAutoBatching?: boolean;
31
+ /**
32
+ * Periodic flush interval in milliseconds (default: 30000)
33
+ */
34
+ flushInterval?: number;
35
+ /**
36
+ * Enable shutdown hooks (default: true)
37
+ */
38
+ enableShutdownHook?: boolean;
39
+ /**
40
+ * Enable beforeExit hook (default: true). Note: beforeExit won't fire for STDIO servers where stdin stays open
41
+ */
42
+ enableBeforeExitHook?: boolean;
43
+ };
18
44
  export type TelemetryOptions = {
19
45
  project: string;
20
46
  key: string;
@@ -30,4 +56,10 @@ export type TelemetryOptions = {
30
56
  enableAppInsights?: boolean;
31
57
  extensionName?: string;
32
58
  o11ySchema?: O11ySchema;
59
+ /**
60
+ * Batching configuration for O11y telemetry
61
+ * Batching is enabled by default. Set o11yBatching.enableAutoBatching to false to disable batching
62
+ * and upload events immediately after each event.
63
+ */
64
+ o11yBatching?: O11yBatchingConfig;
33
65
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/telemetry",
3
- "version": "6.3.0",
3
+ "version": "6.4.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.23.4",
45
45
  "@salesforce/kit": "^3.2.4",
46
- "@salesforce/o11y-reporter": "1.4.0",
46
+ "@salesforce/o11y-reporter": "1.6.0",
47
47
  "applicationinsights": "^2.9.8",
48
48
  "got": "^11",
49
49
  "proxy-agent": "^6.5.0"