Skip to content

feat(otel): add request context to application spans - #375

Merged
pauldambra merged 4 commits into
mainfrom
posthog/go-http-span-context
Oct 26, 2026
Merged

pauldambra merged 4 commits into
mainfrom
posthog/go-http-span-context

Conversation

@pauldambra

@pauldambra pauldambra commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

[Robot] Prepared with PostHog Desktop on behalf of @pauldambra.

💡 Motivation and Context

Backend developers need to connect browser sessions to application traces. The existing AI exporter attaches request IDs only to its exported AI spans.

  • Add an optional context processor for general application spans in the separate posthog-go/otel module.
  • Add HTTP middleware that reads the existing headers, enriches an incoming span, and ends span context when the handler returns or panics.
  • Preserve explicit span attributes, the application provider, sampling, and exporters. The core SDK keeps its existing dependencies.

Public API: ContextSpanProcessor, NewContextSpanProcessor(client), and NewRequestContextMiddleware(client, next, ...posthog.RequestContextOption) in posthogotel. Core clients get an IsEnabled() method on their concrete types only, not on the Client interface, so existing custom clients still compile.

  • The processor and wrapper take the PostHog client they belong to. They add no IDs when it is nil, disabled (empty API key), or closed.
  • Each processor checks only its own client, so separate clients stay independent. There is no global consent gate.
  • A custom client wrapper counts as enabled unless it implements IsEnabled(), the same fallback .NET uses.

Important

When this ships, otel/go.mod must require the core release that adds IsEnabled(). Against an older core, disabled and closed clients count as enabled.

Use the optional wrapper for HTTP lifetime checks. Detached work needs a fresh context to opt in to its own IDs. Manual context has no automatic lifetime.

Tracks sdk-specs#128. Implements the proposed general-span contract in sdk-specs#129. The AI processor keeps its separate attribute contract.

💚 How did you test it?

  • Added table cases for missing IDs, explicit attributes, header options, sanitization, and full span capacity.
  • Added request tests for success, panic, concurrent requests, late work, sampled-out spans, and an unreadable custom span.
  • Added table cases for nil, empty-key, closed, enabled, and wrapper clients, each through the processor and the wrapper. A two-client test closes one client and checks that the other still adds IDs.
  • Ran go test ./... (2,011 tests) and go test -race ./... in the core module, and the race tests (99) in the otel module. A feature-flag race test unrelated to this change failed once and passed on rerun.
  • Verified repeated shutdown stops later enrichment without changing existing spans.
  • Ran the nested module build, vet, and race tests on Go 1.26.4 and the Go 1.25.0 minimum.
  • Checked the core public API snapshot and a temporary module against the pinned released core SDK.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

Added the equivalent minor changeset file directly.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

GPT-6 (Codex) implemented this change with PostHog Desktop. The independent gpt-6-astra pass added the private shutdown guard and its regression test. Used the writing-tests, writing-code-comments, writing-user-facing-copy, and writing-pr-descriptions skills. The bridge uses public OpenTelemetry span APIs and keeps HTTP lifetime state in the optional module. Claude Opus 5.5 (claude-opus-5-5) bound the span check to its client after a cross-SDK privacy review. Human review is required.


Created with PostHog Desktop

Generated-By: PostHog Desktop
Task-Id: 5681be44-4986-48db-b140-1bda88f75f96
@pauldambra pauldambra self-assigned this Oct 17, 2026
@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

posthog-go Compliance Report

Date: 2026-10-23 16:58:13 UTC
Duration: 169234ms

✅ All Tests Passed!

111/111 tests passed


Capture_V1 Tests

