-
Notifications
You must be signed in to change notification settings - Fork 77
feat: opt-in OTel trace/span IDs on regular capture() via capture_trace_context #894
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
Changes from all commits
a9ef7ba
174e57a
2448fee
6938781
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -625,6 +625,104 @@ def test_capture_exception_uses_current_otel_span_context( | |
| self.assertEqual(event["properties"]["$trace_id"], expected_trace_id) | ||
| self.assertEqual(event["properties"]["$span_id"], expected_span_id) | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| ( | ||
| "capture_active_context", | ||
| "capture", | ||
| 0x123, | ||
| 0x456, | ||
| {}, | ||
| "00000000000000000000000000000123", | ||
| "0000000000000456", | ||
| ), | ||
| ( | ||
| "capture_explicit_properties_win", | ||
| "capture", | ||
| 0x123, | ||
| 0x456, | ||
| {"$trace_id": "custom-trace", "$span_id": "custom-span"}, | ||
| "custom-trace", | ||
| "custom-span", | ||
| ), | ||
| ("capture_invalid_context", "capture", 0, 0, {}, None, None), | ||
| ( | ||
| "capture_ai_active_context", | ||
| "capture_ai", | ||
| 0x123, | ||
| 0x456, | ||
| {}, | ||
| "00000000000000000000000000000123", | ||
| "0000000000000456", | ||
| ), | ||
| ( | ||
| "capture_ai_explicit_properties_win", | ||
| "capture_ai", | ||
| 0x123, | ||
| 0x456, | ||
| {"$trace_id": "custom-trace", "$span_id": "custom-span"}, | ||
| "custom-trace", | ||
| "custom-span", | ||
| ), | ||
| ("capture_ai_invalid_context", "capture_ai", 0, 0, {}, None, None), | ||
| ] | ||
| ) | ||
| def test_capture_uses_current_otel_span_context_when_enabled( | ||
| self, | ||
| _, | ||
| entrypoint, | ||
| context_trace_id, | ||
| context_span_id, | ||
| properties, | ||
| expected_trace_id, | ||
| expected_span_id, | ||
| ): | ||
| span_context = SpanContext( | ||
| trace_id=context_trace_id, | ||
| span_id=context_span_id, | ||
| is_remote=False, | ||
| trace_flags=TraceFlags.SAMPLED, | ||
| ) | ||
|
|
||
| with ( | ||
| mock.patch("posthog.client.batch_post") as mock_post, | ||
| use_span(NonRecordingSpan(span_context)), | ||
| ): | ||
| client = Client( | ||
| FAKE_TEST_API_KEY, sync_mode=True, capture_trace_context=True | ||
| ) | ||
| capture = getattr(client, entrypoint) | ||
| capture("$ai_event", distinct_id="distinct_id", properties=properties) | ||
|
|
||
| event = mock_post.call_args.kwargs["batch"][0] | ||
| if expected_trace_id is None: | ||
| self.assertNotIn("$trace_id", event["properties"]) | ||
| self.assertNotIn("$span_id", event["properties"]) | ||
| else: | ||
| self.assertEqual(event["properties"]["$trace_id"], expected_trace_id) | ||
| self.assertEqual(event["properties"]["$span_id"], expected_span_id) | ||
|
Comment on lines
+670
to
+703
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The feature explicitly covers both Knowledge Base Used: Event capture and delivery Prompt To Fix With AIThis is a comment left during a code review.
Path: posthog/test/test_client.py
Line: 649-682
Comment:
**Trace tests omit capture_ai**
The feature explicitly covers both `capture()` and `capture_ai()`, but these tests exercise only `capture()`. Parameterizing the entrypoint would ensure a regression in the dedicated AI capture path cannot pass while breaking the newly documented trace-correlation behavior.
**Knowledge Base Used:** [Event capture and delivery](https://app.greptile.com/posthog-org-19734/-/custom-context/knowledge-base/posthog/posthog-python/-/docs/event-capture-and-delivery.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
|
|
||
| @parameterized.expand([("capture",), ("capture_ai",)]) | ||
| def test_capture_does_not_attach_otel_span_context_by_default(self, entrypoint): | ||
| span_context = SpanContext( | ||
| trace_id=0x123, | ||
| span_id=0x456, | ||
| is_remote=False, | ||
| trace_flags=TraceFlags.SAMPLED, | ||
| ) | ||
|
|
||
| with ( | ||
| mock.patch("posthog.client.batch_post") as mock_post, | ||
| use_span(NonRecordingSpan(span_context)), | ||
| ): | ||
| client = Client(FAKE_TEST_API_KEY, sync_mode=True) | ||
| capture = getattr(client, entrypoint) | ||
| capture("$ai_event", distinct_id="distinct_id") | ||
|
|
||
| event = mock_post.call_args.kwargs["batch"][0] | ||
| self.assertNotIn("$trace_id", event["properties"]) | ||
| self.assertNotIn("$span_id", event["properties"]) | ||
|
|
||
| def test_basic_capture_exception_with_distinct_id(self): | ||
| with mock.patch.object(Client, "capture", return_value=None) as patch_capture: | ||
| client = self.client | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blocking: Super properties override explicit trace IDs — The documented “explicit properties win” guarantee does not hold when
super_propertiescontains$trace_idor$span_id:_enqueue()merges super properties afterward with higher precedence, replacing both the current span and values passed directly tocapture(). A reproduced event passedevent-trace/event-spanbut sentsuper-trace/super-span, causing incorrect trace correlation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if thats intentional, thats ok