Skip to content

Conversation

@mportdata
Copy link

@mportdata mportdata commented Dec 27, 2025

Link to Issue or Description of Change

  1. Link to an existing issue (if applicable):
  • Partially closes: #2792

Problem:
Telemetry spans are always emitted. There wasn’t a
consistent, ADK‑level switch to disable all
telemetry.

Solution:
I've added a disable_telemetry attribute to the BaseAgent class and 2 environment variables OTEL_SDK_DISABLED and ADK_TELEMETRY_DISABLED. When any of the above are set to true then spans are no longer started resulting in tracing being disabled.

Telemetry in Google ADK made up of spans. These are created (and attributes are set on them) in 5 places in Google ADK:

  • in /src/google/adk/runners.py for runner invocation
  • in /src/google/adk/agents/base_agent.py for agent invocation
  • in /src/google/adk/flows/llm_flows/base_llm_flow.py for data being sent to the model
  • in /src/google/adk/models/gemini_context_cache_manager.py for cache creation
  • in /src/google/adk/models/google_llm.py for cache actions

To centralize control over telemetry, I introduced a helper function is_telemetry_enabled. This function returns False if telemetry is disabled via:

  • the new disable_telemetry attribute on BaseAgent, or
  • either of the environment variables OTEL_SDK_DISABLED or ADK_TELEMETRY_DISABLED.

All span creation points in Google ADK now use this function to decide whether tracing should occur.

When telemetry is disabled, span creation is replaced with contextlib.nullcontext(), which preserves the existing with-block structure while performing no operation. This avoids conditional branching at each call site and ensures that no spans are started and no telemetry data is emitted.

When telemetry is enabled, the existing tracing behavior is unchanged and real spans are created as before.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my
    change.
  • All unit tests pass locally.

Integration Tests:

I have created unit tests in the following file /tests/unittests/telemetry/test_telemetry_disable_agent.py
These tests monkey patch the start_as_current_span method used for tracing throughout Google ADK and ensures it is only called when tracing is enabled both via the agent attribute or the environment variables. Conversely I have also tested here that telemetry is returned when expected (when not disabled by either the attribute or the environment variables).

Summary:

  • uv run pytest tests/unittests
  • I have added unit tests in /tests/unittests/telemetry/test_telemetry_disable_agent.py - these unit tests check that zero spans are emitted when telemetry is disabled either by the agent attribute or the environment variables, and that spans are emitted when not set by either.

Manual End-to-End (E2E) Tests:

A simple ADK agent has been tested by running uv run adk web with the disable_telemetry=True behaved as expected.

from google.adk.agents import Agent

root_agent = Agent(
    name="search_assistant",
    model="gemini-2.0-flash-lite",
    instruction="You are a helpful assistant.",
    description="You are a friendly agent",
    disable_telemetry=True,
)
image

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own
    code.
  • I have commented my code, particularly in
    hard-to-understand areas - not required for this change
  • I have added tests that prove my fix is
    effective or that my feature works.
  • New and existing unit tests pass locally
    with my changes.
  • I have manually tested my changes end-to-
    end.
  • Any dependent changes have been merged and
    published in downstream modules.

@google-cla
Copy link

google-cla bot commented Dec 27, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @mportdata, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request provides a robust and flexible way to disable telemetry across the Agent Development Kit (ADK). Previously, telemetry spans were always emitted, but now users can control this behavior through a new disable_telemetry attribute on the BaseAgent class or by setting specific environment variables. This enhancement improves user control over data emission and integrates seamlessly into the existing agent execution flow, ensuring that tracing is only active when desired.

Highlights

  • Telemetry Disabling Mechanism: Introduced a new disable_telemetry attribute to the BaseAgent class, allowing users to disable telemetry directly on an agent instance.
  • Environment Variable Control: Added support for two environment variables, OTEL_SDK_DISABLED and ADK_TELEMETRY_DISABLED, which can be set to 'true' or '1' to globally disable telemetry spans.
  • Centralized Telemetry Utility: A new utility module, telemetry_utils.py, was added to centralize the logic for checking whether telemetry is enabled, simplifying checks across the codebase.
  • Refactored Agent Execution Flow: The BaseAgent's run_async and run_live methods were refactored to use a new _run_callbacks_and_impl method, which conditionally starts telemetry spans based on the new disabling mechanisms.
  • Comprehensive Testing: New unit and integration tests have been added to thoroughly verify that telemetry is correctly disabled when the attribute or environment variables are set.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@adk-bot adk-bot added the tracing [Component] This issue is related to OpenTelemetry tracing label Dec 27, 2025
