Skip to content

[google-genai, util-genai] Decouple event emission and set event severity to DEBUG (#619) - #836

Merged
lmolkova merged 14 commits into
open-telemetry:mainfrom
somuai:fix-google-genai-emit-event-decoupled
Oct 29, 2026
Merged

lmolkova merged 14 commits into
open-telemetry:mainfrom
somuai:fix-google-genai-emit-event-decoupled

Conversation

@somuai

@somuai somuai commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Which problem is this PR solving?

Fixes #619

This PR implements the long-term decoupled GenAI event emission architecture requested by @DylanRussell:

  1. opentelemetry-util-genai:

    • Updates InferenceInvocation._maybe_create_event() to check if the event is disabled via self._logger.enabled(context=self._span_context, severity_number=SeverityNumber.DEBUG, event_name=event_name) (with graceful fallback for custom loggers whose enabled() does not accept severity_number). If disabled, log record creation and emission are skipped.
    • Sets the emitted LogRecord's severity_number to SeverityNumber.DEBUG per GenAI semantic conventions.
  2. opentelemetry-instrumentation-google-genai:

    • Removes unconditional os.environ["OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT"] = "true" in instrument_generate_content(), eliminating global process environment mutation and preserving explicit user configuration and OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT defaults.
  3. Tests:

    • Adds unit tests in opentelemetry-util-genai verifying event DEBUG severity, suppression when logger.enabled returns False, emission when logger.enabled returns True, and signature backwards compatibility.
    • Adds unit tests in opentelemetry-instrumentation-google-genai verifying that instrument() does not mutate or overwrite OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT.
    • All tests passing cleanly.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • pytest util/opentelemetry-util-genai/tests/test_utils_events.py (15/15 passed)
  • pytest instrumentation/opentelemetry-instrumentation-google-genai/tests/test_instrumentor.py (3/3 passed)
  • pytest instrumentation/opentelemetry-instrumentation-google-genai/tests/generate_content/ (124 passed)
  • pytest instrumentation/opentelemetry-instrumentation-google-genai/tests/interactions/ (344 passed)

Copilot AI balanced review requested due to automatic review settings October 2, 2026 15:03
@somuai
somuai requested a review from a team as a code owner October 2, 2026 15:03
somuai added a commit to somuai/opentelemetry-python-genai that referenced this pull request Oct 2, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The fallback can mishandle internal TypeErrors, and new tests depend on ambient environment state.

Review effort: Balanced
Findings: 2 Medium severity

Open (2)
What changed in this PR

Decouples GenAI event emission from Google GenAI instrumentation configuration and adds DEBUG severity/filtering support.

Changes:

  • Removes global event-enabling environment mutation.
  • Checks logger enablement before constructing events.
  • Adds severity, filtering, and configuration-preservation tests.
File Description
_inference_invocation.py Adds DEBUG severity and logger filtering.
test_utils_events.py Tests severity and logger enablement.
generate_content.py Removes environment mutation.
test_instrumentor.py Verifies environment preservation.
nonstreaming_base.py Explicitly enables an expected test event.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Merged · refreshed 2026-10-29 06:08 UTC

Status above doesn't look right?
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@somuai

somuai commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

Pushed commit 6f39257 with the remaining CI fixes:

  • Added hasattr guard on self._logger.enabled to safely handle loggers across test environments without breaking DEBUG event emission.
  • Synchronized uv.lock with pyproject.toml.
  • Formatted test suites via ruff 0.16.1.
  • Scoped explicit OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT: "true" to completion hook tests in test_e2e.py with clean environment restoration on teardown.

All local pre-commit checks and unit tests pass cleanly.

/dashboard route:reviewers

@somuai
somuai force-pushed the fix-google-genai-emit-event-decoupled branch from b0ef28b to 7430c80 Compare October 9, 2026 13:12
@somuai

somuai commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Rebased cleanly onto latest main (cda9b6fa) resolving all merge conflicts, and bumped opentelemetry-api and opentelemetry-sdk to ~= 1.45 across all packages in the workspace per maintainer review feedback. All unit tests and ruff linting pass green.

/dashboard route:reviewers

@somuai

somuai commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @DylanRussell and @lmolkova — pushed commit d1ab64c3 with the requested changes:

  1. Removed OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT: Removed the environment variable from opentelemetry-util-genai, opentelemetry-test-util-genai, and the instrumentation test suites (google-genai, anthropic, smolagents) in favor of OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT and logger configuration (self._logger.enabled(...)).
  2. Documentation & Changelog: Updated util/opentelemetry-util-genai/README.rst (referencing examples/suppress-inference-event) and added util/opentelemetry-util-genai/.changelog/836.removed.
  3. CI Dependency Alignment: Updated dev-requirements-conformance.txt and instrumentation-genai-openai test pins (v1.45.1 / 0.66b1) to match the ~= 1.45 workspace bump.

@somuai

somuai commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

/dashboard route:reviewers

@opentelemetry-pr-dashboard

Copy link
Copy Markdown

@somuai, this pull request was routed to reviewers. The handoff remains active across pushes until newer actionable human feedback arrives. Top-level feedback through this request will not return; unresolved review threads remain open.

@somuai
somuai force-pushed the fix-google-genai-emit-event-decoupled branch from 3f33f35 to f978023 Compare October 13, 2026 20:48
@somuai

somuai commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @DylanRussell and @lmolkova — addressed all review feedback in commit a7dd216f:

  1. Changelog Entry: Updated google-genai/.changelog/836.fixed to match @lmolkova's suggested description.
  2. Pytest Fixture: Refactored test_e2e.py::fixture_setup_instrumentation to use the monkeypatch fixture instead of manual os.environ copying/restoring.
  3. Distributed Changelogs: Updated util-genai/.changelog/836.removed and distributed the .removed changelog entry across all instrumentation packages as requested by @DylanRussell.

All 8 tests in test_e2e.py and workspace ruff checks pass cleanly.

@lmolkova lmolkova left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a couple more nits and we should be good to go

…e and bump qwenpaw api

Signed-off-by: Soumyajit Ghosh <[email protected]>
@somuai

somuai commented Oct 16, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @lmolkova — addressed both nits in commit b5fa2299:

  1. qwenpaw Dependency Bump: Updated instrumentation/opentelemetry-instrumentation-genai-qwenpaw/pyproject.toml to opentelemetry-api ~= 1.45 and re-synced uv.lock.
  2. Tool Definitions Assertion: Restored self.assertNotIn(GEN_AI_TOOL_DEFINITIONS, event.attributes) in test_log_event_no_content_capture within nonstreaming_base.py.

All 8 tests in test_e2e.py, 48 unit tests in generate_content, and ruff linting/formatting pass cleanly.

@somuai

somuai commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

/dashboard route:reviewers

@opentelemetry-pr-dashboard

Copy link
Copy Markdown

@somuai, your reviewer-routing request was recorded; this pull request has the approvals it needs and is now waiting on maintainers. The handoff remains active across pushes until newer actionable human feedback arrives. Top-level feedback through this request will not return; unresolved review threads remain open.

@lmolkova lmolkova left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a few cosmetic suggestions. Thanks!

@opentelemetry-pr-dashboard

This comment has been minimized.

# Conflicts:
#	tox.ini
#	util/opentelemetry-util-genai/src/opentelemetry/util/genai/_inference_invocation.py
#	uv.lock
@lmolkova
lmolkova enabled auto-merge October 29, 2026 05:42
@lmolkova
lmolkova added this pull request to the merge queue Oct 29, 2026
Merged via the queue into open-telemetry:main with commit 068606f Oct 29, 2026
84 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

google-genai: instrument_generate_content() unconditionally sets OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT=true, overriding config and user setting

5 participants