Skip to content

Commit e43e8c9

Browse files
[chore] Address TODO to migrate to VCRpy and remove bespoke RequestMocker code in Google GenAI SDK instrumentation (#3344)
* Remove bespoke request mocker. Replace with direct mocking of the underlying API. * Refactor fake credentials to enable reuse. * Add module to test end to end with a real client. * Add redaction and minimal OTel mocking/testing in the e2e test. * Fix wording of the documentation. * Remove vcr migration from TODOs. * Improve redaction and test naming. * Minor tweaks in the code generation. Add casette files. * Reformat with ruff. * Fix lint and gzip issue. * Reformat with ruff. * Prevent fix for Python 3.9 from breaking tests in other versions. * Record update in changelog. * Don't double iterate when redacting by changing the value. Co-authored-by: Aaron Abbott <[email protected]> --------- Co-authored-by: Aaron Abbott <[email protected]>
1 parent ad29af3 commit e43e8c9

18 files changed

+1547
-298
lines changed

Diff for: instrumentation-genai/opentelemetry-instrumentation-google-genai/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Restructure tests to keep in line with repository conventions ([#3344](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3344))
11+
1012
## Version 0.1b0 (2025-03-05)
1113

1214
- Add support for async and streaming.

Diff for: instrumentation-genai/opentelemetry-instrumentation-google-genai/TODOS.md

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Here are some TODO items required to achieve stability for this package:
1313
- Additional cleanup/improvement tasks such as:
1414
- Adoption of 'wrapt' instead of 'functools.wraps'
1515
- Bolstering test coverage
16-
- Migrate tests to use VCR.py
1716

1817
## Future
1918

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import google.auth.credentials
16+
17+
18+
class FakeCredentials(google.auth.credentials.AnonymousCredentials):
19+
def refresh(self, request):
20+
pass

Diff for: instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/common/base.py

+12-15
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,22 @@
1717

1818
import google.genai
1919

20+
from .auth import FakeCredentials
2021
from .instrumentation_context import InstrumentationContext
2122
from .otel_mocker import OTelMocker
22-
from .requests_mocker import RequestsMocker
23-
24-
25-
class _FakeCredentials(google.auth.credentials.AnonymousCredentials):
26-
def refresh(self, request):
27-
pass
2823

2924

3025
class TestCase(unittest.TestCase):
3126
def setUp(self):
3227
self._otel = OTelMocker()
3328
self._otel.install()
34-
self._requests = RequestsMocker()
35-
self._requests.install()
3629
self._instrumentation_context = None
3730
self._api_key = "test-api-key"
3831
self._project = "test-project"
3932
self._location = "test-location"
4033
self._client = None
4134
self._uses_vertex = False
42-
self._credentials = _FakeCredentials()
35+
self._credentials = FakeCredentials()
4336

4437
def _lazy_init(self):
4538
self._instrumentation_context = InstrumentationContext()
@@ -51,17 +44,22 @@ def client(self):
5144
self._client = self._create_client()
5245
return self._client
5346

54-
@property
55-
def requests(self):
56-
return self._requests
57-
5847
@property
5948
def otel(self):
6049
return self._otel
6150

6251
def set_use_vertex(self, use_vertex):
6352
self._uses_vertex = use_vertex
6453

54+
def reset_client(self):
55+
self._client = None
56+
57+
def reset_instrumentation(self):
58+
if self._instrumentation_context is None:
59+
return
60+
self._instrumentation_context.uninstall()
61+
self._instrumentation_context = None
62+
6563
def _create_client(self):
6664
self._lazy_init()
6765
if self._uses_vertex:
@@ -72,10 +70,9 @@ def _create_client(self):
7270
location=self._location,
7371
credentials=self._credentials,
7472
)
75-
return google.genai.Client(api_key=self._api_key)
73+
return google.genai.Client(vertexai=False, api_key=self._api_key)
7674

7775
def tearDown(self):
7876
if self._instrumentation_context is not None:
7977
self._instrumentation_context.uninstall()
80-
self._requests.uninstall()
8178
self._otel.uninstall()

Diff for: instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/common/requests_mocker.py

-238
This file was deleted.

0 commit comments

Comments
 (0)