diff --git a/packages/telemetry/browser-telemetry/__tests__/BrowserTelemetryImpl.test.ts b/packages/telemetry/browser-telemetry/__tests__/BrowserTelemetryImpl.test.ts index 223be5bb6..1b0c866e9 100644 --- a/packages/telemetry/browser-telemetry/__tests__/BrowserTelemetryImpl.test.ts +++ b/packages/telemetry/browser-telemetry/__tests__/BrowserTelemetryImpl.test.ts @@ -67,8 +67,8 @@ it('limits pending events to maxPendingEvents', () => { telemetry.register(mockClient); - // Should only see the last 2 errors tracked - expect(mockClient.track).toHaveBeenCalledTimes(2); + // Should only see the the session init event and last 2 errors tracked + expect(mockClient.track).toHaveBeenCalledTimes(3); expect(mockClient.track).toHaveBeenCalledWith( '$ld:telemetry:error', expect.objectContaining({ @@ -522,3 +522,15 @@ it('uses the client logger when no logger is provided', () => { 'LaunchDarkly - Browser Telemetry: Error applying breadcrumb filters: Error: Filter error', ); }); + +it('sends session init event when client is registered', () => { + const telemetry = new BrowserTelemetryImpl(defaultOptions); + telemetry.register(mockClient); + + expect(mockClient.track).toHaveBeenCalledWith( + '$ld:telemetry:session:init', + expect.objectContaining({ + sessionId: expect.any(String), + }), + ); +}); diff --git a/packages/telemetry/browser-telemetry/src/BrowserTelemetryImpl.ts b/packages/telemetry/browser-telemetry/src/BrowserTelemetryImpl.ts index 977e93f39..d04bd87c8 100644 --- a/packages/telemetry/browser-telemetry/src/BrowserTelemetryImpl.ts +++ b/packages/telemetry/browser-telemetry/src/BrowserTelemetryImpl.ts @@ -28,7 +28,7 @@ import { getTraceKit } from './vendor/TraceKit'; const CUSTOM_KEY_PREFIX = '$ld:telemetry'; const ERROR_KEY = `${CUSTOM_KEY_PREFIX}:error`; -const SESSION_CAPTURE_KEY = `${CUSTOM_KEY_PREFIX}:sessionCapture`; +const SESSION_INIT_KEY = `${CUSTOM_KEY_PREFIX}:session:init`; const GENERIC_EXCEPTION = 'generic'; const NULL_EXCEPTION_MESSAGE = 'exception was null or undefined'; const MISSING_MESSAGE = 'exception had no message'; @@ -163,6 +163,9 @@ export default class BrowserTelemetryImpl implements BrowserTelemetry { // When the client is registered, we need to set the logger again, because we may be able to use the client's // logger. this._setLogger(); + + this._client.track(SESSION_INIT_KEY, { sessionId: this._sessionId }); + this._pendingEvents.forEach((event) => { this._client?.track(event.type, event.data); }); @@ -237,10 +240,6 @@ export default class BrowserTelemetryImpl implements BrowserTelemetry { this.captureError(errorEvent.error); } - captureSession(sessionEvent: EventData): void { - this._capture(SESSION_CAPTURE_KEY, { ...sessionEvent, breadcrumbs: [...this._breadcrumbs] }); - } - private _applyBreadcrumbFilters( breadcrumb: Breadcrumb, filters: BreadcrumbFilter[],