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(flags): write person properties to flag_evaluations (#116495)

Phil Haack5ffc5b161fa4
diff --git a/nodejs/src/ingestion/common/flag-evaluations/flag-evaluation-row.test.ts b/nodejs/src/ingestion/common/flag-evaluations/flag-evaluation-row.test.ts
index 2c25599572c3..82ab2242247f 100644
--- a/nodejs/src/ingestion/common/flag-evaluations/flag-evaluation-row.test.ts
+++ b/nodejs/src/ingestion/common/flag-evaluations/flag-evaluation-row.test.ts
@@ -1,3 +1,4 @@
+import { pick } from 'lodash'
import { DateTime } from 'luxon'
import { parseJSON } from '~/common/utils/json-parse'
@@ -28,32 +29,25 @@ describe('mapProcessedEventToFlagEvaluationRow', () => {
const row = mapProcessedEventToFlagEvaluationRow(processedEvent)
const eventsRow = serializeEvent(processedEvent)
- expect(row.uuid).toBe('event-uuid-1')
- expect(row.event).toBe('$feature_flag_called')
- expect(row.team_id).toBe(42)
- expect(row.distinct_id).toBe('distinct-1')
- expect(row.person_id).toBe('person-uuid-1')
+ // Both rows derive created_at and person_created_at from the event, so they agree.
+ // Reading the wall clock per serialization instead would stamp two different values.
+ expect(row).toStrictEqual(
+ pick(eventsRow, [
+ 'uuid',
+ 'event',
+ 'properties',
+ 'timestamp',
+ 'team_id',
+ 'distinct_id',
+ 'created_at',
+ 'person_id',
+ 'person_properties',
+ 'person_created_at',
+ 'person_mode',
+ ])
+ )
expect(row.timestamp).toBe('2024-01-15 10:30:00.123')
- expect(row.properties).toBe(eventsRow.properties)
- // Both rows derive created_at from the event, so they agree. Reading the
- // wall clock per serialization instead would stamp two different values.
- expect(row.created_at).toBe(eventsRow.created_at)
expect(row.created_at).toBe('2024-01-15 10:31:00.456')
expect(parseJSON(row.properties!).$feature_flag).toBe('my-flag')
})
-
- it('omits person properties from the flag evaluation wire format', () => {
- const row = mapProcessedEventToFlagEvaluationRow(processedEvent)
-
- expect(Object.keys(row).sort()).toEqual([
- 'created_at',
- 'distinct_id',
- 'event',
- 'person_id',
- 'properties',
- 'team_id',
- 'timestamp',
- 'uuid',
- ])
- })
})
diff --git a/nodejs/src/ingestion/common/flag-evaluations/flag-evaluation-row.ts b/nodejs/src/ingestion/common/flag-evaluations/flag-evaluation-row.ts
index 3f522e36788c..e75c536e04b8 100644
--- a/nodejs/src/ingestion/common/flag-evaluations/flag-evaluation-row.ts
+++ b/nodejs/src/ingestion/common/flag-evaluations/flag-evaluation-row.ts
@@ -1,6 +1,26 @@
+import { pick } from 'lodash'
+
import { serializeEvent } from '~/ingestion/common/steps/event-processing/emit-event-step'
import { ProcessedEvent, RawKafkaEvent } from '~/types'
+// Never add `flag_key`, `response`, `session_id`, `request_id` or `$group_0..4`.
+// The Kafka table does not declare them, and the shard computes them from
+// `properties` as DEFAULT columns. A DEFAULT fills only a column the insert
+// omits. A sent value is stored even where it contradicts `properties`.
+const FLAG_EVALUATION_COLUMNS = [
+ 'uuid',
+ 'event',
+ 'properties',
+ 'timestamp',
+ 'team_id',
+ 'distinct_id',
+ 'created_at',
+ 'person_id',
+ 'person_properties',
+ 'person_created_at',
+ 'person_mode',
+] as const
+
/**
* One row for the kafka_flag_evaluations ClickHouse table (JSONEachRow).
* Keep in sync with posthog/models/flag_evaluations/sql.py.
@@ -10,35 +30,9 @@ import { ProcessedEvent, RawKafkaEvent } from '~/types'
* through serializeEvent keeps every shared field byte-identical to what the
* events table receives for the same event, rather than a second hand-rolled
* mapping that could drift from it.
- *
- * Deliberately not sent:
- * - `person_properties`: flag evaluations omit person properties, so ClickHouse
- * fills the column with an empty string.
- * - `inserted_at`: omitted keys zero-fill in ClickHouse, and the MV treats the
- * epoch sentinel as "absent" and stamps the Kafka message timestamp instead.
- * - `group0..group4_properties`: serializeEvent omits them, so they zero-fill
- * empty in both tables.
- * - `flag_key`, `response`, `session_id`, `request_id`, `$group_0..4`: the Kafka
- * table does not declare these, and the shard computes them from `properties`
- * as DEFAULT columns. A DEFAULT only fills when the insert omits the column,
- * so naming one here would store the sent value even where it contradicts
- * `properties`. This row type must never grow them.
*/
-export type FlagEvaluationRow = Pick<
- RawKafkaEvent,
- 'uuid' | 'event' | 'properties' | 'timestamp' | 'team_id' | 'distinct_id' | 'created_at' | 'person_id'
->
+export type FlagEvaluationRow = Pick<RawKafkaEvent, (typeof FLAG_EVALUATION_COLUMNS)[number]>
export function mapProcessedEventToFlagEvaluationRow(event: ProcessedEvent): FlagEvaluationRow {
- const serialized = serializeEvent(event)
- return {
- uuid: serialized.uuid,
- event: serialized.event,
- properties: serialized.properties,
- timestamp: serialized.timestamp,
- team_id: serialized.team_id,
- distinct_id: serialized.distinct_id,
- created_at: serialized.created_at,
- person_id: serialized.person_id,
- }
+ return pick(serializeEvent(event), FLAG_EVALUATION_COLUMNS)
}
diff --git a/nodejs/tests/ingestion/flag-evaluations-e2e.test.ts b/nodejs/tests/ingestion/flag-evaluations-e2e.test.ts
index aa767c959240..e673f346d6ff 100644
--- a/nodejs/tests/ingestion/flag-evaluations-e2e.test.ts
+++ b/nodejs/tests/ingestion/flag-evaluations-e2e.test.ts
@@ -8,6 +8,7 @@ import { KafkaProducerObserver } from '~/tests/helpers/mocks/producer.spy'
import { createHogTransformerService } from '~/cdp/hog-transformations/hog-transformer.service'
import { KAFKA_CLICKHOUSE_FLAG_EVALUATIONS, KAFKA_REALTIME_ONLY_EVENTS_JSON } from '~/common/config/kafka-topics'
import { ClickhouseGroupRepository } from '~/common/groups/repositories/clickhouse-group-repository'
+import { parseRawClickHouseEvent } from '~/common/utils/event'
import { parseJSON } from '~/common/utils/json-parse'
import { createFlagEvaluationsService } from '~/ingestion/common/flag-evaluations/flag-evaluations-service'
import { getDefaultIngestionConsumerConfig, getDefaultIngestionOutputsConfig } from '~/ingestion/config'
@@ -69,6 +70,7 @@ describe('Flag evaluations shadow-routing E2E', () => {
.withProperties({
$feature_flag: 'my-flag',
$feature_flag_response: true,
+ $set: { email: '[email protected]' },
})
.build()
@@ -102,7 +104,12 @@ describe('Flag evaluations shadow-routing E2E', () => {
// event — the parity per-flag uniques depend on.
expect(flagEvaluations[0].person_id).not.toBe('00000000-0000-0000-0000-000000000000')
expect(flagEvaluations[0].person_id).toBe(events[0].person_id)
- // The producer omits inserted_at; the MV fallback must fill it.
+ const flagEvaluation = parseRawClickHouseEvent(flagEvaluations[0])
+ expect(flagEvaluation.person_properties).toEqual(events[0].person_properties)
+ expect(flagEvaluation.person_properties.email).toBe('[email protected]')
+ expect(flagEvaluation.person_created_at?.toISO()).toBe(events[0].person_created_at?.toISO())
+ expect(flagEvaluations[0].person_mode).toBe(events[0].person_mode)
+ // The MV stamps inserted_at with now64() when it processes the row.
expect(flagEvaluations[0].inserted_at).not.toMatch(/^1970/)
}
)