Skip to content

Commit 7248d3b

Browse files
authored
docs(otel): register on the existing provider instead of replacing it
The usage snippet built a new sdktrace.TracerProvider and made it global. For the flagship case — an ADK Go agent inside a service that already has OpenTelemetry configured — this loses AI spans: the OTel global delegation fires only once, so an already-installed provider's tracers (including ADK's, cached at package init) stay bound to it and never reach the PostHog processor, while the replacement provider also drops the user's resource, sampler, and existing exporters. The README prose already said "register it on your TracerProvider", which the code contradicted. Show provider.RegisterSpanProcessor(processor) on the application's own *sdktrace.TracerProvider, and shut down the processor (not the provider) so PostHog's setup does not tear down the user's tracing pipeline. Keeps the fresh-context, error-reported shutdown from the previous fix. Generated-By: PostHog Desktop Task-Id: 01e1bd45-4478-4317-8196-8ddfcfe917bf
1 parent 2109ad3 commit 7248d3b

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

otel/README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,37 @@ go get github.com/posthog/posthog-go/otel
1616

1717
## Usage
1818

19-
`SpanProcessor` is the recommended integration. Register it on your
20-
`TracerProvider`:
19+
`SpanProcessor` is the recommended integration. Register it on the
20+
`*sdktrace.TracerProvider` your application already owns — rather than replacing
21+
the global provider with a new one — so your resource, sampler, and existing
22+
exporters are kept and tracers already handed out (such as ADK Go's) route
23+
through it:
2124

2225
```go
2326
processor, err := posthogotel.NewSpanProcessor(ctx, "phc_your_project_api_key")
2427
if err != nil {
2528
log.Fatal(err)
2629
}
27-
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(processor))
28-
otel.SetTracerProvider(provider)
2930

30-
// Flush buffered spans on shutdown. Use a fresh context, not one that is
31-
// already canceled (for example from signal.NotifyContext): the SDK skips
32-
// the final export on a canceled context and silently drops queued spans.
31+
// provider is your existing *sdktrace.TracerProvider.
32+
provider.RegisterSpanProcessor(processor)
33+
34+
// Shut down the processor — not your provider — to flush its buffered spans.
35+
// Use a fresh context, not one that is already canceled (for example from
36+
// signal.NotifyContext): the SDK skips the final export on a canceled context
37+
// and silently drops queued spans.
3338
defer func() {
3439
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
3540
defer cancel()
36-
if err := provider.Shutdown(shutdownCtx); err != nil {
37-
log.Printf("shutdown tracer provider: %v", err)
41+
if err := processor.Shutdown(shutdownCtx); err != nil {
42+
log.Printf("shutdown posthog processor: %v", err)
3843
}
3944
}()
4045
```
4146

47+
If you don't already have a `TracerProvider`, create one and register the
48+
processor on it, as [`example/`](example) does.
49+
4250
Use `WithHost` for a host other than PostHog US cloud, for example
4351
`posthogotel.WithHost("https://eu.i.posthog.com")`.
4452

0 commit comments

Comments
 (0)