Skip to content

: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.

fix(devex): bound OTel trace flush at process exit (#70964)

Julian Bez0543d12700d9
diff --git a/posthog/otel_instrumentation.py b/posthog/otel_instrumentation.py
index 572429d953e6..db913d513b16 100644
--- a/posthog/otel_instrumentation.py
+++ b/posthog/otel_instrumentation.py
@@ -1,4 +1,5 @@
import os
+import atexit
import typing
import logging
@@ -101,11 +102,21 @@ def initialize_otel():
source_module="otel_instrumentation",
)
- provider = TracerProvider(resource=resource)
+ # shutdown_on_exit=False: the SDK's own atexit hook calls provider.shutdown(),
+ # which joins the BatchSpanProcessor export thread WITHOUT a timeout. If the
+ # OTLP collector is unreachable, that thread sits in the gRPC exporter's
+ # retry/backoff loop (~63s of sleeps per batch, and the exporter's shutdown
+ # flag is only set after the join returns), so every process exit hangs until
+ # SIGKILL — under granian this turns each worker stop into a
+ # "refused to gracefully stop" hard kill. A bounded force_flush gives spans
+ # their best shot at export and then lets the process exit; the export thread
+ # is a daemon, so skipping shutdown() leaks nothing at exit.
+ provider = TracerProvider(resource=resource, shutdown_on_exit=False)
otlp_exporter = OTLPSpanExporter() # Assumes OTLP endpoint is configured via env vars
processor = BatchSpanProcessor(otlp_exporter)
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)
+ atexit.register(lambda: provider.force_flush(timeout_millis=5_000))
logger.info(
"otel_core_components_initialized_successfully",
service_name=service_name,