Files
Haran Rajkumar 0a62d39ff9 chore(agents): tag ManagedAgent traffic with a +managed_agent version suffix
ManagedAgent and Gemini(use_interactions_api=True) both reach the Interactions
API and surface identically as tool_name=google-adk in Google's usage pipeline,
with no way to tell them apart. Thread an optional framework_label through
merge_tracking_headers / get_tracking_headers / get_client_labels /
_get_default_labels, and have ManagedAgent emit google-adk/<version>+managed_agent
on the per-request extra_headers it sends to interactions.create, so its traffic
is distinguishable via the tool_version dimension while tool_name stays
google-adk. The suffix is applied on the request-time header path because that is
what reaches the Interactions wire (the per-request extra_headers override the
genai client's construction-time headers; verified by live capture). An explicit
framework_label takes precedence over the Agent Engine (+remote_reasoning_engine)
suffix; all other callers of merge_tracking_headers keep the no-arg default and
are unchanged. Follow-up to the ManagedAgent tracking-headers change.

Co-authored-by: Haran Rajkumar <haranrk@google.com>
PiperOrigin-RevId: 947239702
2026-07-13 14:19:30 -07:00

135 lines
4.1 KiB
Python

# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
from google.adk import version
from google.adk.utils import _google_client_headers
import pytest
_EXPECTED_BASE_HEADER = (
f"google-adk/{version.__version__} gl-python/{sys.version.split()[0]}"
)
def test_get_tracking_headers():
"""Test get_tracking_headers returns correct headers."""
headers = _google_client_headers.get_tracking_headers()
assert headers == {
"x-goog-api-client": _EXPECTED_BASE_HEADER,
"user-agent": _EXPECTED_BASE_HEADER,
}
@pytest.mark.parametrize(
"input_headers, expected_headers",
[
(
None,
{
"x-goog-api-client": _EXPECTED_BASE_HEADER,
"user-agent": _EXPECTED_BASE_HEADER,
},
),
(
{},
{
"x-goog-api-client": _EXPECTED_BASE_HEADER,
"user-agent": _EXPECTED_BASE_HEADER,
},
),
(
{"x-goog-api-client": "label3 label4"},
{
"x-goog-api-client": f"{_EXPECTED_BASE_HEADER} label3 label4",
"user-agent": _EXPECTED_BASE_HEADER,
},
),
(
{"x-goog-api-client": f"gl-python/{sys.version.split()[0]} label3"},
{
"x-goog-api-client": f"{_EXPECTED_BASE_HEADER} label3",
"user-agent": _EXPECTED_BASE_HEADER,
},
),
(
{"other-header": "value"},
{
"x-goog-api-client": _EXPECTED_BASE_HEADER,
"user-agent": _EXPECTED_BASE_HEADER,
"other-header": "value",
},
),
],
)
def test_merge_tracking_headers(input_headers, expected_headers):
"""Test merge_tracking_headers with various inputs."""
headers = _google_client_headers.merge_tracking_headers(input_headers)
assert headers == expected_headers
def test_get_tracking_http_options():
"""get_tracking_http_options returns HttpOptions carrying tracking headers."""
http_options = _google_client_headers.get_tracking_http_options()
assert http_options.headers == {
"x-goog-api-client": _EXPECTED_BASE_HEADER,
"user-agent": _EXPECTED_BASE_HEADER,
}
def test_get_tracking_headers_with_framework_label():
"""framework_label flows into both tracking header values."""
expected = (
f"google-adk/{version.__version__}+managed_agent"
f" gl-python/{sys.version.split()[0]}"
)
headers = _google_client_headers.get_tracking_headers(
framework_label="managed_agent"
)
assert headers == {
"x-goog-api-client": expected,
"user-agent": expected,
}
def test_merge_tracking_headers_with_framework_label():
"""framework_label flows into the merged tracking header values."""
expected = (
f"google-adk/{version.__version__}+managed_agent"
f" gl-python/{sys.version.split()[0]}"
)
headers = _google_client_headers.merge_tracking_headers(
None, framework_label="managed_agent"
)
assert headers == {
"x-goog-api-client": expected,
"user-agent": expected,
}
def test_merge_tracking_headers_with_framework_label_preserves_custom_headers():
"""The suffix is applied while unrelated custom headers pass through."""
expected = (
f"google-adk/{version.__version__}+managed_agent"
f" gl-python/{sys.version.split()[0]}"
)
headers = _google_client_headers.merge_tracking_headers(
{"x-custom": "v"}, framework_label="managed_agent"
)
assert headers == {
"x-goog-api-client": expected,
"user-agent": expected,
"x-custom": "v",
}