Skip to content

Commit e1c56f2

Browse files
Rima-agaabmass
andauthored
[google-genai] Test instrumentation on google-genai v1.64.0 (#4253)
* [google-genai] Test instrumentation on google-genai v1.63.0 * Add package version upper bound * bump latest version for python 3.9 * fix README * bump google-auth version for python 3.9 * add package upper limit to pyproject.toml * [google-genai] Test instrumentation on google-genai v1.64.0 * fix upper case method in cassette not matching lower case one generated by aiohttp * fix async calls hanging in vcrpy * Remove package upper limit * fix import error in python 3.9 * fix google-genai module lower bound * Add link to vcr issue * Add comment about request method * Fix lint errors * Fix spellcheck --------- Co-authored-by: Aaron Abbott <[email protected]>
1 parent 8fec6b6 commit e1c56f2

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

instrumentation-genai/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
| --------------- | ------------------ | --------------- | -------------- |
44
| [opentelemetry-instrumentation-anthropic](./opentelemetry-instrumentation-anthropic) | anthropic >= 0.16.0 | No | development
55
| [opentelemetry-instrumentation-claude-agent-sdk](./opentelemetry-instrumentation-claude-agent-sdk) | claude-agent-sdk >= 0.1.14 | No | development
6-
| [opentelemetry-instrumentation-google-genai](./opentelemetry-instrumentation-google-genai) | google-genai >= 1.0.0 | No | development
6+
| [opentelemetry-instrumentation-google-genai](./opentelemetry-instrumentation-google-genai) | google-genai >= 1.32.0 | No | development
77
| [opentelemetry-instrumentation-langchain](./opentelemetry-instrumentation-langchain) | langchain >= 0.3.21 | No | development
88
| [opentelemetry-instrumentation-openai-agents-v2](./opentelemetry-instrumentation-openai-agents-v2) | openai-agents >= 0.3.3 | No | development
99
| [opentelemetry-instrumentation-openai-v2](./opentelemetry-instrumentation-openai-v2) | openai >= 1.26.0 | Yes | development

instrumentation-genai/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
_instruments = ("google-genai >= 1.0.0",)
15+
_instruments = ("google-genai >= 1.32.0",)

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/generate_content/test_e2e.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
from google.genai import types
4141
from vcr.record_mode import RecordMode
4242

43+
try:
44+
# These modules are only supported in python >= 3.10
45+
from aiohttp.client_exceptions import ClientConnectionError
46+
from vcr.stubs import aiohttp_stubs
47+
except ImportError:
48+
ClientConnectionError = None
49+
aiohttp_stubs = None
50+
4351
from opentelemetry.instrumentation._semconv import (
4452
OTEL_SEMCONV_STABILITY_OPT_IN,
4553
_OpenTelemetrySemanticConventionStability,
@@ -135,6 +143,9 @@ def _redact_headers(headers):
135143

136144

137145
def _before_record_request(request):
146+
# aiohttp reports the request method in lower case while it is recorded in the cassette in upper case.
147+
if request.method:
148+
request.method = request.method.upper()
138149
if request.headers:
139150
_redact_headers(request.headers)
140151
uri = request.uri
@@ -316,6 +327,48 @@ def setup_vcr(vcr):
316327
return vcr
317328

318329

330+
@pytest.fixture(name="patch_vcr_aiohttp_stream", scope="module", autouse=True)
331+
def fixture_patch_vcr_aiohttp_stream():
332+
# Allows the async tests to not be stuck in infinite loop when streaming
333+
# a VCR cassette with aiohttp stubs.
334+
# https://github.com/kevin1024/vcrpy/issues/927
335+
if ClientConnectionError is None or aiohttp_stubs is None:
336+
return
337+
338+
class _ReplayMockStream(aiohttp_stubs.MockStream):
339+
# Keep vcrpy's stream behavior, but ignore aiohttp's
340+
# close-time ClientConnectionError("Connection closed") during
341+
# cassette replay, where the full response is already buffered
342+
# and this condition should be treated as normal EOF.
343+
def set_exception(self, exc):
344+
if isinstance(exc, ClientConnectionError) and exc.args == (
345+
"Connection closed",
346+
):
347+
return
348+
super().set_exception(exc)
349+
350+
class _ReplayMockClientResponse(aiohttp_stubs.MockClientResponse):
351+
def __init__(self, *args, **kwargs):
352+
super().__init__(*args, **kwargs)
353+
self._mock_content_stream = None
354+
355+
@property
356+
def content(self):
357+
# vcrpy's aiohttp MockClientResponse.content creates a fresh stream object
358+
# on every property access. google-genai async streaming repeatedly reads
359+
# response.content.readline() and expects the same stream instance until EOF is
360+
# reached.
361+
if self._mock_content_stream is None:
362+
body = self._body or b""
363+
stream = _ReplayMockStream()
364+
stream.feed_data(body)
365+
stream.feed_eof()
366+
self._mock_content_stream = stream
367+
return self._mock_content_stream
368+
369+
aiohttp_stubs.MockClientResponse = _ReplayMockClientResponse
370+
371+
319372
@pytest.fixture(name="instrumentor")
320373
def fixture_instrumentor():
321374
return GoogleGenAiSdkInstrumentor()

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/requirements.latest.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ pytest==7.4.4
4040
pytest-asyncio==0.21.0
4141
pytest-vcr==1.0.2
4242

43-
google-auth==2.38.0
44-
google-genai==1.32.0
43+
google-auth==2.47.0
44+
45+
google-genai==1.47.0; python_version < "3.10"
46+
google-genai==1.64.0; python_version >= "3.10"
4547

4648
# Install locally from the folder. This path is relative to the
4749
# root directory, given invocation from "tox" at root level.

0 commit comments

Comments
 (0)