Skip to content

Commit ca218da

Browse files
committed
feat: @W-20318404 Enable telemetry batching in O11yReporter
1 parent 0389b59 commit ca218da

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

packages/salesforcedx-utils-vscode/src/telemetry/reporters/o11yReporter.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class O11yReporter
2121
private o11yUploadEndpoint: string;
2222
private toDispose: Disposable[] = [];
2323
private readonly o11yService: O11yService;
24+
private batchingCleanup: (() => void) | null = null;
2425

2526
// user defined tag to add to properties that is defined via setting
2627
private telemetryTag: string | undefined;
@@ -45,6 +46,12 @@ export class O11yReporter
4546

4647
public async initialize(extensionName: string): Promise<void> {
4748
await this.o11yService.initialize(extensionName, this.o11yUploadEndpoint);
49+
50+
// Enable automatic batching with 30-second periodic flush
51+
this.batchingCleanup = this.o11yService.enableAutoBatching({
52+
flushInterval: 30_000, // 30 seconds
53+
enableShutdownHook: true, // Ensure events are flushed on shutdown
54+
});
4855
}
4956

5057
private getUserProperties(): Record<string, string> {
@@ -88,7 +95,8 @@ export class O11yReporter
8895
measurements
8996
});
9097

91-
void this.o11yService.upload();
98+
// Batching is enabled - no need to upload after each event
99+
// Events will be automatically batched and uploaded based on threshold (50KB) or periodic flush (30s)
92100
}
93101
}
94102

@@ -121,12 +129,20 @@ export class O11yReporter
121129
measurements
122130
});
123131

124-
void this.o11yService.upload();
132+
// Batching is enabled - no need to upload after each event
133+
// Events will be automatically batched and uploaded based on threshold (50KB) or periodic flush (30s)
125134
}
126135
}
127136

128137
public async dispose(): Promise<void> {
129-
await this.o11yService.upload();
138+
// Cleanup batching (removes timers and shutdown hooks)
139+
if (this.batchingCleanup) {
140+
this.batchingCleanup();
141+
this.batchingCleanup = null;
142+
}
143+
144+
// Force final flush of any remaining events
145+
await this.o11yService.forceFlush();
130146
}
131147

132148
/**

packages/salesforcedx-utils-vscode/test/jest/telemetry/reporters/o11yReporter.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ describe('O11yReporter', () => {
1919

2020
let sendMock: jest.Mock;
2121
let uploadMock: jest.Mock;
22+
let forceFlushMock: jest.Mock;
23+
let enableAutoBatchingMock: jest.Mock;
2224
let o11yReporter: O11yReporter;
2325

2426
beforeEach(() => {
@@ -32,10 +34,17 @@ describe('O11yReporter', () => {
3234
// Mock O11yService
3335
sendMock = jest.fn();
3436
uploadMock = jest.fn();
37+
forceFlushMock = jest.fn().mockResolvedValue(undefined);
38+
enableAutoBatchingMock = jest.fn().mockReturnValue(() => {
39+
// Return a cleanup function
40+
});
3541

3642
jest.spyOn(O11yService, 'getInstance').mockReturnValue({
37-
logEvent: sendMock, // Now mocks logEvent correctly
38-
upload: uploadMock // Also mocks upload to prevent dispose failure
43+
logEvent: sendMock,
44+
upload: uploadMock,
45+
forceFlush: forceFlushMock,
46+
enableAutoBatching: enableAutoBatchingMock,
47+
initialize: jest.fn().mockResolvedValue(undefined)
3948
} as any);
4049

4150
// Mock workspace config for telemetry tag

0 commit comments

Comments
 (0)