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(temporal): stop double-instrumenting worker trace spans (#67855)

Rossf5724bd17390
diff --git a/posthog/temporal/common/client.py b/posthog/temporal/common/client.py
index c5f9422bf3e0..9971a38664df 100644
--- a/posthog/temporal/common/client.py
+++ b/posthog/temporal/common/client.py
@@ -23,6 +23,7 @@ async def connect(
server_root_ca_cert: str | None = None,
settings: Any | None = django_settings,
use_pydantic_converter: bool = False,
+ add_otel_tracing_interceptor: bool = True,
) -> Client:
tls: TLSConfig | bool = False
if client_cert and client_key:
@@ -42,12 +43,22 @@ async def connect(
payload_codec=EncryptionCodec.from_settings(settings=settings),
)
+ # The classic TracingInterceptor injects trace context into workflow start headers (so a
+ # caller's span becomes the parent of the workflow) AND creates spans for activity/workflow
+ # execution on any worker built from this client. Worker processes disable it via
+ # `add_otel_tracing_interceptor=False` because they trace execution through the worker's
+ # OpenTelemetryPlugin instead; leaving both on double-instruments every activity and workflow.
+ # Non-worker callers (Django, Celery, the CLI, schedules) keep it for start-context propagation.
+ interceptors: list[temporalio.client.Interceptor] = []
+ if add_otel_tracing_interceptor:
+ interceptors.append(temporalio.contrib.opentelemetry.TracingInterceptor())
+
client = await Client.connect(
f"{host}:{port}",
namespace=namespace,
tls=tls,
runtime=runtime,
- interceptors=[temporalio.contrib.opentelemetry.TracingInterceptor()],
+ interceptors=interceptors,
data_converter=data_converter,
)
return client
diff --git a/posthog/temporal/common/worker.py b/posthog/temporal/common/worker.py
index 3c6176192454..1a3eac688125 100644
--- a/posthog/temporal/common/worker.py
+++ b/posthog/temporal/common/worker.py
@@ -350,6 +350,10 @@ async def create_worker(
client_key=client_key,
runtime=runtime,
use_pydantic_converter=use_pydantic_converter,
+ # This worker traces activity/workflow execution through the OpenTelemetryPlugin below.
+ # Keeping the client-level TracingInterceptor as well would emit a second, duplicate span
+ # for every activity and workflow, so drop it here.
+ add_otel_tracing_interceptor=False,
)
supported_interceptors = [
interceptor() for interceptor in ALL_INTERCEPTOR_CLASSES if is_task_queue_supported(task_queue, interceptor)