Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class O11yReporter
private o11yUploadEndpoint: string;
private toDispose: Disposable[] = [];
private readonly o11yService: O11yService;
private batchingCleanup: (() => void) | null = null;

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

public async initialize(extensionName: string): Promise<void> {
await this.o11yService.initialize(extensionName, this.o11yUploadEndpoint);

// Enable automatic batching with 30-second periodic flush
this.batchingCleanup = this.o11yService.enableAutoBatching({
flushInterval: 30_000, // 30 seconds
enableShutdownHook: true, // Ensure events are flushed on shutdown
});
}

private getUserProperties(): Record<string, string> {
Expand Down Expand Up @@ -88,7 +95,8 @@ export class O11yReporter
measurements
});

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

Expand Down Expand Up @@ -121,12 +129,20 @@ export class O11yReporter
measurements
});

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

public async dispose(): Promise<void> {
await this.o11yService.upload();
// Cleanup batching (removes timers and shutdown hooks)
if (this.batchingCleanup) {
this.batchingCleanup();
this.batchingCleanup = null;
}

// Force final flush of any remaining events
await this.o11yService.forceFlush();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ describe('O11yReporter', () => {

let sendMock: jest.Mock;
let uploadMock: jest.Mock;
let forceFlushMock: jest.Mock;
let enableAutoBatchingMock: jest.Mock;
let o11yReporter: O11yReporter;

beforeEach(() => {
Expand All @@ -32,10 +34,17 @@ describe('O11yReporter', () => {
// Mock O11yService
sendMock = jest.fn();
uploadMock = jest.fn();
forceFlushMock = jest.fn().mockResolvedValue(undefined);
enableAutoBatchingMock = jest.fn().mockReturnValue(() => {
// Return a cleanup function
});

jest.spyOn(O11yService, 'getInstance').mockReturnValue({
logEvent: sendMock, // Now mocks logEvent correctly
upload: uploadMock // Also mocks upload to prevent dispose failure
logEvent: sendMock,
upload: uploadMock,
forceFlush: forceFlushMock,
enableAutoBatching: enableAutoBatchingMock,
initialize: jest.fn().mockResolvedValue(undefined)
} as any);

// Mock workspace config for telemetry tag
Expand Down