Skip to content
Draft
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
55 changes: 54 additions & 1 deletion packages/browser/src/integrations/browsersession.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { captureSession, debug, defineIntegration, getIsolationScope, startSession } from '@sentry/core/browser';
import {
captureSession,
debug,
defineIntegration,
getIsolationScope,
SEMANTIC_ATTRIBUTE_SESSION_ID,
startSession,
} from '@sentry/core/browser';
import { addHistoryInstrumentationHandler } from '@sentry-internal/browser-utils';
import { DEBUG_BUILD } from '../debug-build';
import { WINDOW } from '../helpers';
Expand Down Expand Up @@ -29,6 +36,52 @@ export const browserSessionIntegration = defineIntegration((options: BrowserSess

return {
name: 'BrowserSession',
setup(client) {
function attachSessionId<T extends { attributes?: Record<string, unknown> | undefined }>(telemetryItem: T): T {
const session = getIsolationScope().getSession();
const attributes = telemetryItem.attributes ?? (telemetryItem.attributes = {});
if (session?.sid && !attributes?.[SEMANTIC_ATTRIBUTE_SESSION_ID]) {
attributes[SEMANTIC_ATTRIBUTE_SESSION_ID] = session.sid;
}
return telemetryItem;
}

client.on('processMetric', attachSessionId);
client.on('beforeCaptureLog', attachSessionId);
// only applies to streamed spans
client.on('processSpan', attachSessionId);

// for errors and transactions (non-streamed spans)
client.addEventProcessor(event => {
if (event.type && event.type !== 'transaction') {
// ignore other events than errors and transactions for now
return event;
}

const sessionId = getIsolationScope().getSession()?.sid;
if (!sessionId) {
return event;
}

// set a session context on the event. Relay will extract the `session.id`
// tag from this context which will make it queryable in the UI.
event.contexts = {
session: {
id: sessionId,
},
...event.contexts,
};

event.spans?.forEach(span => {
span.data = {
[SEMANTIC_ATTRIBUTE_SESSION_ID]: sessionId,
...span.data,
};
});

return event;
});
},
setupOnce() {
if (typeof WINDOW.document === 'undefined') {
DEBUG_BUILD &&
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/semanticAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,5 @@ export const SEMANTIC_LINK_ATTRIBUTE_LINK_TYPE = 'sentry.link.type';
* For LangGraph: configurable.thread_id
*/
export const GEN_AI_CONVERSATION_ID_ATTRIBUTE = 'gen_ai.conversation.id';

export const SEMANTIC_ATTRIBUTE_SESSION_ID = 'session.id';
5 changes: 5 additions & 0 deletions packages/core/src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Contexts extends Record<string, Context | undefined> {
state?: StateContext;
profile?: ProfileContext;
flags?: FeatureFlagContext;
session?: SessionContext;
}

export interface StateContext extends Record<string, unknown> {
Expand Down Expand Up @@ -139,3 +140,7 @@ export interface MissingInstrumentationContext extends Record<string, unknown> {
export interface FeatureFlagContext extends Record<string, unknown> {
values: FeatureFlag[];
}

export interface SessionContext extends Record<string, unknown> {
id?: string;
}
Loading