feat: Add telemetry traces to callbacks
Is your feature request related to a problem? Please describe. We are developing an application in a highly regulated industry and need to implement strong guardrails in the form of callbacks. We need to understand the latency of our guardrails to optimize them and provide a great user experience. The current implementation of telemetry in the package seems to package up the callback tracing inside the outer agent run. So I can only infer the total time of all callbacks.
Describe the solution you'd like Each callback should be traced and appear separately in the trace UI. I believe this would be a trace_callback function in https://github.com/google/adk-python/blob/main/src/google/adk/telemetry.py
Describe alternatives you've considered I can use a third-party logging tool like logfire or opentelemetry-instrumentation-google-genai to add spans to each of the callbacks but it would be much better if this were built in.
Additional context
Callbacks are not broken out:
@sjhatfield Thanks for reporting We are currently reviewing your request ,Its under review Thanks for your patience
I checked 7d3658e with a fake model and an in-memory OTel exporter. Before/after agent and model callbacks all ran, but they only appeared inside the agent or model span, so their individual latency still cannot be measured.
Could I add a child span around each canonical callback in _callback_pipeline, covering the awaited call as well? I would keep the first change separate from plugin tracing and record no arguments or results. Is execute_callback <callback name> suitable, and should these spans use the existing experimental telemetry opt-in?
Hi @sjhatfield, sorry for getting back to you so late.
Quick question, would recording callback duration as a metric work for your use-case?
Recording these as spans leads to a noisy trace, especially for streaming cases, where the callback runs for every chunk:
This issue has been automatically marked as stale because it has not had recent activity for 7 days after a maintainer requested clarification. It will be closed if no further activity occurs within 7 days.