Commit ef41ddf
feat: Fixing the spans produced by agent calls to have the right parent spans
This change fixes an issue where OpenTelemetry spans generated during agent executions were not correctly associated with their parent spans, leading to fragmented traces.
### Core Changes
* **Explicit Context Propagation**: Instead of relying on `Context.current()`, which can be unreliable in asynchronous RxJava streams, the OTel `Context` is now explicitly captured at entry points (like `Runner.runAsync`) and passed down through method signatures in `BaseAgent`, `BaseLlmFlow`, and `Functions`.
* **RxJava Context Support**: Introduced `Tracing.withContext(Context)`, a new set of transformers (`FlowableTransformer`, etc.) that re-activates a captured OTel context during the subscription of reactive streams. This ensures that any work done inside `flatMap` or `concatMap` remains within the correct trace.
* **Synchronous Scope Management**: Wrapped direct calls to plugins and tool callbacks in `try-with-resources` blocks using `context.makeCurrent()` to ensure the tracing context is active during synchronous execution.
* **Tracing Enhancements**:
* Updated `TracerProvider` to propagate the agent name via a new `AGENT_NAME_CONTEXT_KEY`.
* Improved agent name retrieval from spans using reflection (supporting `ReadableSpan`) when it's not explicitly available in the context.
* Modified span lifecycle management to start the span immediately upon subscription setup (via `Flowable.defer`) rather than waiting for `doOnSubscribe`.
### Impacted Areas
* **`BaseAgent` & `BaseLlmFlow`**: Now strictly pass the parent context to all internal stages (preprocessing, model calls, postprocessing, and callbacks).
* **`Runner`**: Entry points for `runAsync` and `runLive` are now consistently wrapped in an `"invocation"` span that serves as the root for the agent's work.
* **`PluginManager`**: Ensures that plugin-provided callbacks are executed within the trace context captured when the callback was triggered.
* **`Functions`**: Tool execution, including before/after callbacks and response event building, is now correctly parented.
### Note on Reviewer Comments
The reviewer noted a `ComplianceLint` warning regarding pending files in `Functions.java` that needs to be addressed by taking a snapshot.
PiperOrigin-RevId: 8793559581 parent 11ce49e commit ef41ddf
6 files changed
Lines changed: 519 additions & 281 deletions
File tree
- core/src/main/java/com/google/adk
- agents
- flows/llmflows
- plugins
- runner
- telemetry
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
312 | 313 | | |
313 | 314 | | |
314 | 315 | | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
315 | 319 | | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
347 | 356 | | |
348 | 357 | | |
349 | 358 | | |
| |||
387 | 396 | | |
388 | 397 | | |
389 | 398 | | |
390 | | - | |
| 399 | + | |
| 400 | + | |
391 | 401 | | |
392 | 402 | | |
393 | 403 | | |
| |||
397 | 407 | | |
398 | 408 | | |
399 | 409 | | |
400 | | - | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
419 | 428 | | |
420 | 429 | | |
421 | 430 | | |
| |||
0 commit comments