: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(llma): default $geoip_disable for otel ai traces (#57474)
Carlos Marchalc3024d901a46
diff --git a/rust/capture/src/otel/fan_out.rs b/rust/capture/src/otel/fan_out.rsindex 2d607e5a216a..a43622a7d7be 100644--- a/rust/capture/src/otel/fan_out.rs+++ b/rust/capture/src/otel/fan_out.rs@@ -94,6 +94,17 @@ fn filter_resource_attributes(attrs: &[KeyValue]) -> serde_json::Map<String, Val.collect()}+fn apply_geoip_default(properties: &mut serde_json::Map<String, Value>) {+ let alias = properties.remove("posthog.geoip_disable");+ if properties.contains_key("$geoip_disable") {+ return;+ }+ properties.insert(+ "$geoip_disable".to_string(),+ alias.unwrap_or(Value::Bool(true)),+ );+}+fn nanos_to_datetime(nanos: u64) -> Option<DateTime<Utc>> {if nanos == 0 {return None;@@ -143,6 +154,8 @@ pub fn expand_into_events(let mut properties = resource_attrs.clone();properties.extend(span_attrs);+ apply_geoip_default(&mut properties);+properties.insert("$ai_trace_id".to_string(),Value::String(hex::encode(&span.trace_id)),@@ -198,6 +211,7 @@ mod tests {use opentelemetry_proto::tonic::common::v1::AnyValue;use opentelemetry_proto::tonic::resource::v1::Resource;use opentelemetry_proto::tonic::trace::v1::{ResourceSpans, ScopeSpans, Span};+ use rstest::rstest;fn make_kv(key: &str, value: any_value::Value) -> KeyValue {KeyValue {@@ -642,4 +656,68 @@ mod tests {let events = expand_into_events(&request, "fallback-id");assert_eq!(events[0].distinct_id, "fallback-id");}++ fn make_minimal_request(+ extra_resource_attrs: Vec<KeyValue>,+ extra_span_attrs: Vec<KeyValue>,+ ) -> ExportTraceServiceRequest {+ let mut span_attrs = vec![make_kv(+ "gen_ai.request.model",+ any_value::Value::StringValue("gpt-4".to_string()),+ )];+ span_attrs.extend(extra_span_attrs);++ ExportTraceServiceRequest {+ resource_spans: vec![ResourceSpans {+ resource: Some(Resource {+ attributes: extra_resource_attrs,+ dropped_attributes_count: 0,+ }),+ scope_spans: vec![ScopeSpans {+ scope: None,+ spans: vec![make_span(+ vec![0; 16],+ vec![0; 8],+ vec![],+ 0,+ 0,+ "",+ span_attrs,+ )],+ schema_url: String::new(),+ }],+ schema_url: String::new(),+ }],+ }+ }++ #[rstest]+ // Default: no attributes set; $geoip_disable defaults to true.+ #[case::default_disabled(vec![], vec![], true)]+ // Canonical $geoip_disable on span wins.+ #[case::canonical_opt_in(vec![], vec![("$geoip_disable", false)], false)]+ // posthog.geoip_disable alias on resource is copied into $geoip_disable.+ #[case::alias_opt_in(vec![("posthog.geoip_disable", false)], vec![], false)]+ // Both set: canonical wins, alias is consumed (not duplicated).+ #[case::canonical_wins_over_alias(+ vec![("posthog.geoip_disable", false)],+ vec![("$geoip_disable", true)],+ true+ )]+ fn test_geoip_default_behavior(+ #[case] resource_attrs: Vec<(&str, bool)>,+ #[case] span_attrs: Vec<(&str, bool)>,+ #[case] expected_geoip_disable: bool,+ ) {+ let to_kv = |(k, v): &(&str, bool)| make_kv(k, any_value::Value::BoolValue(*v));+ let request = make_minimal_request(+ resource_attrs.iter().map(to_kv).collect(),+ span_attrs.iter().map(to_kv).collect(),+ );+ let events = expand_into_events(&request, "user");+ let props = events[0].properties.as_object().unwrap();+ assert_eq!(props["$geoip_disable"], Value::Bool(expected_geoip_disable));+ // Alias must always be consumed so it doesn't leak as a duplicate property.+ assert!(!props.contains_key("posthog.geoip_disable"));+ }}