✅ 94/94 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint ✅ 9ms
Endpoint And Method.Does Not Use Legacy Endpoints ✅ 6ms
Required Headers.Has Authorization Bearer Header ✅ 7ms
Required Headers.Has Content Type Json ✅ 5ms
Required Headers.Has Posthog Sdk Info Format ✅ 7ms
Required Headers.Has Posthog Attempt Header ✅ 6ms
Required Headers.Has Posthog Request Id ✅ 6ms
Required Headers.Has Posthog Request Timestamp ✅ 5ms
Required Headers.Has User Agent ✅ 7ms
Body Format.Body Has Created At And Batch ✅ 6ms
Body Format.No Api Key In Body ✅ 6ms
Body Format.No Sent At In Body ✅ 6ms
Event Format.Event Has Required Root Fields ✅ 6ms
Event Format.Event Uuid Is Valid ✅ 5ms
Event Format.Event Timestamp Is Rfc3339 ✅ 7ms
Event Format.Distinct Id Is String ✅ 6ms
Event Format.Distinct Id At Root Not Properties ✅ 6ms
Event Format.Custom Properties Preserved ✅ 6ms
Event Format.Set Properties Preserved ✅ 6ms
Event Format.Set Once Properties Preserved ✅ 15ms
Event Format.Groups Properties Preserved ✅ 7ms
Event Format.Sdk Generates Uuid If Not Provided ✅ 6ms
Event Format.Event Has Required Root Fields Batch ✅ 8ms
Event Format.Event Uuid Is Valid Batch ✅ 8ms
Event Format.Event Timestamp Is Rfc3339 Batch ✅ 8ms
Event Format.Distinct Id Is String Batch ✅ 8ms
Event Format.Distinct Id At Root Not Properties Batch ✅ 9ms
Event Format.Custom Properties Preserved Batch ✅ 8ms
Event Format.Set Properties Preserved Batch ✅ 8ms
Event Format.Set Once Properties Preserved Batch ✅ 19ms
Event Format.Groups Properties Preserved Batch ✅ 9ms
Event Format.Sdk Generates Uuid If Not Provided Batch ✅ 8ms
Batch Behavior.Multiple Events In Single Batch ✅ 31ms
Batch Behavior.Batch Envelope Smoke ✅ 9ms
Batch Behavior.Flush With No Events Sends Nothing ✅ 4ms
Batch Behavior.Flush At Triggers Batch ✅ 1007ms
Batch Behavior.Created At Reflects Batch Creation Time ✅ 8ms
Deduplication.Generates Unique Uuids ✅ 15ms
Deduplication.Different Events Same Content Different Uuids ✅ 9ms
Deduplication.Preserves Uuid On Retry ✅ 5134ms
Deduplication.Preserves Timestamp On Retry ✅ 5136ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 5138ms
Deduplication.No Duplicate Events In Batch ✅ 32ms
Header Behavior On Retry.Attempt Header Starts At One ✅ 6ms
Header Behavior On Retry.Attempt Header Increments On Retry ✅ 10141ms
Header Behavior On Retry.Request Id Preserved On Retry ✅ 5136ms
Header Behavior On Retry.Different Requests Have Different Request Ids ✅ 2015ms
Header Behavior On Retry.Request Timestamp Changes On Retry ✅ 5138ms
Response Format Validation.Success Response Has Uuid Keyed Results ✅ 8ms
Response Format Validation.Success Response Has Ok For Each Event ✅ 27ms
Response Format Validation.Success No Retry After When All Ok ✅ 28ms
Response Format Validation.Success Retry After Present When Retry Events ✅ 129ms
Response Format Validation.Success No Retry After When Drop Only ✅ 28ms
Response Format Validation.Response Echoes Request Id ✅ 7ms
Retry Behavior.Retries On 408 ✅ 5135ms
Retry Behavior.Retries On 500 ✅ 5136ms
Retry Behavior.Retries On 503 ✅ 5136ms
Retry Behavior.Retries On 504 ✅ 5134ms
Retry Behavior.Retryable Errors Have Retry After ✅ 2134ms
Retry Behavior.Respects Retry After On Retryable Error ✅ 8138ms
Retry Behavior.Does Not Retry On 400 ✅ 2134ms
Retry Behavior.Does Not Retry On 401 ✅ 2133ms
Retry Behavior.Does Not Retry On 402 ✅ 2135ms
Retry Behavior.Does Not Retry On 413 ✅ 2131ms
Retry Behavior.Does Not Retry On 415 ✅ 2133ms
Retry Behavior.Non Retryable Errors Have No Retry After ✅ 2134ms
Retry Behavior.Implements Backoff ✅ 15145ms
Retry Behavior.Max Retries Respected ✅ 15146ms
Partial Batch Handling.Handles 200 Full Success ✅ 2010ms
Partial Batch Handling.Handles 200 With All Ok ✅ 3033ms
Partial Batch Handling.Does Not Retry Dropped Events ✅ 3031ms
Partial Batch Handling.Does Not Retry Limited Events ✅ 3034ms
Partial Batch Handling.Prunes Ok Events On Partial Retry ✅ 5136ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry ✅ 5140ms
Partial Batch Handling.Retries Only Retry Events From Partial ✅ 5135ms
Partial Batch Handling.Partial Retry Preserves Uuids ✅ 5138ms
Partial Batch Handling.Partial Retry Attempt Header Increments ✅ 5136ms
Partial Batch Handling.Partial Retry Request Id Preserved ✅ 5138ms
Partial Batch Handling.Respects Retry After On Partial ✅ 5134ms
Partial Batch Handling.Unknown Result Treated As Terminal ✅ 3028ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry ✅ 3035ms
Compression.Sends Gzip Content Encoding ✅ 18ms
Compression.No Content Encoding When Disabled ✅ 18ms
Compression.Compressed Body Is Decompressible ✅ 17ms
Error Handling.Does Not Retry On Unknown 4Xx ✅ 2132ms
Event Options.Cookieless Mode Override ✅ 8ms
Event Options.Disable Skew Correction Override ✅ 6ms
Event Options.Process Person Profile Override ✅ 7ms
Event Options.Product Tour Id Override ✅ 6ms
Event Options.Unset Options Omitted ✅ 16ms
Event Options.Options Override In Batch ✅ 9ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties ✅ 16ms
Geoip And Historical Migration.Historical Migration Set In Body ✅ 7ms
Geoip And Historical Migration.Historical Migration Absent By Default ✅ 6ms

