Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for the session init event. #738

Merged
merged 1 commit into from
Jan 21, 2025
Merged
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 @@ -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
Loading