fix(cli): Stop unbounded debug trace span retention in ApiServer - #6696
Open
chelsealong wants to merge 1 commit into
Open
fix(cli): Stop unbounded debug trace span retention in ApiServer#6696chelsealong wants to merge 1 commit into
chelsealong wants to merge 1 commit into
Conversation
ApiServer.get_fast_api_app() unconditionally registered two in-memory span exporters (ApiServerSpanExporter, InMemoryExporter) on the global tracer provider. Their retained span/attribute dicts are never pruned, so any long-lived server serving through ApiServer.get_fast_api_app() (web=False) leaked memory for the life of the process. The only reader of this data is DevServer's /debug/trace endpoint (web=True), so the exporters are now only registered when _registers_debug_trace_exporters is set, which DevServer overrides to True. ApiServer no longer registers them at all. Fixes google#6692
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
ApiServer.get_fast_api_app()unconditionally registered two in-memoryspan processors (
ApiServerSpanExporter,InMemoryExporter) on the globaltracer provider.
InMemoryExporter.export()appends every span to anunbounded list and
ApiServerSpanExporter.export()writes every span'sattributes into a dict, neither of which is ever pruned.
clear()existsbut has no callers. The only reader of this retained data is
DevServer's/debug/traceendpoint (used byadk web), so any server started withweb=False(e.g. viaadk api_serverorget_fast_api_app(..., web=False))leaked memory for the life of the process with no way to opt out.
Solution:
Added a
_registers_debug_trace_exportersclass attribute onApiServer(default
False), and only build/register the two debug-trace spanprocessors when it's set.
DevServeroverrides it toTrue, since it'sthe only place that reads the retained data (
AdkWebServerinherits thisfrom
DevServer). PlainApiServer(production/web=False) now sets uptelemetry with no internal exporters, so no spans are retained and nothing
observable changes for
DevServer/adk webusers.Testing Plan
Unit Tests:
Added
test_debug_trace_exporters_only_registered_for_dev_serverintests/unittests/cli/test_fast_api.py, which patchesgoogle.adk.cli.api_server._setup_telemetryand asserts it is called withinternal_exporters=[]whenweb=Falseand with the two debug-trace spanprocessors when
web=True.Verified the new test fails without the fix (checked out
src/google/adk/cli/api_server.py/dev_server.pyfrom the parent commitand reran):
With the fix applied:
Also ran
isort --check-onlyandpyink --checkon the changed files;both report no issues.
Manual End-to-End (E2E) Tests:
Followed the repro from the issue:
get_fast_api_app(agents_dir=..., web=False)now sets up the tracer provider with zero internal exporters, so
InMemoryExporter/ApiServerSpanExporterare never instantiated as spanprocessors and
len(memory_exporter._spans)no longer grows as requestsare served.
adk web(web=True,DevServer) is unaffected — the/debug/trace/{event_id}endpoint continues to work exactly as before,covered by the existing
test_debug_tracetest.Checklist
Additional context
This PR was prepared with AI assistance (Claude Code), reviewed and
verified by me before pushing.