Feature_Flags Tests

✅ 17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id ✅ 16ms
Request Payload.Flags Request Uses V2 Query Param ✅ 16ms
Request Payload.Flags Request Hits Flags Path Not Decide ✅ 17ms
Request Payload.Flags Request Omits Authorization Header ✅ 16ms
Request Payload.Token In Flags Body Matches Init ✅ 17ms
Request Payload.Groups Round Trip ✅ 16ms
Request Payload.Groups Default To Empty Object ✅ 16ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False ✅ 16ms
Request Payload.Disable Geoip Omitted Defaults To False ✅ 17ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key ✅ 16ms
Request Lifecycle.No Flags Request On Init Alone ✅ 2ms
Request Lifecycle.No Flags Request On Normal Capture ✅ 6ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests ✅ 30ms
Request Lifecycle.Mock Response Value Is Returned To Caller ✅ 16ms
Retry Behavior.Retries Flags On 502 ✅ 19ms
Retry Behavior.Retries Flags On 504 ✅ 18ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event ✅ 18ms

Generated-By: PostHog Desktop
Task-Id: 5681be44-4986-48db-b140-1bda88f75f96
@pauldambra
pauldambra marked this pull request as ready for review October 17, 2026 14:44
@pauldambra
pauldambra requested a review from a team as a code owner October 17, 2026 14:44
@pauldambra

pauldambra commented Oct 17, 2026 •

Copy link
Copy Markdown
Member Author

Note

🤖 Automated comment by QA Swarm — not written by a human

Verdict: ✅ APPROVE (privacy round @ 40432bb)

The processor and HTTP wrapper now take their PostHog client. A nil, disabled, or closed client adds no span IDs. Separate clients stay independent. A custom wrapper without IsEnabled() counts as enabled, the same as .NET.

Checks

  • 12 new table cases fail before the fix and pass after it.
  • Core tests (2,011) and race tests pass. The otel module race tests (99) pass. vet, gofmt, go mod tidy, and the API diff are clean.

Before release

otel/go.mod must require the core release that adds IsEnabled(). Against an older core, disabled clients count as enabled.

Earlier rounds

Verdict: ✅ APPROVE (round 1 @ 56f84cf)

No remaining actionable finding in the reviewed change. The optional module boundary, request lifetime, explicit attributes, and repeated processor shutdown were checked.

Key findings

  • None remain in this revision. Existing focused tests and completed independent review coverage were reused.
  • Human review and the shared specification decision remain required. This comment does not approve a merge or release.

Convergence

No remaining duplicate finding.

Reviewer summaries

Reviewer Assessment
Router (gpt-6-sol) Complexity HIGH, confidence HIGH. Completed independent API and concurrency coverage was reused. No further delegation was needed.

Automated by QA Swarm. Human review is required. This comment does not approve a merge or release.

Generated-By: PostHog Desktop
Task-Id: bfd18848-e498-4352-a58a-b9ba85de4198

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostHog Review · Standard · reviewed 40432bb: 1 Must fix.

Comment thread otel/context_span_processor.go
@pauldambra
pauldambra merged commit 94ff576 into main Oct 26, 2026
35 checks passed
@pauldambra
pauldambra deleted the posthog/go-http-span-context branch October 26, 2026 22:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants