: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(logs): filter renamed custom column cells on the attribute they read (#122639)
Christiaan Hendriksenef87721b8141
diff --git a/products/logs/frontend/components/LogsViewer/config/LogsColumnConfigurator.tsx b/products/logs/frontend/components/LogsViewer/config/LogsColumnConfigurator.tsxindex 136e1e8c2bbb..43dff431805b 100644--- a/products/logs/frontend/components/LogsViewer/config/LogsColumnConfigurator.tsx+++ b/products/logs/frontend/components/LogsViewer/config/LogsColumnConfigurator.tsx@@ -42,7 +42,7 @@ function taxonomicSelectionToColumn(group: TaxonomicFilterGroup, value: string):return { type: 'custom', expression: value }}const prefix = TAXONOMIC_GROUP_TO_PREFIX[group.type]- // customColumnFilterType matches this exact `<prefix>.<key>` form, so a change here needs a change there+ // customColumnFilterTarget matches this exact `<prefix>.<key>` form, so a change here needs a change therereturn prefix ? { type: 'custom', name: value, expression: `${prefix}.${value}` } : null}diff --git a/products/logs/frontend/components/LogsViewer/config/columns.test.ts b/products/logs/frontend/components/LogsViewer/config/columns.test.tsindex d07af402fd5d..6e6236e815c5 100644--- a/products/logs/frontend/components/LogsViewer/config/columns.test.ts+++ b/products/logs/frontend/components/LogsViewer/config/columns.test.ts@@ -1,12 +1,13 @@import { PropertyFilterType } from '~/types'+import { FacetFilterTarget } from 'products/logs/frontend/components/LogsViewer/FacetRail/facetFilters'import { AttributeColumnConfig } from 'products/logs/frontend/types'import {attributeLookupExpression,columnLabel,columnsToCustomColumns,- customColumnFilterType,+ customColumnFilterTarget,LogsColumnConfig,migrateAttributeColumns,normalizeColumns,@@ -93,8 +94,9 @@ describe('logs column config', () => {})})- describe('customColumnFilterType', () => {+ describe('customColumnFilterTarget', () => {const key = 'k8s.pod'+ const escapedKey = "we'ird\\key"it.each<[string,@@ -102,7 +104,7 @@ describe('logs column config', () => {string,Record<string, unknown> | undefined,Record<string, unknown> | undefined,- PropertyFilterType,+ FacetFilterTarget,]>([[@@ -111,7 +113,7 @@ describe('logs column config', () => {attributeLookupExpression(key),{},{ [key]: 'r' },- PropertyFilterType.LogResourceAttribute,+ { key, type: PropertyFilterType.LogResourceAttribute },],['fallback, empty value in attributes',@@ -119,7 +121,7 @@ describe('logs column config', () => {attributeLookupExpression(key),{ [key]: '' },{ [key]: 'r' },- PropertyFilterType.LogAttribute,+ { key, type: PropertyFilterType.LogAttribute },],['fallback, inherited key in neither map',@@ -127,16 +129,47 @@ describe('logs column config', () => {attributeLookupExpression('constructor'),undefined,{},- PropertyFilterType.LogAttribute,+ { key: 'constructor', type: PropertyFilterType.LogAttribute },+ ],+ [+ 'attributes shorthand',+ key,+ `attributes.${key}`,+ {},+ { [key]: 'r' },+ { key, type: PropertyFilterType.LogAttribute },],- ['attributes shorthand', key, `attributes.${key}`, {}, { [key]: 'r' }, PropertyFilterType.LogAttribute],['resource_attributes shorthand with trailing space',key,`resource_attributes.${key} `,{ [key]: 'a' },{ [key]: 'r' },- PropertyFilterType.LogResourceAttribute,+ { key, type: PropertyFilterType.LogResourceAttribute },+ ],+ [+ 'renamed resource_attributes shorthand',+ 'Service',+ 'resource_attributes.service.name',+ {},+ { 'service.name': 'api' },+ { key: 'service.name', type: PropertyFilterType.LogResourceAttribute },+ ],+ [+ 'renamed fallback with an escaped key',+ 'Odd',+ attributeLookupExpression(escapedKey),+ {},+ { [escapedKey]: 'r' },+ { key: escapedKey, type: PropertyFilterType.LogResourceAttribute },+ ],+ [+ 'renamed service_name column',+ 'Service',+ 'service_name',+ {},+ {},+ { key: 'service_name', type: PropertyFilterType.Log },],['other HogQL',@@ -144,11 +177,11 @@ describe('logs column config', () => {`upper(resource_attributes['${key}'])`,{},{ [key]: 'r' },- PropertyFilterType.LogAttribute,+ { key, type: PropertyFilterType.LogAttribute },],])('%s', (_, name, expression, attributes, resourceAttributes, expected) => {expect(- customColumnFilterType({ id: 'c', type: 'custom', name, expression }, attributes, resourceAttributes)+ customColumnFilterTarget({ id: 'c', type: 'custom', name, expression }, attributes, resourceAttributes)).toEqual(expected)})})diff --git a/products/logs/frontend/components/LogsViewer/config/columns.ts b/products/logs/frontend/components/LogsViewer/config/columns.tsindex fed43133b540..a9c023d9f193 100644--- a/products/logs/frontend/components/LogsViewer/config/columns.ts+++ b/products/logs/frontend/components/LogsViewer/config/columns.ts@@ -3,6 +3,8 @@ import { uuid } from 'lib/utils/dom'import { LogsQuery } from '~/queries/schema/schema-general'import { PropertyFilterType } from '~/types'+import { FacetFilterTarget } from 'products/logs/frontend/components/LogsViewer/FacetRail/facetFilters'+import { COLUMN_FILTER_KEYS } from 'products/logs/frontend/components/LogsViewer/Filters/columnFilters'import { MAX_ATTRIBUTE_COLUMN_WIDTH } from 'products/logs/frontend/components/VirtualizedLogsList/layoutUtils'import { AttributeColumnConfig, ParsedLogMessage } from 'products/logs/frontend/types'@@ -155,30 +157,56 @@ export function attributeLookupExpression(key: string): string {return `if(mapContains(attributes, '${escaped}'), attributes['${escaped}'], resource_attributes['${escaped}'])`}-/** Only app-written attribute expressions tie `column.name` to a map key, so others keep the log attribute filter. */-export function customColumnFilterType(+const ATTRIBUTE_LOOKUP_PATTERN =+ /^if\(mapContains\(attributes, '((?:[^'\\]|\\[\s\S])*)'\), attributes\['\1'\], resource_attributes\['\1'\]\)$/++function attributeLookupKey(expression: string): string | null {+ const escaped = ATTRIBUTE_LOOKUP_PATTERN.exec(expression)?.[1]+ if (escaped === undefined) {+ return null+ }+ const key = escaped.replace(/\\([\s\S])/g, '$1')+ return attributeLookupExpression(key) === expression ? key : null+}++function shorthandKey(expression: string, prefix: 'attributes' | 'resource_attributes'): string | null {+ const key = expression.startsWith(`${prefix}.`) ? expression.slice(prefix.length + 1) : ''+ return key || null+}++export function customColumnFilterTarget(column: LogsColumnConfig,attributes: Record<string, unknown> | undefined,resourceAttributes: Record<string, unknown> | undefined-): PropertyFilterType.LogAttribute | PropertyFilterType.LogResourceAttribute {- const key = column.name- const expression = column.expression?.trim()- if (!key) {- return PropertyFilterType.LogAttribute+): FacetFilterTarget {+ const expression = column.expression?.trim() ?? ''++ const attributeKey = shorthandKey(expression, 'attributes')+ if (attributeKey) {+ return { key: attributeKey, type: PropertyFilterType.LogAttribute }+ }+ const resourceAttributeKey = shorthandKey(expression, 'resource_attributes')+ if (resourceAttributeKey) {+ return { key: resourceAttributeKey, type: PropertyFilterType.LogResourceAttribute }}- if (expression === `resource_attributes.${key}`) {- return PropertyFilterType.LogResourceAttribute++ const lookupKey = attributeLookupKey(expression)+ if (lookupKey !== null) {+ // Same check as the expression's mapContains, so the filter targets the map the cell read+ const readResourceMap =+ !(attributes && Object.hasOwn(attributes, lookupKey)) &&+ !!resourceAttributes &&+ Object.hasOwn(resourceAttributes, lookupKey)+ return {+ key: lookupKey,+ type: readResourceMap ? PropertyFilterType.LogResourceAttribute : PropertyFilterType.LogAttribute,+ }}- // Same check as the expression's mapContains, so the filter targets the map the cell read- if (- expression === attributeLookupExpression(key) &&- !(attributes && Object.hasOwn(attributes, key)) &&- resourceAttributes &&- Object.hasOwn(resourceAttributes, key)- ) {- return PropertyFilterType.LogResourceAttribute++ if (COLUMN_FILTER_KEYS.has(expression)) {+ return { key: expression, type: PropertyFilterType.Log }}- return PropertyFilterType.LogAttribute+ return { key: column.name ?? expression, type: PropertyFilterType.LogAttribute }}/** Migrate the legacy persisted `attributeColumnsConfig` map to typed custom columns. */diff --git a/products/logs/frontend/components/VirtualizedLogsList/cells/AttributeCell.tsx b/products/logs/frontend/components/VirtualizedLogsList/cells/AttributeCell.tsxindex e0d309f89f3f..04fc7bc0b65a 100644--- a/products/logs/frontend/components/VirtualizedLogsList/cells/AttributeCell.tsx+++ b/products/logs/frontend/components/VirtualizedLogsList/cells/AttributeCell.tsx@@ -33,6 +33,7 @@ export interface AttributeCellProps {* together.*/cellKey?: string+ filterKey?: string/** Popover filter type, default log attribute. Pass resource attribute for values read from resource attributes. */filterType?: PropertyFilterType}@@ -43,6 +44,7 @@ export const AttributeCell = memo(function AttributeCell({width,timestamp,cellKey,+ filterKey,filterType,}: AttributeCellProps): JSX.Element {const { id, isAttributeColumn } = useValues(logsViewerLogic)@@ -64,7 +66,7 @@ export const AttributeCell = memo(function AttributeCell({attributeKey={attributeKey}value={value}isColumn={isAttributeColumn(attributeKey)}- onAddFilter={(key, filterValue, operator) => addFilter(key, filterValue, operator, filterType)}+ onAddFilter={(key, filterValue, operator) => addFilter(filterKey ?? key, filterValue, operator, filterType)}onToggleColumn={toggleAttributeColumn}><div style={{ width, flexShrink: 0 }} className="relative flex items-center self-stretch group/attr pr-1">diff --git a/products/logs/frontend/components/VirtualizedLogsList/columnDefinitions.tsx b/products/logs/frontend/components/VirtualizedLogsList/columnDefinitions.tsxindex 19a9f55bf8e3..ba3037f438dc 100644--- a/products/logs/frontend/components/VirtualizedLogsList/columnDefinitions.tsx+++ b/products/logs/frontend/components/VirtualizedLogsList/columnDefinitions.tsx@@ -17,7 +17,7 @@ import {LogsColumnConfig,LogsColumnType,columnLabel,- customColumnFilterType,+ customColumnFilterTarget,} from 'products/logs/frontend/components/LogsViewer/config/columns'import { logsViewerLogic } from 'products/logs/frontend/components/LogsViewer/logsViewerLogic'import { AttributeCell } from 'products/logs/frontend/components/VirtualizedLogsList/cells/AttributeCell'@@ -399,20 +399,23 @@ export function createConfiguredColumn(params: {? (log: ParsedLogMessage): JSX.Element => (<IdentityCell log={log} type={identityType} width={totalWidth} />)- : (log: ParsedLogMessage): JSX.Element => (- <AttributeCell- attributeKey={semanticKey}- value={getBuiltInValue ? getBuiltInValue(log) : customColumnValue(log, alias)}- // Raw map: the stringified copy drops a `__proto__` key that the server still reads- filterType={- config.type === 'custom'- ? customColumnFilterType(config, log.originalLog.attributes, log.resource_attributes)- : undefined- }- width={totalWidth}- timestamp={log.timestamp}- />- )+ : (log: ParsedLogMessage): JSX.Element => {+ // Raw map: the stringified copy drops a `__proto__` key that the server still reads+ const filterTarget =+ config.type === 'custom'+ ? customColumnFilterTarget(config, log.originalLog.attributes, log.resource_attributes)+ : undefined+ return (+ <AttributeCell+ attributeKey={semanticKey}+ value={getBuiltInValue ? getBuiltInValue(log) : customColumnValue(log, alias)}+ filterKey={filterTarget?.key}+ filterType={filterTarget?.type}+ width={totalWidth}+ timestamp={log.timestamp}+ />+ )+ }return {key: `col:${config.id}`,