Skip to content

Commit

Permalink
feat: Add support for the session init event. (#738)
Browse files Browse the repository at this point in the history
This PR adds an event which is emitted when a session is started.

It also removes some unused code related to session replay.

BEGIN_COMMIT_OVERRIDE
feat: Add support for the session init event.
chore: Remove session capture until session replay is added.
END_COMMIT_OVERRIDE
  • Loading branch information
kinyoklion authored Jan 21, 2025
1 parent 2ef1486 commit 320c07d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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),
}),
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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[],
Expand Down

0 comments on commit 320c07d

Please sign in to comment.