Skip to content

feat(telemetry): add user.id to gen_ai.user.message log records#5796

Closed
Achuth17 wants to merge 2 commits into
google:mainfrom
Achuth17:feat-telemetry-user-id-event-only
Closed

feat(telemetry): add user.id to gen_ai.user.message log records#5796
Achuth17 wants to merge 2 commits into
google:mainfrom
Achuth17:feat-telemetry-user-id-event-only

Conversation

@Achuth17
Copy link
Copy Markdown
Contributor

Please note that this change adds user.id field to an existing opt-in: OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT. user.id is no longer set as a span attribute; it is emitted only on log records.

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

  • Closes: #issue_number
  • Related: #issue_number

2. Or, if no issue exists, describe the change:

Problem:

  • user.id was being attached as a span attribute on generate_content spans, which conflicted with security/privacy guidance that prompt/response-related PII should not be put in spans.
  • user.id should only be emitted on event logs (gated by the existing OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT opt-in), not on spans.

Solution:
What: Stop setting user.id as a span attribute. Instead, emit it only on gen_ai.user.message log records (and on the experimental operation_details log), gated by the existing OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT opt-in modes that include event output (EVENT_ONLY, SPAN_AND_EVENT).

Why this solution:

  • Reuses an existing, well-known opt-in (OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT) rather than introducing a new one, keeps the user-facing surface small.
  • Keeps PII (user.id) out of spans entirely.
  • Threads a separate log_only_* parameter through the existing helpers so the span vs. log split is explicit at every call site, avoiding accidental leakage back into span attributes.
  • Gracefully degrades when opentelemetry-instrumentation-google-genai doesn't yet support the event-only context key (via try/except ImportError), so the change is non-blocking on the upstream contrib PR.

Testing Plan

Tests run:

  • tests/unittests/telemetry/test_spans.py35/35 passed. Includes new ported tests covering: user.id absent from span attributes, present on gen_ai.user.message logs only when OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT includes event output and user_id is non-null, genai-instrumentor branch doesn't receive user.id, and graceful degradation when the upstream opentelemetry-instrumentation-google-genai doesn't expose the event-only context key.
  • tests/unittests/telemetry/ (broader suite incl. e2e test_node_functional.py) — 103/103 passed.
  • tests/unittests/flows/ (regression on tracing.py callers) — 379/379 passed.

Unit Tests:

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

Manual End-to-End (E2E) Tests:

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.

Additional context

Add any other context or screenshots about the feature request here.

Please note that this change adds `user.id` field to an existing opt-in: `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT`. user.id is no longer set as a span attribute; it is emitted only on log records.
@adk-bot adk-bot added the tracing [Component] This issue is related to OpenTelemetry tracing label May 21, 2026
@Achuth17 Achuth17 marked this pull request as ready for review May 21, 2026 23:19
@xuanyang15 xuanyang15 self-assigned this May 21, 2026
copybara-service Bot pushed a commit that referenced this pull request May 22, 2026
Merge: #5796

ORIGINAL_AUTHOR=Achuth Narayan Rajagopal <achuth.narayan@gmail.com>
GitOrigin-RevId: ec8265e
Change-Id: Ib77c4e2ad3df29a7ca734f142b9dc42d0890f429
@xuanyang15
Copy link
Copy Markdown
Collaborator

Merged by eb379be

@xuanyang15 xuanyang15 closed this May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tracing [Component] This issue is related to OpenTelemetry tracing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants