Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added distinct_id to kafka headers #27666

Merged
merged 18 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 13 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
7 changes: 5 additions & 2 deletions posthog/api/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,8 @@ def capture_internal(
if extra_headers is None:
extra_headers = []

headers = [("token", token), ("distinct_id", distinct_id), *extra_headers]

parsed_event = build_kafka_event_data(
distinct_id=distinct_id,
ip=ip,
Expand All @@ -871,7 +873,6 @@ def capture_internal(

if event["event"] in SESSION_RECORDING_EVENT_NAMES:
session_id = event["properties"]["$session_id"]
headers = [("token", token), *extra_headers]

overflowing = False
if token in settings.REPLAY_OVERFLOW_FORCED_TOKENS:
Expand Down Expand Up @@ -900,7 +901,9 @@ def capture_internal(
else:
kafka_partition_key = candidate_partition_key

return log_event(parsed_event, event["event"], partition_key=kafka_partition_key, historical=historical)
return log_event(
parsed_event, event["event"], partition_key=kafka_partition_key, historical=historical, headers=headers
)


def is_randomly_partitioned(candidate_partition_key: str) -> bool:
Expand Down
19 changes: 14 additions & 5 deletions rust/capture/src/sinks/kafka.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ impl KafkaSink {
let data_type = metadata.data_type;
let event_key = event.key();
let session_id = metadata.session_id.clone();
let distinct_id = event.distinct_id.clone();

drop(event); // Events can be EXTREMELY memory hungry

Expand Down Expand Up @@ -255,10 +256,17 @@ impl KafkaSink {
partition: None,
key: partition_key,
timestamp: None,
headers: Some(OwnedHeaders::new().insert(Header {
key: "token",
value: Some(&token),
})),
headers: Some(
OwnedHeaders::new()
.insert(Header {
key: "token",
value: Some(&token),
})
.insert(Header {
key: "distinct_id",
value: Some(&distinct_id),
}),
),
}) {
Ok(ack) => Ok(ack),
Err((e, _)) => match e.rdkafka_error_code() {
Expand Down Expand Up @@ -413,9 +421,10 @@ mod tests {
// We test different cases in a single test to amortize the startup cost of the producer.

let (cluster, sink) = start_on_mocked_sink(Some(3000000)).await;
let distinct_id = "test_distinct_id_123".to_string();
let event: CapturedEvent = CapturedEvent {
uuid: uuid_v7(),
distinct_id: "id1".to_string(),
distinct_id: distinct_id.clone(),
ip: "".to_string(),
data: "".to_string(),
now: "".to_string(),
Expand Down
Loading