[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
Conversation
Contributor
There was a problem hiding this comment.
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/AsyncModelspatching towrapt.wrap_function_wrapperwrappers. - Clarify README limitation text around
execute_toolspans and when tool calls are traced. - Add
wraptas 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,
lmolkova
approved these changes
Jun 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Use
wraptto monkey patch (https://pypi.org/project/wrapt/) instead offunctools.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_toolspan (we do it when automatic function calling is enabled only..)..Fixes # (issue)
How has this been tested?
Unit tests
Checklist
See CONTRIBUTING.md
for the style guide, changelog guidance, and more.