Skip to content

Support for Disabling or Extending Internal OpenTelemetry Tracing in Google ADK (Python) #2792

@laji

Description

@laji

Hi ADK Team,

We are integrating Google ADK with a FastAPI microservice that uses OpenTelemetry for distributed tracing and log correlation. We’ve encountered persistent context propagation errors (e.g., ValueError: <Token ...> was created in a different Context) when both our application and ADK use OpenTelemetry, with async generators and multiprocessing.

Problem:

ADK manages its own OpenTelemetry tracer and context, but does not expose a public API to disable, configure, or extend this tracing.
This leads to context detachment errors and makes it impossible to unify or customize tracing across our stack.
We cannot add custom span attributes, propagate our own context, or inject a custom tracer/span processor/exporter.

Feature Request:

Please provide a way to disable ADK’s internal OpenTelemetry tracing, or
Allow users to inject their own OpenTelemetry tracer, span processor, or exporter, or
Expose hooks to add custom attributes and propagate context, or
Document a recommended pattern for integrating ADK tracing with application-level OpenTelemetry tracing in async/multiprocessing environments.
Use Case:

We want to achieve unified distributed tracing and logging across our FastAPI app and ADK agents, with custom attributes and context propagation, without context errors.

If there’s a workaround or undocumented config, please let us know.

Stack Trace

Failed to detach context Traceback (most recent call last): File "/.pyenv/versions/3.13.0/lib/python3.13/site-packages/opentelemetry/trace/init.py", line 589, in use_span yield span File "/.pyenv/versions/3.13.0/lib/python3.13/site-packages/opentelemetry/sdk/trace/init.py", line 1105, in start_as_current_span yield span File "/.pyenv/versions/3.13.0/lib/python3.13/site-packages/google/adk/runners.py", line 236, in run_async yield event GeneratorExit During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/.pyenv/versions/3.13.0/lib/python3.13/site-packages/opentelemetry/context/init.py", line 155, in detach _RUNTIME_CONTEXT.detach(token) ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^ File "/.pyenv/versions/3.13.0/lib/python3.13/site-packages/opentelemetry/context/contextvars_context.py", line 53, in detach self._current_context.reset(token) ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^ ValueError: <Token var=<ContextVar name='current_context' default={} at 0x106060770> at 0x148bdb580> was created in a different Context Failed to detach context Traceback (most recent call last): File "/.pyenv/versions/3.13.0/lib/python3.13/site-packages/opentelemetry/trace/init.py", line 589, in use_span yield span File "/.pyenv/versions/3.13.0/lib/python3.13/site-packages/opentelemetry/sdk/trace/init.py", line 1105, in start_as_current_span yield span File "/.pyenv/versions/3.13.0/lib/python3.13/site-packages/google/adk/agents/base_agent.py", line 224, in run_async yield event GeneratorExit During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/.pyenv/versions/3.13.0/lib/python3.13/site-packages/opentelemetry/context/init.py", line 155, in detach _RUNTIME_CONTEXT.detach(token) ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^ File "/.pyenv/versions/3.13.0/lib/python3.13/site-packages/opentelemetry/context/contextvars_context.py", line 53, in detach self._current_context.reset(token) ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^ ValueError: <Token var=<ContextVar name='current_context' default={} at 0x106060770> at 0x148bdbc80> was created in a different Context Failed to detach context Traceback (most recent call last): File "/.pyenv/versions/3.13.0/lib/python3.13/site-packages/opentelemetry/trace/init.py", line 589, in use_span yield span File "/.pyenv/versions/3.13.0/lib/python3.13/site-packages/opentelemetry/sdk/trace/init.py", line 1105, in start_as_current_span yield span File "/.pyenv/versions/3.13.0/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 629, in _call_llm_async yield llm_response GeneratorExit During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/.pyenv/versions/3.13.0/lib/python3.13/site-packages/opentelemetry/context/init.py", line 155, in detach _RUNTIME_CONTEXT.detach(token) ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^ File "/.pyenv/versions/3.13.0/lib/python3.13/site-packages/opentelemetry/context/contextvars_context.py", line 53, in detach self._current_context.reset(token) ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^ ValueError: <Token var=<ContextVar name='current_context' default={} at 0x106060770> at 0x148bf0ec0> was created in a different Context

Metadata

Metadata

Assignees

Labels

needs-review[Status] The PR is awaiting review from the maintainertracing[Component] This issue is related to OpenTelemetry tracing

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions