Skip to content

[opentelemetry-instrumentation-google-genai] Use wrapt to monkey patch instead of functools.wraps. Clarify readme tool calling section.#151

Open
DylanRussell wants to merge 5 commits into
open-telemetry:mainfrom
DylanRussell:openinference
Open

[opentelemetry-instrumentation-google-genai] Use wrapt to monkey patch instead of functools.wraps. Clarify readme tool calling section.#151
DylanRussell wants to merge 5 commits into
open-telemetry:mainfrom
DylanRussell:openinference

Conversation

@DylanRussell

Copy link
Copy Markdown
Contributor

Description

Use wrapt to monkey patch (https://pypi.org/project/wrapt/) instead of functools.wraps.. I think this is a good best practice, I see openinference's instrumentation uses this, and a bunch of instrumentation in contrib..

I also updated the README to correctly describe when we generate an execute_tool span (we do it when automatic function calling is enabled only..)..

Fixes # (issue)

  • [ x] Bug fix (non-breaking change which fixes an issue)

How has this been tested?

Unit tests

Checklist

See CONTRIBUTING.md
for the style guide, changelog guidance, and more.

  • [x ] Followed the style guidelines of this project
  • [ x] Changelog updated if the change requires an entry
  • [x ] Unit tests added
  • [ x] Documentation updated

Copilot AI review requested due to automatic review settings June 18, 2026 15:07
@DylanRussell DylanRussell requested a review from a team as a code owner June 18, 2026 15:07

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.

Pull request overview

This PR updates the Google GenAI instrumentation to use wrapt’s wrap_function_wrapper for monkey-patching SDK methods (instead of functools.wraps + direct assignment), and adjusts the README to clarify how tool-call spans are emitted when automatic function calling is enabled.

Changes:

  • Switch Models/AsyncModels patching to wrapt.wrap_function_wrapper wrappers.
  • Clarify README limitation text around execute_tool spans and when tool calls are traced.
  • Add wrapt as a package dependency and record the change via a towncrier fragment (and remove the corresponding TODO).

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
instrumentation/opentelemetry-instrumentation-google-genai/TODOS.md Removes the “adopt wrapt” TODO now that it’s implemented.
instrumentation/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/generate_content.py Reworks patching/wrappers to use wrap_function_wrapper and adjusts wrapper signatures/flow.
instrumentation/opentelemetry-instrumentation-google-genai/README.rst Updates limitation text describing tool-call span behavior.
instrumentation/opentelemetry-instrumentation-google-genai/pyproject.toml Adds wrapt dependency.
instrumentation/opentelemetry-instrumentation-google-genai/.changelog/151.changed Adds a changelog fragment for the wrapt monkey-patching change.
Comments suppressed due to low confidence (1)

instrumentation/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/generate_content.py:683

  • In the async streaming wrapper, extra attributes from context are applied before _apply_request_attributes(...), which is inconsistent with the sync wrappers. Applying extras last is safer because it lets caller-provided extra attrs override defaults in case of key collisions.
            invocation.attributes.update(
                _get_extra_generate_content_attributes()
            )
            _apply_request_attributes(
                wrapped_config,

Comment thread instrumentation/opentelemetry-instrumentation-google-genai/README.rst Outdated
Comment thread instrumentation/opentelemetry-instrumentation-google-genai/pyproject.toml Outdated
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.

3 participants