: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): resolve environment facet across renamed resource keys (#78739)
Jon McCallum3531d13f9153
diff --git a/products/logs/frontend/components/LogsViewer/FacetRail/facetCountsLogic.ts b/products/logs/frontend/components/LogsViewer/FacetRail/facetCountsLogic.tsindex 35cf340d05c5..993ab9b96d4c 100644--- a/products/logs/frontend/components/LogsViewer/FacetRail/facetCountsLogic.ts+++ b/products/logs/frontend/components/LogsViewer/FacetRail/facetCountsLogic.ts@@ -24,7 +24,7 @@ import { logsViewerFiltersLogic } from 'products/logs/frontend/components/LogsViimport { logsAttributesRetrieve, logsFacetValuesCreate } from '../../../generated/api'import { _LogFacetValueApi, _LogPropertyFilterApi, _LogsFacetValuesBodyApi } from '../../../generated/api.schemas'import type { LogsViewerFilters } from '../config/types'-import { FACETS, FacetConfig } from './facets'+import { FACETS, FacetConfig, resolveFacets } from './facets'// Broad, filter-independent window for the "which resource attributes does this tenant emit" probe.// Cheap: keys-only group-by on the log_attributes aggregation table (no value scan).@@ -257,7 +257,8 @@ export const facetCountsLogic = kea<facetCountsLogicType>([// typing in one facet's search must not cancel a still-debouncing full reload.loadFacetValuesForKey: async (facetKey: string, breakpoint) => {await breakpoint(300)- const facet = FACETS.find((f) => f.key === facetKey)+ // Resolved, not raw: an aliased facet must search the key the tenant emits.+ const facet = values.visibleFacets.find((f) => f.key === facetKey)if (!facet) {return values.facetValues}@@ -286,11 +287,11 @@ export const facetCountsLogic = kea<facetCountsLogicType>([}),selectors({- // Column facets always render; resource-attribute facets only when the tenant emits the key.+ // Column facets always render; resource-attribute facets only when the tenant emits the key (or one+ // of its aliases, which resolution rewrites the facet onto).visibleFacets: [(s) => [s.presentResourceKeys],- (presentResourceKeys: string[]): FacetConfig[] =>- FACETS.filter((f) => f.source.type === 'column' || presentResourceKeys.includes(f.source.key)),+ (presentResourceKeys: string[]): FacetConfig[] => resolveFacets(FACETS, presentResourceKeys),],// Per-facet loading state. A filter-change reload and a per-facet search have independent// breakpoints and can overlap, so union both sources — otherwise whichever settles first woulddiff --git a/products/logs/frontend/components/LogsViewer/FacetRail/facets.test.ts b/products/logs/frontend/components/LogsViewer/FacetRail/facets.test.tsindex 74d6355c350c..9eabc174dc07 100644--- a/products/logs/frontend/components/LogsViewer/FacetRail/facets.test.ts+++ b/products/logs/frontend/components/LogsViewer/FacetRail/facets.test.ts@@ -2,11 +2,13 @@ import { FilterLogicalOperator, PropertyFilterType, PropertyOperator, UniversalFimport { FacetOption } from './Facet'import {+ FACETS as CONFIGURED_FACETS,FacetConfig,cycleResourceAttributeFilter,filterFacetsByName,logFilterExclusions,mergeSelectedIntoOptions,+ resolveFacets,resourceAttributeSelection,setLogFilterExclusions,} from './facets'@@ -239,4 +241,31 @@ describe('facets', () => {})})})++ describe('resolveFacets', () => {+ const environmentKey = (presentResourceKeys: string[]): string | undefined => {+ const facet = resolveFacets(CONFIGURED_FACETS, presentResourceKeys).find((f) => f.key === 'environment')+ return facet?.source.type === 'resourceAttribute' ? facet.source.key : undefined+ }++ // The rail queries and filters on whichever key resolution picks, so picking the wrong one (or+ // none) silently hides a facet the tenant's data can populate.+ it.each<[string, string[], string | undefined]>([+ ['the current key is used as-is', ['deployment.environment.name'], 'deployment.environment.name'],+ ['the superseded key still resolves', ['deployment.environment'], 'deployment.environment'],+ ['a datadog env tag still resolves', ['env'], 'env'],+ [+ 'the current key wins over its aliases',+ ['env', 'deployment.environment', 'deployment.environment.name'],+ 'deployment.environment.name',+ ],+ ['no spelling emitted drops the facet', ['k8s.pod.name'], undefined],+ ])('%s', (_, presentResourceKeys, expected) => {+ expect(environmentKey(presentResourceKeys)).toEqual(expected)+ })++ it('keeps column facets whatever the tenant emits', () => {+ expect(resolveFacets(CONFIGURED_FACETS, []).map((f) => f.key)).toEqual(['level', 'service'])+ })+ })})diff --git a/products/logs/frontend/components/LogsViewer/FacetRail/facets.ts b/products/logs/frontend/components/LogsViewer/FacetRail/facets.tsindex 567861b6721f..8ec133fc967f 100644--- a/products/logs/frontend/components/LogsViewer/FacetRail/facets.ts+++ b/products/logs/frontend/components/LogsViewer/FacetRail/facets.ts@@ -45,7 +45,7 @@ export type FacetSource =*/exclusionKey?: string}- | { type: 'resourceAttribute'; key: string }+ | { type: 'resourceAttribute'; key: string; aliasKeys?: string[] }export interface FacetConfig {/** Stable id used for collapse state and data-attrs. */@@ -222,15 +222,22 @@ const SERVICE_FACET: FacetConfig = {maxHeight: 300,}-// Curated OTel resource attributes worth faceting. Keys are the stable OTel semantic-convention names;-// `deployment.environment.name` is the 1.27+ stable key (older data may use `deployment.environment`).-function resourceAttributeFacet(key: string, slug: string, title: string, group: string): FacetConfig {+// Curated OTel resource attributes worth faceting. `key` is the current OTel semantic-convention name;+// `aliasKeys` are older or non-standard spellings of the same attribute that resolveFacets falls back to+// when the tenant doesn't emit the current key.+function resourceAttributeFacet(+ key: string,+ slug: string,+ title: string,+ group: string,+ aliasKeys?: string[]+): FacetConfig {return {key: slug,title,group,kind: 'dynamic',- source: { type: 'resourceAttribute', key },+ source: { type: 'resourceAttribute', key, aliasKeys },searchable: true,searchPlaceholder: `Search ${title.toLowerCase()}…`,emptyLabel: `No ${title.toLowerCase()} values`,@@ -238,11 +245,16 @@ function resourceAttributeFacet(key: string, slug: string, title: string, group:}}+// The only faceted attribute semconv has ever renamed: `deployment.environment` became+// `deployment.environment.name` in 1.27. `env` is not a semantic convention — it's what a Datadog `env:`+// tag lands as, since the Datadog ingest path stores ddtags verbatim. The rest below need no aliases:+// the `k8s.*.name` keys are stable and were never renamed, and `host.name` has kept its spelling too.const ENVIRONMENT_FACET = resourceAttributeFacet('deployment.environment.name','environment','Environment',- 'Standard'+ 'Standard',+ ['deployment.environment', 'env'])const NAMESPACE_FACET = resourceAttributeFacet('k8s.namespace.name', 'namespace', 'Namespace', 'Kubernetes')const DEPLOYMENT_FACET = resourceAttributeFacet('k8s.deployment.name', 'deployment', 'Deployment', 'Kubernetes')@@ -253,7 +265,8 @@ const HOST_FACET = resourceAttributeFacet('host.name', 'host', 'Host', 'Infrastr/*** The rail is rendered entirely from this list — append a config to add a facet (or a new group).* Ordered by group (Standard → Kubernetes → Infrastructure) since facetsByGroup keeps first-appearance order.- * Resource-attribute facets only render when the tenant actually emits the key (see facetCountsLogic).+ * Resource-attribute facets only render when the tenant actually emits the key or one of its aliases+ * (see resolveFacets, called from facetCountsLogic).*/export const FACETS: FacetConfig[] = [LEVEL_FACET,@@ -266,6 +279,29 @@ export const FACETS: FacetConfig[] = [HOST_FACET,]+/**+ * Resolve the configured facets against the resource-attribute keys a tenant actually emits.+ * Column facets always pass through. A resource-attribute facet is kept only if the tenant emits its+ * current key or one of its aliases, and its source is rewritten onto whichever spelling is present so+ * the rail queries and filters on the key that has data. The current key wins when several are present.+ */+export function resolveFacets(facets: FacetConfig[], presentResourceKeys: string[]): FacetConfig[] {+ const present = new Set(presentResourceKeys)+ const resolved: FacetConfig[] = []+ for (const facet of facets) {+ if (facet.source.type === 'column') {+ resolved.push(facet)+ continue+ }+ const match = [facet.source.key, ...(facet.source.aliasKeys ?? [])].find((k) => present.has(k))+ if (match === undefined) {+ continue+ }+ resolved.push(match === facet.source.key ? facet : { ...facet, source: { ...facet.source, key: match } })+ }+ return resolved+}+/*** Filter facets by a free-text query matching the field name or its group (case-insensitive* substring) — powers the rail's "search facets" box. A blank query returns everything, sodiff --git a/products/tracing/frontend/components/FacetRail/facetCountsLogic.ts b/products/tracing/frontend/components/FacetRail/facetCountsLogic.tsindex 347799a137d3..8339767eca8c 100644--- a/products/tracing/frontend/components/FacetRail/facetCountsLogic.ts+++ b/products/tracing/frontend/components/FacetRail/facetCountsLogic.ts@@ -21,7 +21,7 @@ import {SpanPropertyTypeEnumApi,TracingSpansAttributesRetrieveAttributeType,} from '../../generated/api.schemas'-import { FACETS, FacetConfig, innerFilters } from './facets'+import { FACETS, FacetConfig, innerFilters, resolveFacets } from './facets'export interface FacetCountsLogicProps {id: string@@ -307,7 +307,8 @@ export const facetCountsLogic = kea<facetCountsLogicType>([// typing in one facet's search must not cancel a still-debouncing full reload.loadFacetValuesForKey: async (facetKey: string, breakpoint) => {await breakpoint(300)- const facet = FACETS.find((f) => f.key === facetKey)+ // Resolved, not raw: an aliased facet must search the key the tenant emits.+ const facet = values.visibleFacets.find((f) => f.key === facetKey)const result = facet ? await mergeFetched([facet]) : values.facetValuesbreakpoint()return result@@ -335,11 +336,11 @@ export const facetCountsLogic = kea<facetCountsLogicType>([}),selectors({- // Column facets always render; resource-attribute facets only when the tenant emits the key.+ // Column facets always render; resource-attribute facets only when the tenant emits the key (or one+ // of its aliases, which resolution rewrites the facet onto).visibleFacets: [(s) => [s.presentResourceKeys],- (presentResourceKeys: string[]): FacetConfig[] =>- FACETS.filter((f) => f.source.type === 'column' || presentResourceKeys.includes(f.source.key)),+ (presentResourceKeys: string[]): FacetConfig[] => resolveFacets(FACETS, presentResourceKeys),],// Per-facet loading state. A filter-change reload and a per-facet search have independent// breakpoints and can overlap, so union both sources — otherwise whichever settles first woulddiff --git a/products/tracing/frontend/components/FacetRail/facets.test.ts b/products/tracing/frontend/components/FacetRail/facets.test.tsindex 4cd37708db12..d1c1002529dd 100644--- a/products/tracing/frontend/components/FacetRail/facets.test.ts+++ b/products/tracing/frontend/components/FacetRail/facets.test.ts@@ -1,11 +1,13 @@import { FilterLogicalOperator, PropertyFilterType, PropertyOperator, UniversalFiltersGroup } from '~/types'import {+ FACETS,FacetSource,FilterGroupFacetSource,facetFilterValues,facetSelectedValues,mergeSelectedIntoOptions,+ resolveFacets,toggleFacetFilter,} from './facets'@@ -139,4 +141,31 @@ describe('facets', () => {expect(options).toEqual(expect.arrayContaining(fetched))})})++ describe('resolveFacets', () => {+ const environmentKey = (presentResourceKeys: string[]): string | undefined => {+ const facet = resolveFacets(FACETS, presentResourceKeys).find((f) => f.key === 'environment')+ return facet?.source.type === 'resourceAttribute' ? facet.source.key : undefined+ }++ // The rail queries and filters on whichever key resolution picks, so picking the wrong one (or+ // none) silently hides a facet the tenant's data can populate.+ it.each<[string, string[], string | undefined]>([+ ['the current key is used as-is', ['deployment.environment.name'], 'deployment.environment.name'],+ ['the superseded key still resolves', ['deployment.environment'], 'deployment.environment'],+ ['a datadog env tag still resolves', ['env'], 'env'],+ [+ 'the current key wins over its aliases',+ ['env', 'deployment.environment', 'deployment.environment.name'],+ 'deployment.environment.name',+ ],+ ['no spelling emitted drops the facet', ['k8s.pod.name'], undefined],+ ])('%s', (_, presentResourceKeys, expected) => {+ expect(environmentKey(presentResourceKeys)).toEqual(expected)+ })++ it('keeps column facets whatever the tenant emits', () => {+ expect(resolveFacets(FACETS, []).map((f) => f.key)).toEqual(['service', 'status'])+ })+ })})diff --git a/products/tracing/frontend/components/FacetRail/facets.ts b/products/tracing/frontend/components/FacetRail/facets.tsindex c28b192c142c..b3ebc9434d02 100644--- a/products/tracing/frontend/components/FacetRail/facets.ts+++ b/products/tracing/frontend/components/FacetRail/facets.ts@@ -34,7 +34,9 @@ export type FacetColumn = 'service_name' | 'status_code'* - `resourceAttribute`: a `resource_attributes` map key (e.g. k8s.namespace.name), queried with* breakdownType `span_resource_attribute`. Selection is a span_resource_attribute property filter.*/-export type FacetSource = { type: 'column'; column: FacetColumn } | { type: 'resourceAttribute'; key: string }+export type FacetSource =+ | { type: 'column'; column: FacetColumn }+ | { type: 'resourceAttribute'; key: string; aliasKeys?: string[] }/*** The sources whose selection lives in the filterGroup. `service_name` is deliberately excluded:@@ -43,7 +45,7 @@ export type FacetSource = { type: 'column'; column: FacetColumn } | { type: 'res*/export type FilterGroupFacetSource =| { type: 'column'; column: Exclude<FacetColumn, 'service_name'> }- | { type: 'resourceAttribute'; key: string }+ | { type: 'resourceAttribute'; key: string; aliasKeys?: string[] }export interface FacetConfig {/** Stable id used for collapse state and data-attrs. */@@ -185,15 +187,22 @@ const STATUS_FACET: FacetConfig = {fixedOptions: STATUS_OPTIONS,}-// Curated OTel resource attributes worth faceting. Keys are the stable OTel semantic-convention names;-// `deployment.environment.name` is the 1.27+ stable key (older data may use `deployment.environment`).-function resourceAttributeFacet(key: string, slug: string, title: string, group: string): FacetConfig {+// Curated OTel resource attributes worth faceting. `key` is the current OTel semantic-convention name;+// `aliasKeys` are older or non-standard spellings of the same attribute that resolveFacets falls back to+// when the tenant doesn't emit the current key.+function resourceAttributeFacet(+ key: string,+ slug: string,+ title: string,+ group: string,+ aliasKeys?: string[]+): FacetConfig {return {key: slug,title,group,kind: 'dynamic',- source: { type: 'resourceAttribute', key },+ source: { type: 'resourceAttribute', key, aliasKeys },searchable: true,searchPlaceholder: `Search ${title.toLowerCase()}…`,emptyLabel: `No ${title.toLowerCase()} values`,@@ -201,11 +210,17 @@ function resourceAttributeFacet(key: string, slug: string, title: string, group:}}+// The only faceted attribute semconv has ever renamed: `deployment.environment` became+// `deployment.environment.name` in 1.27. `env` is not a semantic convention — it's what a Datadog `env:`+// tag lands as, since the Datadog ingest path stores ddtags verbatim. The rest below need no aliases:+// the `k8s.*.name` keys and `service.version` are stable and were never renamed, and `host.name` has+// kept its spelling too.const ENVIRONMENT_FACET = resourceAttributeFacet('deployment.environment.name','environment','Environment',- 'Standard'+ 'Standard',+ ['deployment.environment', 'env'])const VERSION_FACET = resourceAttributeFacet('service.version', 'version', 'Version', 'Standard')const NAMESPACE_FACET = resourceAttributeFacet('k8s.namespace.name', 'namespace', 'Namespace', 'Kubernetes')@@ -215,7 +230,8 @@ const HOST_FACET = resourceAttributeFacet('host.name', 'host', 'Host', 'Infrastr/*** The rail is rendered entirely from this list — append a config to add a facet (or a new group).* Ordered by group (Standard → Kubernetes → Infrastructure) since facetsByGroup keeps first-appearance order.- * Resource-attribute facets only render when the tenant actually emits the key (see facetCountsLogic).+ * Resource-attribute facets only render when the tenant actually emits the key or one of its aliases+ * (see resolveFacets, called from facetCountsLogic).*/export const FACETS: FacetConfig[] = [SERVICE_FACET,@@ -227,6 +243,29 @@ export const FACETS: FacetConfig[] = [HOST_FACET,]+/**+ * Resolve the configured facets against the resource-attribute keys a tenant actually emits.+ * Column facets always pass through. A resource-attribute facet is kept only if the tenant emits its+ * current key or one of its aliases, and its source is rewritten onto whichever spelling is present so+ * the rail queries and filters on the key that has data. The current key wins when several are present.+ */+export function resolveFacets(facets: FacetConfig[], presentResourceKeys: string[]): FacetConfig[] {+ const present = new Set(presentResourceKeys)+ const resolved: FacetConfig[] = []+ for (const facet of facets) {+ if (facet.source.type === 'column') {+ resolved.push(facet)+ continue+ }+ const match = [facet.source.key, ...(facet.source.aliasKeys ?? [])].find((k) => present.has(k))+ if (match === undefined) {+ continue+ }+ resolved.push(match === facet.source.key ? facet : { ...facet, source: { ...facet.source, key: match } })+ }+ return resolved+}+/*** Filter facets by a free-text query matching the field name or its group (case-insensitive* substring) — powers the rail's "search facets" box. A blank query returns everything, so