Skip to content

Commit ce2e0e8

Browse files
Merge pull request #483 from forcedotcom/W-21438157-stopAsync-o11y-batching-cleanup
feat: @W-21438157 - add O11yReporter.stop() and TelemetryReporter.stopAsync() for O11y batching cleanup
2 parents 7db06c5 + 7dd9fa7 commit ce2e0e8

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ reporter.sendTelemetryEvent('event-name', { foo: 'bar', executionTime: 0.5912 })
4444
reporter.stop();
4545
```
4646

47+
To ensure all buffered events are flushed before process exit (e.g. when using O11y batching), use `stopAsync()`:
48+
49+
```javascript
50+
// Await shutdown so all events are sent before exiting
51+
await reporter.stopAsync();
52+
process.exit(0);
53+
```
54+
4755
**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();`
4856

4957
### O11y (Observability) Telemetry
@@ -159,6 +167,14 @@ You can also manually flush buffered events when needed (e.g., before critical o
159167
await reporter.flush();
160168
```
161169

170+
For graceful shutdown that stops the batching interval and flushes all events, use `stopAsync()`:
171+
172+
```javascript
173+
// Stop batching, flush remaining events, and dispose
174+
await reporter.stopAsync();
175+
process.exit(0);
176+
```
177+
162178
**Note:** When batching is disabled, events are uploaded immediately after each `sendTelemetryEvent()`, `sendTelemetryException()`, `sendTelemetryTrace()`, or `sendTelemetryMetric()` call for backward compatibility.
163179

164180
## Env Variables

src/o11yReporter.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,26 @@ export class O11yReporter extends BaseReporter {
162162
await this.service.forceFlush();
163163
}
164164

165+
/**
166+
* Stop batching (if enabled), flush any remaining events, and clean up.
167+
* Call this before process exit to ensure all events are sent and timers are cleared.
168+
*
169+
* @example
170+
* ```typescript
171+
* await reporter.stop();
172+
* process.exit(0);
173+
* ```
174+
*/
175+
public async stop(): Promise<void> {
176+
await this.initialized;
177+
if (this._batchingCleanup) {
178+
this._batchingCleanup();
179+
this._batchingCleanup = null;
180+
this._batchingEnabled = false;
181+
}
182+
await this.service.forceFlush();
183+
}
184+
165185
/**
166186
* Publishes exception to O11y service
167187
*

src/telemetryReporter.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,22 @@ export class TelemetryReporter extends AsyncCreatable<TelemetryOptions> {
119119
*/
120120
public stop(): void {
121121
this.reporter?.stop();
122-
void this.o11yReporter?.flush();
122+
void this.o11yReporter?.stop();
123+
}
124+
125+
/**
126+
* Async version of stop() that awaits O11y cleanup (batching interval stop + flush).
127+
* Use this when you need to ensure telemetry is fully shut down before process exit.
128+
*
129+
* @example
130+
* ```typescript
131+
* await reporter.stopAsync();
132+
* process.exit(0);
133+
* ```
134+
*/
135+
public async stopAsync(): Promise<void> {
136+
this.reporter?.stop();
137+
await this.o11yReporter?.stop();
123138
}
124139

125140
public async waitForConnection(): Promise<void> {

0 commit comments

Comments
 (0)