@adk-bot
Copy link
Collaborator

adk-bot commented Dec 27, 2025

Response from ADK Triaging Agent

Hello @mportdata, thank you for your contribution!

Before we can review your pull request, you'll need to sign our Contributor License Agreement (CLA). It seems that the CLA check has failed.

You can review and sign the CLA here: https://cla.developers.google.com/

Once the CLA is signed, we can proceed with the review. Thanks!

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new disable_telemetry flag to BaseAgent and Gemini LLM classes, allowing users to explicitly control OpenTelemetry tracing for agents and LLM interactions. The changes involve adding the is_telemetry_enabled utility function and refactoring existing code to conditionally apply tracing spans based on this flag. Specifically, run_async and run_live methods in base_agent.py, _call_llm_async in base_llm_flow.py, and tool execution in functions.py now check this flag before creating spans. The GeminiContextCacheManager and Gemini LLM also incorporate this flag to conditionally trace cache creation and LLM calls. Additionally, many string literals across various files were updated from single quotes to double quotes for consistency, and minor formatting adjustments were made to import statements. Review comments highlighted opportunities to further reduce code duplication by using contextlib.nullcontext for conditional tracing and corrected a typo in a docstring example.

Add unit tests for agent and Gemini telemetry disable flags including env var gating.

Add integration test asserting spans are emitted when enabled and omitted when ADK_TELEMETRY_DISABLED is set.
Add integration coverage for OTEL_SDK_DISABLED, ADK_TELEMETRY_DISABLED, and agent.disable_telemetry.
Add back _create_gemini_cache wrapper for compatibility after telemetry refactor.

Apply autoformat across touched files.
@mportdata mportdata force-pushed the feature/disable-telemetry branch from 2c1e727 to ba0ce7a Compare December 27, 2025 23:31
Use a nullcontext fallback to avoid duplicating the async run loop when telemetry is disabled.
Use a nullcontext fallback to keep run_live telemetry handling consistent with run_async.
Use nullcontext fallbacks for tracing in base_agent and base_llm_flow.

Run autoformat on telemetry utilities.
Use a nullcontext fallback in _call_llm_with_optional_tracing to avoid branching.
Use early return when telemetry is disabled to avoid branching in execute_tool spans.
Use a nullcontext fallback for cache creation tracing.

Update GEPA sample formatting.
Restore GEPA sample files to upstream main to avoid unrelated formatting changes.
Reuse callback runner with a nullcontext span and avoid quote-only churn.
Reapply only the model telemetry toggle without quote-style churn.
Use nullcontext for live send_data/call_llm spans and skip tool tracing when telemetry is disabled.

Add a shared context_manager_with_span fixture for telemetry tests.
Keep only telemetry logic in google_llm and runners without quote/indent churn.
Use _create_gemini_cache name consistently and update tests.

Revert tracing.py quote-only churn.
@ryanaiagent ryanaiagent self-assigned this Dec 29, 2025
@ryanaiagent
Copy link
Collaborator

Hi @mportdata, Thank you for your contribution! We appreciate you taking the time to submit this pull request.
Can you please fix the lint errors before we can proceed with the review. You can use autoformat.sh

@ryanaiagent ryanaiagent added the request clarification [Status] The maintainer need clarification or more information from the author label Dec 29, 2025
@ryanaiagent
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a mechanism to disable telemetry throughout the ADK. The changes are well-implemented, adding a disable_telemetry flag to agents and honoring OTEL_SDK_DISABLED and ADK_TELEMETRY_DISABLED environment variables. The use of contextlib.nullcontext() to conditionally apply tracing is a clean approach. The new tests are comprehensive and cover the different ways to disable telemetry.

I've found one critical issue in the newly added is_telemetry_enabled utility function, where the docstring and examples are incorrect and misleading. I've provided a detailed comment with a suggested fix for it.

Overall, this is a solid contribution that adds valuable functionality.

@mportdata
Copy link
Author

Hi @mportdata, Thank you for your contribution! We appreciate you taking the time to submit this pull request. Can you please fix the lint errors before we can proceed with the review. You can use autoformat.sh

@ryanaiagent I have ran the autoformatter, committed and pushed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

request clarification [Status] The maintainer need clarification or more information from the author tracing [Component] This issue is related to OpenTelemetry tracing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants