Commit 7d29bfa
authored
When Agent Engine telemetry is enabled
(GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY=true) and Cloud Trace
export is configured on the global OTel tracer provider, the
BigQuery Agent Analytics plugin causes every instrumented operation
to appear as TWO spans in Cloud Trace — the framework's real span
plus a duplicate plugin-owned span. Reported in
GoogleCloudPlatform/BigQuery-Agent-Analytics-SDK#94 (also at
haiyuan-eng-google#94) by a regular BQAA + Agent Engine user.
Root cause
----------
TraceManager.push_span() called tracer.start_span(...) purely as an
ID carrier, so the plugin could populate BigQuery span_id /
parent_span_id columns. The author was aware of one related class
of pitfall (re-parenting the framework's spans — fixed by not
attaching the plugin span to the ambient context, see google#4561), but
that guard does not stop the plugin span from going through the
configured SpanProcessor → BatchSpanProcessor → Cloud Trace exporter.
Once Agent Engine wires the global provider to Cloud Trace, every
push/pop pair is exported.
The plugin already tracked every parent/child relationship on its
own contextvar-backed _SpanRecord stack — the OTel span object was
incidental to correctness.
Fix (scoped to TraceManager methods + _SpanRecord)
-------------------------------------------------
* _SpanRecord no longer holds an OTel span object. It carries
span_id, trace_id, owns_span, start_time_ns, first_token_time.
* push_span generates a 16-hex span_id directly and inherits
trace_id by precedence: top of internal stack → ambient OTel span
→ freshly generated 32-hex. No tracer.start_span call.
* attach_current_span extracts trace_id/span_id from the ambient
span (the existing path the framework already supports) and
stores them as plain strings — no longer holds the OTel object.
* pop_span / clear_stack drop the .end()/.start_time machinery
since there is no OTel span to end.
* get_trace_id / get_start_time read directly from the record.
The signatures of push_span / attach_current_span / pop_span /
clear_stack / get_trace_id / get_start_time are unchanged.
Module-level `tracer = trace.get_tracer(...)` is retained for ABI
compat (currently unused by plugin code; can be removed in a
follow-up if no external consumers are identified).
attach_current_span() is otherwise untouched — it only observed
the ambient span; it never created one. That path was already
correct and remains so.
Cross-system correlation
------------------------
BigQuery rows still carry trace_id, inherited from the ambient
Agent Engine / Runner span when present. Joining `agent_events` to
Cloud Trace by trace_id continues to work end-to-end.
Tests
-----
Three existing tests that were directly asserting the bug
(test_otel_integration, test_otel_integration_real_provider,
test_clear_stack_ends_owned_spans) are rewritten as inverted
regression guards:
* test_push_pop_does_not_call_tracer_start_span
* test_push_pop_does_not_export_spans_through_real_provider
* test_clear_stack_does_not_export_spans
Each asserts that the corresponding code path does NOT export an
OTel span via an InMemorySpanExporter wired to a real provider, or
does NOT invoke tracer.start_span via a mock spy.
Four new tests added to lock in the lifecycle / inheritance
contracts the plugin must keep:
* test_push_span_inherits_ambient_trace_id — when an ambient
OTel span exists (the Agent Engine pattern), the plugin's
trace_id matches it.
* test_llm_request_response_share_span_id_contract — the
paired LLM_REQUEST / LLM_RESPONSE rows share one span_id (the
documented BQAA join contract).
* test_tool_starting_completed_share_span_id_contract — same
invariant for the tool lifecycle pair.
* test_streaming_llm_response_shares_span_id_until_final_contract
— multiple partial LLM_RESPONSE callbacks reuse the same
span_id and only the final fire pops the span. Prevents a
future "dedupe" of streaming rows from breaking the contract.
226/229 plugin tests pass (6 skipped for unrelated optional
deps); pyink + isort clean.
Refs:
- haiyuan-eng-google/BigQuery-Agent-Analytics-SDK#94 (reported by a customer)
- google#4561 (prior fix for span-hierarchy re-parenting)
- google#4645 (prior fix for trace_id fracture)
1 parent e5c0d8b commit 7d29bfa
2 files changed
Lines changed: 315 additions & 154 deletions
File tree
- src/google/adk/plugins
- tests/unittests/plugins
Lines changed: 83 additions & 91 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
634 | 634 | | |
635 | 635 | | |
636 | 636 | | |
637 | | - | |
638 | | - | |
639 | | - | |
640 | | - | |
641 | | - | |
642 | | - | |
643 | | - | |
644 | | - | |
645 | | - | |
646 | | - | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
647 | 662 | | |
648 | 663 | | |
649 | | - | |
650 | 664 | | |
| 665 | + | |
651 | 666 | | |
652 | 667 | | |
653 | 668 | | |
| |||
689 | 704 | | |
690 | 705 | | |
691 | 706 | | |
692 | | - | |
| 707 | + | |
693 | 708 | | |
694 | 709 | | |
695 | | - | |
696 | | - | |
697 | | - | |
| 710 | + | |
698 | 711 | | |
699 | | - | |
700 | | - | |
701 | | - | |
702 | | - | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
703 | 717 | | |
704 | 718 | | |
705 | 719 | | |
| |||
708 | 722 | | |
709 | 723 | | |
710 | 724 | | |
711 | | - | |
712 | | - | |
713 | | - | |
714 | | - | |
715 | | - | |
716 | | - | |
717 | | - | |
718 | | - | |
719 | | - | |
720 | | - | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
721 | 744 | | |
| 745 | + | |
722 | 746 | | |
723 | 747 | | |
724 | | - | |
725 | | - | |
726 | | - | |
727 | | - | |
728 | | - | |
729 | | - | |
730 | | - | |
731 | | - | |
732 | 748 | | |
733 | | - | |
734 | | - | |
735 | | - | |
736 | | - | |
737 | | - | |
738 | | - | |
739 | | - | |
| 749 | + | |
| 750 | + | |
740 | 751 | | |
741 | | - | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
742 | 759 | | |
743 | 760 | | |
744 | | - | |
745 | 761 | | |
| 762 | + | |
746 | 763 | | |
747 | 764 | | |
748 | 765 | | |
749 | | - | |
750 | | - | |
751 | | - | |
| 766 | + | |
752 | 767 | | |
753 | 768 | | |
754 | 769 | | |
755 | 770 | | |
756 | 771 | | |
757 | 772 | | |
758 | 773 | | |
759 | | - | |
| 774 | + | |
760 | 775 | | |
761 | | - | |
762 | | - | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
763 | 780 | | |
764 | 781 | | |
765 | 782 | | |
766 | | - | |
767 | | - | |
768 | | - | |
769 | | - | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
770 | 787 | | |
771 | | - | |
| 788 | + | |
| 789 | + | |
772 | 790 | | |
773 | 791 | | |
774 | | - | |
775 | 792 | | |
| 793 | + | |
776 | 794 | | |
777 | 795 | | |
778 | 796 | | |
779 | | - | |
780 | 797 | | |
781 | | - | |
782 | | - | |
| 798 | + | |
783 | 799 | | |
784 | 800 | | |
785 | 801 | | |
| |||
828 | 844 | | |
829 | 845 | | |
830 | 846 | | |
831 | | - | |
| 847 | + | |
832 | 848 | | |
833 | | - | |
834 | | - | |
| 849 | + | |
| 850 | + | |
835 | 851 | | |
836 | 852 | | |
837 | 853 | | |
| |||
841 | 857 | | |
842 | 858 | | |
843 | 859 | | |
844 | | - | |
845 | | - | |
846 | | - | |
847 | | - | |
848 | | - | |
849 | | - | |
850 | | - | |
851 | | - | |
852 | | - | |
853 | | - | |
854 | | - | |
| 860 | + | |
855 | 861 | | |
856 | 862 | | |
857 | 863 | | |
858 | 864 | | |
859 | 865 | | |
860 | | - | |
861 | | - | |
862 | | - | |
863 | | - | |
864 | | - | |
865 | | - | |
866 | | - | |
| 866 | + | |
867 | 867 | | |
868 | 868 | | |
869 | 869 | | |
| |||
894 | 894 | | |
895 | 895 | | |
896 | 896 | | |
897 | | - | |
| 897 | + | |
898 | 898 | | |
899 | 899 | | |
900 | 900 | | |
901 | 901 | | |
902 | | - | |
903 | | - | |
904 | | - | |
905 | | - | |
906 | | - | |
907 | | - | |
908 | | - | |
909 | | - | |
910 | 902 | | |
911 | 903 | | |
912 | 904 | | |
| |||
0 commit comments