Skip to content

fix(telemetry): add ADK_STORE_USER_ID to preserve user.id on traces - #8450

Open
josuetrindade wants to merge 1 commit into
google:mainfrom
josuetrindade:fix/telemetry-store-user-id
Open

josuetrindade wants to merge 1 commit into
google:mainfrom
josuetrindade:fix/telemetry-store-user-id

Conversation

@josuetrindade

Copy link
Copy Markdown

Link to Issue or Description of Change

Problem:
When opentelemetry-instrumentation-google-genai is installed and active, ADK delegates Gemini inference spans and places user.id only in log_only_extra_attributes. _use_extra_generate_content_attributes() attempts to read GENERATE_CONTENT_EVENT_ONLY_EXTRA_ATTRIBUTES_CONTEXT_KEY from opentelemetry.instrumentation.google_genai, catches (ImportError, AttributeError), and silently ignores it. Because released versions of opentelemetry-instrumentation-google-genai (including 0.7b2 and 1.0b0) do not export that key, user.id is silently dropped from instrumented spans and events.

Solution:
Add opt-in ADK_STORE_USER_ID (NO_CONTENT, EVENT_ONLY, SPAN_ONLY, SPAN_AND_EVENT) on TelemetryConfig:

  • When ADK_STORE_USER_ID is unset, default behavior is preserved: user.id is not placed on spans, and native telemetry still follows OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT for log records.
  • When ADK_STORE_USER_ID is SPAN_ONLY or SPAN_AND_EVENT, user.id is included on native inference spans and forwarded in GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY for opentelemetry-instrumentation-google-genai.
  • When ADK_STORE_USER_ID is EVENT_ONLY or NO_CONTENT, user.id is never widened onto spans; NO_CONTENT suppresses it from both spans and log events, while EVENT_ONLY enables it on native log events regardless of message-content capture mode.
  • When ADK_STORE_USER_ID has an unrecognized value, a warning is logged once and it falls back to unset.
  • When log_only_extra_attributes is dropped because GENERATE_CONTENT_EVENT_ONLY_EXTRA_ATTRIBUTES_CONTEXT_KEY is unavailable, a warning is logged once instead of failing silently.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added unit and integration tests covering ADK_STORE_USER_ID parsing and warn-once validation (tests/unittests/telemetry/test_telemetry_context.py), use_inference_span native and delegated routing plus one-time missing-key warning (tests/unittests/telemetry/test_spans.py), and end-to-end GoogleGenAiSdkInstrumentor span attribute propagation (tests/unittests/telemetry/test_genai_instrumentor_bypass.py).

Local checks run on Windows (Python 3.11.0):

  • .venv\Scripts\pytest.exe tests/unittests/telemetry/ -q: 1245 passed, 1 skipped, 2 xfailed (the 1 skipped test is pre-existing in test_genai_instrumentor_bypass.py when opentelemetry-util-genai < 1.4b0).
  • .venv\Scripts\pytest.exe tests/unittests/test_import_loading.py -q: 30 passed.
  • .venv\Scripts\ruff.exe check, isort --check --diff, pyink --check --diff, codespell, and scripts/compliance_checks.py on all 6 modified files: passed.
  • .venv\Scripts\mypy.exe src/google/adk/telemetry/context.py src/google/adk/telemetry/_experimental_semconv.py src/google/adk/telemetry/tracing.py: 0 new errors (1 pre-existing no-any-return at tracing.py:1676 in untouched _response_model).

Not run locally:

  • Full non-telemetry tests/unittests/ suite, tox multi-version matrix, addlicense, and live Vertex AI / Cloud Trace calls (GoogleGenAiSdkInstrumentor was exercised offline with AsyncModels.generate_content stubbed).

Manual End-to-End (E2E) Tests:

Verified offline with GoogleGenAiSdkInstrumentor, TracerProvider, InMemorySpanExporter, and stubbed AsyncModels.generate_content: with ADK_TELEMETRY_SCHEMA_VERSION_OPT_IN=2 and OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental, generate_content spans omit user.id by default / under EVENT_ONLY / under NO_CONTENT, and record user.id="test_user" under ADK_STORE_USER_ID=SPAN_ONLY and SPAN_AND_EVENT.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

When opentelemetry-instrumentation-google-genai is active, ADK delegates
inference spans and passes user.id via log_only_extra_attributes, which
tries GENERATE_CONTENT_EVENT_ONLY_EXTRA_ATTRIBUTES_CONTEXT_KEY. Because
released versions of that instrumentation package do not export that key,
user.id is silently dropped from both spans and events.

Add ADK_STORE_USER_ID (NO_CONTENT, EVENT_ONLY, SPAN_ONLY, SPAN_AND_EVENT)
on TelemetryConfig so operators can explicitly opt in to propagating
user.id onto inference spans (including
GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY for delegated Gemini
calls) or log events, while preserving the default privacy boundary when
unset and logging a one-time warning when the event-only instrumentation
key is unavailable.

Fixes google#6361
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

user.id silently dropped when GENERATE_CONTENT_EVENT_ONLY_EXTRA_ATTRIBUTES_CONTEXT_KEY is missing

1 participant