Skip to content

Commit dc5dcc7

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: disable request/response content in custom ADK spans on Agent Engine in case there's no explicit consent
PiperOrigin-RevId: 820829272
1 parent d13b230 commit dc5dcc7

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

tests/unit/vertex_adk/test_agent_engine_templates_adk.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,26 @@ def test_enable_tracing_warning(self, caplog):
457457
# app.set_up()
458458
# assert "enable_tracing=True but proceeding with tracing disabled" in caplog.text
459459

460+
@mock.patch.dict(os.environ)
461+
def test_span_content_capture_disabled_by_default(self):
462+
app = agent_engines.AdkApp(agent=_TEST_AGENT)
463+
app.set_up()
464+
assert os.environ["ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS"] == "false"
465+
466+
@mock.patch.dict(
467+
os.environ, {"OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT": "true"}
468+
)
469+
def test_span_content_capture_enabled_with_env_var(self):
470+
app = agent_engines.AdkApp(agent=_TEST_AGENT)
471+
app.set_up()
472+
assert os.environ["ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS"] == "true"
473+
474+
@mock.patch.dict(os.environ)
475+
def test_span_content_capture_enabled_with_tracing(self):
476+
app = agent_engines.AdkApp(agent=_TEST_AGENT, enable_tracing=True)
477+
app.set_up()
478+
assert os.environ["ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS"] == "true"
479+
460480

461481
def test_dump_event_for_json():
462482
from google.adk.events import event

vertexai/agent_engines/templates/adk.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,19 @@ def set_up(self):
575575
os.environ["GOOGLE_CLOUD_PROJECT"] = project
576576
location = self._tmpl_attrs.get("location")
577577
os.environ["GOOGLE_CLOUD_LOCATION"] = location
578+
579+
content_enabled = os.getenv(
580+
"OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT", "false"
581+
).lower() in ("true", "1")
582+
# Disable content capture in custom ADK spans unless:
583+
# 1. User opted-in for content capture.
584+
# 2. Or user enabled tracing explicitly with the old flag (this is to
585+
# preserve compatibility with old behavior).
586+
if self._tmpl_attrs.get("enable_tracing") or content_enabled:
587+
os.environ["ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS"] = "true"
588+
else:
589+
os.environ["ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS"] = "false"
590+
578591
if self._tmpl_attrs.get("enable_tracing"):
579592
instrumentor_builder = (
580593
self._tmpl_attrs.get("instrumentor_builder")

0 commit comments

Comments
 (0)