Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(logs): honor configured session id keys in session detection (#70736)

Jon McCallum72841dcd4a6e
diff --git a/products/logs/frontend/components/LogsViewer/LogAttributes.tsx b/products/logs/frontend/components/LogsViewer/LogAttributes.tsx
index c1c67dcbb768..0c41ee706dc2 100644
--- a/products/logs/frontend/components/LogsViewer/LogAttributes.tsx
+++ b/products/logs/frontend/components/LogsViewer/LogAttributes.tsx
@@ -16,6 +16,7 @@ import { PropertyFilterType, PropertyOperator } from '~/types'
import { AttributeBreakdowns } from 'products/logs/frontend/AttributeBreakdowns'
import { logsViewerLogic } from 'products/logs/frontend/components/LogsViewer/logsViewerLogic'
+import { logsConfigLogic } from 'products/logs/frontend/logsConfigLogic'
import { isDistinctIdKey, isSessionIdKey } from 'products/logs/frontend/utils'
export interface LogAttributesProps {
@@ -27,6 +28,7 @@ export interface LogAttributesProps {
export function LogAttributes({ attributes, type, logUuid, title }: LogAttributesProps): JSX.Element {
const { expandedAttributeBreakdowns, id, isAttributeColumn } = useValues(logsViewerLogic)
+ const { configuredSessionIdKeys } = useValues(logsConfigLogic)
const { addFilter, toggleAttributeColumn, toggleAttributeBreakdown } = useActions(logsViewerLogic)
const expandedBreakdownsForThisLog = expandedAttributeBreakdowns[logUuid] || []
@@ -138,7 +140,7 @@ export function LogAttributes({ attributes, type, logUuid, title }: LogAttribute
<span onClick={(e) => e.stopPropagation()}>
<PersonDisplay person={{ distinct_id: record.value }} noEllipsis inline />
</span>
- ) : isSessionIdKey(record.key) ? (
+ ) : isSessionIdKey(record.key, configuredSessionIdKeys) && record.value ? (
<ViewRecordingButton
sessionId={record.value}
openPlayerIn={RecordingPlayerType.Modal}
diff --git a/products/logs/frontend/components/LogsViewer/LogContextSelector/LogContextSelector.tsx b/products/logs/frontend/components/LogsViewer/LogContextSelector/LogContextSelector.tsx
index d6f8decaf0f0..9a22699d437a 100644
--- a/products/logs/frontend/components/LogsViewer/LogContextSelector/LogContextSelector.tsx
+++ b/products/logs/frontend/components/LogsViewer/LogContextSelector/LogContextSelector.tsx
@@ -1,9 +1,10 @@
-import { useActions } from 'kea'
+import { useActions, useValues } from 'kea'
import posthog from 'posthog-js'
import { IconList } from '@posthog/icons'
import { LemonButton, LemonMenu } from '@posthog/lemon-ui'
+import { logsConfigLogic } from 'products/logs/frontend/logsConfigLogic'
import { ParsedLogMessage } from 'products/logs/frontend/types'
import { logDetailsModalLogic } from '../LogDetailsModal/logDetailsModalLogic'
@@ -19,8 +20,9 @@ interface LogContextSelectorProps {
export function LogContextSelector({ log, size = 'xsmall', noPadding }: LogContextSelectorProps): JSX.Element | null {
const { openLogsViewerModal } = useActions(logsViewerModalLogic)
const { closeLogDetails } = useActions(logDetailsModalLogic)
+ const { configuredSessionIdKeys } = useValues(logsConfigLogic)
- const contexts = getAvailableContexts(log)
+ const contexts = getAvailableContexts(log, configuredSessionIdKeys)
if (contexts.length === 0) {
return null
}
@@ -33,7 +35,7 @@ export function LogContextSelector({ log, size = 'xsmall', noPadding }: LogConte
onClick: () => {
posthog.capture('logs context viewed', { context_type: ctx.type })
closeLogDetails()
- const filters = buildContextFilters(log, ctx.type)
+ const filters = buildContextFilters(log, ctx.type, configuredSessionIdKeys)
openLogsViewerModal({
id: `context-${log.uuid}-${ctx.type}`,
fullScreen: false,
diff --git a/products/logs/frontend/components/LogsViewer/LogContextSelector/logContextUtils.test.ts b/products/logs/frontend/components/LogsViewer/LogContextSelector/logContextUtils.test.ts
index 5e8003b28846..2c684c287b50 100644
--- a/products/logs/frontend/components/LogsViewer/LogContextSelector/logContextUtils.test.ts
+++ b/products/logs/frontend/components/LogsViewer/LogContextSelector/logContextUtils.test.ts
@@ -110,4 +110,14 @@ describe('buildContextFilters', () => {
expect(filters).toEqual(expect.objectContaining(expected))
}
)
+
+ it('session filter uses the matched configured key', () => {
+ const customLog = makeLog({ timestamp, attributes: { 'my.custom.key': 'sess-456' } })
+ const filters = buildContextFilters(customLog, 'session', ['my.custom.key'])
+ expect(filters).toEqual(
+ expect.objectContaining({
+ filterGroup: makeFilterGroup('my.custom.key', 'sess-456', PropertyFilterType.LogAttribute),
+ })
+ )
+ })
})
diff --git a/products/logs/frontend/components/LogsViewer/LogContextSelector/logContextUtils.ts b/products/logs/frontend/components/LogsViewer/LogContextSelector/logContextUtils.ts
index 29e58816aa49..d0f5833bfab3 100644
--- a/products/logs/frontend/components/LogsViewer/LogContextSelector/logContextUtils.ts
+++ b/products/logs/frontend/components/LogsViewer/LogContextSelector/logContextUtils.ts
@@ -4,7 +4,7 @@ import { FilterLogicalOperator, PropertyFilterType, PropertyOperator, UniversalF
import { LogsViewerFilters } from 'products/logs/frontend/components/LogsViewer/config/types'
import { ParsedLogMessage } from 'products/logs/frontend/types'
-import { getSessionIdWithKey } from 'products/logs/frontend/utils'
+import { SessionIdMatch, getSessionIdWithKey } from 'products/logs/frontend/utils'
export type LogContextType = 'surrounding_service' | 'surrounding_all' | 'trace' | 'session'
@@ -24,10 +24,8 @@ function getServiceName(log: ParsedLogMessage): string | null {
return serviceName ? String(serviceName) : null
}
-function getSessionMatch(
- log: ParsedLogMessage
-): { key: string; value: string; source: 'attribute' | 'resource_attribute' } | null {
- return getSessionIdWithKey(log.attributes, log.resource_attributes as Record<string, unknown> | undefined)
+function getSessionMatch(log: ParsedLogMessage, configuredKeys?: string[]): SessionIdMatch | null {
+ return getSessionIdWithKey(log.attributes, log.resource_attributes, configuredKeys)
}
function buildDateRangeAround(timestamp: string, windowMinutes: number): { date_from: string; date_to: string } {
@@ -57,7 +55,7 @@ function buildFilterGroup(key: string, value: string, propertyType: PropertyFilt
}
}
-export function getAvailableContexts(log: ParsedLogMessage): LogContextOption[] {
+export function getAvailableContexts(log: ParsedLogMessage, configuredKeys?: string[]): LogContextOption[] {
const contexts: LogContextOption[] = []
const serviceName = getServiceName(log)
@@ -83,7 +81,7 @@ export function getAvailableContexts(log: ParsedLogMessage): LogContextOption[]
})
}
- if (getSessionMatch(log)) {
+ if (getSessionMatch(log, configuredKeys)) {
contexts.push({
type: 'session',
label: 'View session logs',
@@ -94,7 +92,11 @@ export function getAvailableContexts(log: ParsedLogMessage): LogContextOption[]
return contexts
}
-export function buildContextFilters(log: ParsedLogMessage, contextType: LogContextType): Partial<LogsViewerFilters> {
+export function buildContextFilters(
+ log: ParsedLogMessage,
+ contextType: LogContextType,
+ configuredKeys?: string[]
+): Partial<LogsViewerFilters> {
const base: Pick<LogsViewerFilters, 'searchTerm' | 'severityLevels'> = {
searchTerm: '',
severityLevels: [],
@@ -124,7 +126,7 @@ export function buildContextFilters(log: ParsedLogMessage, contextType: LogConte
}
}
case 'session': {
- const session = getSessionMatch(log)
+ const session = getSessionMatch(log, configuredKeys)
return {
...base,
dateRange: buildDateRangeAround(log.timestamp, SESSION_WINDOW_MINUTES),
diff --git a/products/logs/frontend/components/LogsViewer/LogDetailsModal/logDetailsModalLogic.ts b/products/logs/frontend/components/LogsViewer/LogDetailsModal/logDetailsModalLogic.ts
index 188a1d0409f6..159d8431c95d 100644
--- a/products/logs/frontend/components/LogsViewer/LogDetailsModal/logDetailsModalLogic.ts
+++ b/products/logs/frontend/components/LogsViewer/LogDetailsModal/logDetailsModalLogic.ts
@@ -1,7 +1,8 @@
-import { actions, kea, key, listeners, path, props, reducers, selectors } from 'kea'
+import { actions, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { subscriptions } from 'kea-subscriptions'
import posthog from 'posthog-js'
+import { logsConfigLogic } from 'products/logs/frontend/logsConfigLogic'
import { ParsedLogMessage } from 'products/logs/frontend/types'
import { getSessionIdFromLogAttributes } from 'products/logs/frontend/utils'
@@ -17,6 +18,7 @@ export const logDetailsModalLogic = kea<logDetailsModalLogicType>([
path(['products', 'logs', 'frontend', 'components', 'LogsViewer', 'LogDetailsModal', 'logDetailsModalLogic']),
props({} as LogDetailsModalProps),
key((props) => props.id),
+ connect({ values: [logsConfigLogic, ['configuredSessionIdKeys']] }),
actions({
openLogDetails: (log: ParsedLogMessage) => ({ log }),
@@ -72,10 +74,14 @@ export const logDetailsModalLogic = kea<logDetailsModalLogicType>([
selectors({
sessionId: [
- (s) => [s.selectedLog],
- (selectedLog): string | null =>
+ (s) => [s.selectedLog, s.configuredSessionIdKeys],
+ (selectedLog, configuredSessionIdKeys): string | null =>
selectedLog
- ? getSessionIdFromLogAttributes(selectedLog.attributes, selectedLog.resource_attributes)
+ ? getSessionIdFromLogAttributes(
+ selectedLog.attributes,
+ selectedLog.resource_attributes,
+ configuredSessionIdKeys
+ )
: null,
],
hasSessionId: [(s) => [s.sessionId], (sessionId): boolean => sessionId !== null],
diff --git a/products/logs/frontend/components/LogsViewer/LogRowFAB/LogRowFAB.tsx b/products/logs/frontend/components/LogsViewer/LogRowFAB/LogRowFAB.tsx
index 7b75119f02af..8fbe24230c6a 100644
--- a/products/logs/frontend/components/LogsViewer/LogRowFAB/LogRowFAB.tsx
+++ b/products/logs/frontend/components/LogsViewer/LogRowFAB/LogRowFAB.tsx
@@ -12,6 +12,7 @@ import { LogContextSelector } from 'products/logs/frontend/components/LogsViewer
import { logDetailsModalLogic } from 'products/logs/frontend/components/LogsViewer/LogDetailsModal'
import { logsViewerLogic } from 'products/logs/frontend/components/LogsViewer/logsViewerLogic'
import { useCellScrollControls } from 'products/logs/frontend/components/VirtualizedLogsList/useCellScroll'
+import { logsConfigLogic } from 'products/logs/frontend/logsConfigLogic'
import { ParsedLogMessage } from 'products/logs/frontend/types'
import { getSessionIdFromLogAttributes } from 'products/logs/frontend/utils'
@@ -35,10 +36,11 @@ export function LogRowFAB({
showScrollButtons = false,
}: LogRowFABProps): JSX.Element {
const { id } = useValues(logsViewerLogic)
+ const { configuredSessionIdKeys } = useValues(logsConfigLogic)
const { copyLinkToLog } = useActions(logsViewerLogic)
const { openLogDetails } = useActions(logDetailsModalLogic)
const { startScrolling, stopScrolling } = useCellScrollControls({ id, cellKey: 'message' })
- const sessionId = getSessionIdFromLogAttributes(log.attributes, log.resource_attributes)
+ const sessionId = getSessionIdFromLogAttributes(log.attributes, log.resource_attributes, configuredSessionIdKeys)
return (
<div
diff --git a/products/logs/frontend/components/VirtualizedLogsList/cells/AttributeCell.tsx b/products/logs/frontend/components/VirtualizedLogsList/cells/AttributeCell.tsx
index 360e28ddf0cf..9270479811a3 100644
--- a/products/logs/frontend/components/VirtualizedLogsList/cells/AttributeCell.tsx
+++ b/products/logs/frontend/components/VirtualizedLogsList/cells/AttributeCell.tsx
@@ -11,6 +11,7 @@ import { LogsViewerCellPopover } from 'products/logs/frontend/components/LogsVie
import { logsViewerLogic } from 'products/logs/frontend/components/LogsViewer/logsViewerLogic'
import { LogRowScrollButtons } from 'products/logs/frontend/components/VirtualizedLogsList/LogRowScrollButtons'
import { useCellScroll } from 'products/logs/frontend/components/VirtualizedLogsList/useCellScroll'
+import { logsConfigLogic } from 'products/logs/frontend/logsConfigLogic'
import { isDistinctIdKey, isSessionIdKey } from 'products/logs/frontend/utils'
export interface AttributeCellProps {
@@ -25,6 +26,7 @@ export const AttributeCell = memo(function AttributeCell({
width,
}: AttributeCellProps): JSX.Element {
const { id, isAttributeColumn } = useValues(logsViewerLogic)
+ const { configuredSessionIdKeys } = useValues(logsConfigLogic)
const { addFilter, toggleAttributeColumn } = useActions(logsViewerLogic)
const { scrollRef, handleScroll, startScrolling, stopScrolling } = useCellScroll({
@@ -46,7 +48,7 @@ export const AttributeCell = memo(function AttributeCell({
<span className="font-mono text-xs whitespace-nowrap pr-24" title={value}>
<PersonDisplay person={{ distinct_id: value }} noEllipsis inline />
</span>
- ) : isSessionIdKey(attributeKey) && value ? (
+ ) : isSessionIdKey(attributeKey, configuredSessionIdKeys) && value ? (
<ViewRecordingButton
sessionId={value}
openPlayerIn={RecordingPlayerType.Modal}
diff --git a/products/logs/frontend/logsConfigLogic.ts b/products/logs/frontend/logsConfigLogic.ts
index 88f3fd9c321f..d9d16485a77d 100644
--- a/products/logs/frontend/logsConfigLogic.ts
+++ b/products/logs/frontend/logsConfigLogic.ts
@@ -1,4 +1,4 @@
-import { afterMount, connect, kea, path } from 'kea'
+import { afterMount, connect, kea, path, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import api from 'lib/api'
@@ -39,6 +39,14 @@ export const logsConfigLogic = kea<logsConfigLogicType>([
},
],
})),
+ selectors({
+ // Undefined until logs_config resolves — session detection falls back to its
+ // built-in convention list (which contains the default) in the meantime.
+ configuredSessionIdKeys: [
+ (s) => [s.logsConfig],
+ (logsConfig): string[] | undefined => logsConfig?.logs_session_id_attribute_keys,
+ ],
+ }),
afterMount(({ actions }) => {
actions.loadLogsConfig()
}),
diff --git a/products/logs/frontend/utils.test.ts b/products/logs/frontend/utils.test.ts
index e2d61b91938a..0a162db69204 100644
--- a/products/logs/frontend/utils.test.ts
+++ b/products/logs/frontend/utils.test.ts
@@ -93,6 +93,61 @@ describe('logs utils', () => {
})
})
+ describe('configured session ID keys', () => {
+ it.each([
+ [
+ 'configured key wins over a built-in convention key',
+ ['my.custom.key'],
+ { session_id: 'builtin', 'my.custom.key': 'custom' },
+ undefined,
+ 'custom',
+ ],
+ [
+ 'configured keys are checked in list order',
+ ['second.key', 'first.key'],
+ { 'first.key': 'first', 'second.key': 'second' },
+ undefined,
+ 'second',
+ ],
+ [
+ 'configured key found in resource_attributes',
+ ['my.custom.key'],
+ undefined,
+ { 'my.custom.key': 'from-resource' },
+ 'from-resource',
+ ],
+ [
+ 'falls back to built-in conventions when configured keys are absent',
+ ['my.custom.key'],
+ { $session_id: 'builtin' },
+ undefined,
+ 'builtin',
+ ],
+ [
+ 'configured keys match exactly, not by dot suffix',
+ ['custom.key'],
+ { 'prefix.custom.key': 'suffixed' },
+ undefined,
+ null,
+ ],
+ ])('%s', (_, configuredKeys, attributes, resourceAttributes, expected) => {
+ expect(
+ getSessionIdFromLogAttributes(
+ attributes as Record<string, unknown> | undefined,
+ resourceAttributes as Record<string, unknown> | undefined,
+ configuredKeys
+ )
+ ).toBe(expected)
+ })
+
+ it.each([
+ ['my.custom.key', ['my.custom.key'], true],
+ ['prefix.my.custom.key', ['my.custom.key'], false],
+ ])('isSessionIdKey(%s, %j) returns %s', (key, configuredKeys, expected) => {
+ expect(isSessionIdKey(key, configuredKeys)).toBe(expected)
+ })
+ })
+
const filterGroup = (
...filters: Array<{ key: string; value: any; type?: string; operator?: string }>
): Record<string, any> => ({
diff --git a/products/logs/frontend/utils.tsx b/products/logs/frontend/utils.tsx
index a8079d0d158c..759c7c0509cf 100644
--- a/products/logs/frontend/utils.tsx
+++ b/products/logs/frontend/utils.tsx
@@ -97,8 +97,10 @@ export function isDistinctIdKey(key: string): boolean {
return matchesKey(key, DISTINCT_ID_KEYS)
}
-export function isSessionIdKey(key: string): boolean {
- return matchesKey(key, SESSION_ID_KEYS)
+// Configured keys (the team's `logs_session_id_attribute_keys` setting) match exactly;
+// only the built-in convention list gets dot-suffix matching.
+export function isSessionIdKey(key: string, configuredKeys?: string[]): boolean {
+ return (configuredKeys ?? []).includes(key) || matchesKey(key, SESSION_ID_KEYS)
}
export interface SessionIdMatch {
@@ -109,8 +111,23 @@ export interface SessionIdMatch {
export function getSessionIdWithKey(
attributes: Record<string, unknown> | undefined,
- resourceAttributes: Record<string, unknown> | undefined
+ resourceAttributes: Record<string, unknown> | undefined,
+ configuredKeys?: string[]
): SessionIdMatch | null {
+ // Configured keys win over the built-in conventions, in list order: for each key,
+ // attributes are checked before resource_attributes, and the first value found wins.
+ for (const key of configuredKeys ?? []) {
+ const attributeValue = attributes?.[key]
+ if (attributeValue) {
+ return { key, value: String(attributeValue), source: 'attribute' }
+ }
+ const resourceAttributeValue = resourceAttributes?.[key]
+ if (resourceAttributeValue) {
+ return { key, value: String(resourceAttributeValue), source: 'resource_attribute' }
+ }
+ }
+ // Built-in convention fallback only — the configured-key pass already ran above,
+ // so isSessionIdKey is deliberately called without configuredKeys here.
for (const [key, value] of Object.entries(attributes || {})) {
if (isSessionIdKey(key) && value) {
return { key, value: String(value), source: 'attribute' }
@@ -126,7 +143,8 @@ export function getSessionIdWithKey(
export function getSessionIdFromLogAttributes(
attributes: Record<string, unknown> | undefined,
- resourceAttributes: Record<string, unknown> | undefined
+ resourceAttributes: Record<string, unknown> | undefined,
+ configuredKeys?: string[]
): string | null {
- return getSessionIdWithKey(attributes, resourceAttributes)?.value ?? null
+ return getSessionIdWithKey(attributes, resourceAttributes, configuredKeys)?.value ?? null
}