Skip to content

perf(telemetry): skip non-recording LLM trace work#6371

Closed
dexhunter wants to merge 1 commit into
google:mainfrom
dexhunter:perf/skip-non-recording-llm-trace
Closed

perf(telemetry): skip non-recording LLM trace work#6371
dexhunter wants to merge 1 commit into
google:mainfrom
dexhunter:perf/skip-non-recording-llm-trace

Conversation

@dexhunter

@dexhunter dexhunter commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:

trace_call_llm builds telemetry configuration and serializes request and response content before writing span attributes, even when the supplied or current span is not recording. A non-recording span cannot retain those values, so large LLM payloads incur avoidable CPU work.

Solution:

Resolve the span first and return when span.is_recording() is false. Recording spans keep the existing telemetry and attribute path unchanged. The regression test covers both explicitly supplied and current spans and proves that neither request nor response serialization runs on the non-recording path.

This addresses one narrow source of the repeated serialization reported in #4233; it does not claim to eliminate every serialization path in that issue.

Performance

The strict same-workload evaluator enables content capture and uses a large deterministic response. Each reported run is itself the median of nine benchmark batches.

Revision Three repeated evaluator results (us/call) Median (us/call)
Current main 153.056, 153.571, 154.278 153.571
This PR 0.122033, 0.122233, 0.125897 0.122233

That is a 99.92% reduction in this helper's non-recording-span overhead. This is a helper-level CPU benchmark, not an end-to-end agent or model latency claim. The evaluator also verifies that recording-span attributes and serialization behavior remain unchanged.

The early-guard direction was identified through autoresearch with Weco. The comparable baseline, patch, and hardened regression are recorded in this public autoresearch trajectory.

Testing Plan

Unit Tests:

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

Results on the final rebased patch:

  • pytest tests/unittests/telemetry/test_spans.py -q: 80 passed, 1 skipped
  • strict evaluator: three repeated upstream and candidate runs passed all recording/non-recording correctness gates
  • uvx ruff check --fix src/google/adk/telemetry/tracing.py tests/unittests/telemetry/test_spans.py: passed
  • repository hooks and git diff --check: passed

Manual End-to-End (E2E) Tests:

Not applicable for this tracing guard. The focused unit suite and strict evaluator exercise explicit and current spans, recording and non-recording behavior, request and response serialization, telemetry-disabled cases, tools/no-tools cases, and serialization fallback behavior without an external model request.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have added tests that prove my fix is effective.
  • New and existing focused unit tests pass with my changes.
  • I have manually tested my changes end-to-end. (Not applicable; see above.)

Additional context

The patch is intentionally limited to the non-recording trace path; recording-span behavior remains covered and unchanged.

@adk-bot adk-bot added the tracing [Component] This issue is related to OpenTelemetry tracing label Jul 10, 2026
@dexhunter
dexhunter marked this pull request as draft July 11, 2026 16:27
@dexhunter
dexhunter force-pushed the perf/skip-non-recording-llm-trace branch from c6b6d96 to 4da52ff Compare July 12, 2026 10:47
@dexhunter
dexhunter marked this pull request as ready for review July 13, 2026 11:25
Avoid telemetry configuration and payload serialization when the active span cannot record attributes.

Refs google#4233
@dexhunter
dexhunter force-pushed the perf/skip-non-recording-llm-trace branch from 4da52ff to 8d452b5 Compare July 13, 2026 12:01
copybara-service Bot pushed a commit that referenced this pull request Jul 13, 2026
Merge #6371

Fixes #4233

PiperOrigin-RevId: 947164166
@adk-bot

adk-bot commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Thank you @dexhunter for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit 0c63a89.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Jul 13, 2026
@adk-bot adk-bot closed this Jul 13, 2026
@dexhunter

dexhunter commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main at fa884e3d. The feature diff remains limited to tracing.py and test_spans.py.

Validation after the rebase:

  • 81 passed in tests/unittests/telemetry/test_spans.py
  • git diff --check upstream/main passed
  • the PR branch is clean

The remaining repository-wide failures reproduce outside this patch on current main:

  • tests/unittests/test_verify_snippets.py points to the missing open_source_workspace/.agents/skills/adk-verify-snippets/scripts/verify_md.py path, while the script is currently under .agents/...
  • end-of-file-fixer modifies the current-main .agents/skills/adk-style/references/imports.md
  • the mypy baseline comparison can report the same lite_llm.py enum union in a different literal order as a new error, despite identical source and equal error counts

I left those unrelated upstream issues out of this focused telemetry PR.

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

Labels

merged [Status] This PR is merged tracing [Component] This issue is related to OpenTelemetry tracing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants