Skip to content

Commit 9f73c64

Browse files
committed
Fix 'make test' command and lint issues.
1 parent 06ab153 commit 9f73c64

File tree

11 files changed

+51
-47
lines changed

11 files changed

+51
-47
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def _guess_genai_system_from_env():
8787
"true",
8888
"1",
8989
]:
90-
return _get_vertexai_system_name()
90+
return _get_vertexai_system_name()
9191
return _get_gemini_system_name()
9292

9393

@@ -230,8 +230,8 @@ def _maybe_update_error_type(self, response: GenerateContentResponse):
230230
(response.prompt_feedback.block_reason == BlockedReason.BLOCKED_REASON_UNSPECIFIED)):
231231
self._error_type = "NO_CANDIDATES"
232232
return
233-
block_reason = response.prompt_feedback.block_reason
234-
self._error_type = "BLOCKED_{}".format(block_reason.name)
233+
block_reason = response.prompt_feedback.block_reason.name.upper()
234+
self._error_type = f'BLOCKED_{block_reason}'
235235

236236
def _maybe_log_system_instruction(self, config: Optional[GenerateContentConfigOrDict]=None):
237237
if not self._content_recording_enabled:
@@ -324,8 +324,8 @@ def instrumented_generate_content(
324324
response = wrapped_func(self, model=model, contents=contents, config=config)
325325
helper.process_response(response)
326326
return response
327-
except Exception as e:
328-
helper.process_error(e)
327+
except Exception as error:
328+
helper.process_error(error)
329329
raise
330330
finally:
331331
helper.finalize_processing()
@@ -353,8 +353,8 @@ def instrumented_generate_content_stream(
353353
for response in wrapped_func(self, model=model, contents=contents, config=config):
354354
helper.process_response(response)
355355
yield response
356-
except Exception as e:
357-
helper.process_error(e)
356+
except Exception as error:
357+
helper.process_error(error)
358358
raise
359359
finally:
360360
helper.finalize_processing()
@@ -382,8 +382,8 @@ async def instrumented_generate_content(
382382
response = await wrapped_func(self, model=model, contents=contents, config=config)
383383
helper.process_response(response)
384384
return response
385-
except Exception as e:
386-
helper.process_error(e)
385+
except Exception as error:
386+
helper.process_error(error)
387387
raise
388388
finally:
389389
helper.finalize_processing()
@@ -411,8 +411,8 @@ async def instrumented_generate_content_stream(
411411
async for response in wrapped_func(self, model=model, contents=contents, config=config):
412412
helper.process_response(response)
413413
yield response
414-
except Exception as e:
415-
helper.process_error(e)
414+
except Exception as error:
415+
helper.process_error(error)
416416
raise
417417
finally:
418418
helper.finalize_processing()

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class GoogleGenAiSdkInstrumentor(BaseInstrumentor):
3131
def __init__(self):
3232
self._generate_content_snapshot = None
3333

34-
def instrumentation_dependencies(self) -> Collection[str]:
34+
# Inherited, abstract function from 'BaseInstrumentor'. Even though 'self' is
35+
# not used in the definition, a method is required per the API contract.
36+
def instrumentation_dependencies(self) -> Collection[str]: # pylint: disable=no-self-use
3537
return ["google-genai>=1.0.0,<2"]
3638

3739
def _instrument(self, **kwargs: Any):
@@ -47,4 +49,3 @@ def _instrument(self, **kwargs: Any):
4749

4850
def _uninstrument(self, **kwargs: Any):
4951
uninstrument_generate_content(self._generate_content_snapshot)
50-

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,3 @@ def log_response_content(self, attributes, body):
102102
def _log_event(self, event_name, attributes, body):
103103
event = Event(event_name, body=body, attributes=attributes)
104104
self.event_logger.emit(event)
105-

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/common/instrumentation_context.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
sys.path.append("../../src")
1818

19-
from opentelemetry.instrumentation.google_genai import (
19+
# This import has to happen after 'sys.path.append' above, so that it is possible
20+
# to use this name relative to "../../src" (at least when this module is imported
21+
# from a test script that has been invoked directly).
22+
from opentelemetry.instrumentation.google_genai import ( # pylint: disable=wrong-import-position
2023
GoogleGenAiSdkInstrumentor,
2124
)
2225

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/common/otel_mocker.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def get_span_named(self, name):
169169
def assert_has_span_named(self, name):
170170
span = self.get_span_named(name)
171171
finished_spans = self.get_finished_spans()
172-
assert span is not None, 'Could not find span named "{}"; finished spans: {}'.format(name, finished_spans)
172+
assert span is not None, f'Could not find span named "{name}"; finished spans: {finished_spans}'
173173

174174
def get_event_named(self, event_name):
175175
for event in self.get_finished_logs():
@@ -183,11 +183,11 @@ def get_event_named(self, event_name):
183183
def assert_has_event_named(self, name):
184184
event = self.get_event_named(name)
185185
finished_logs = self.get_finished_logs()
186-
assert event is not None, 'Could not find event named "{}"; finished logs: {}'.format(name, finished_logs)
186+
assert event is not None, f'Could not find event named "{name}"; finished logs: {finished_logs}'
187187

188188
def assert_does_not_have_event_named(self, name):
189189
event = self.get_event_named(name)
190-
assert event is None, "Unexpected event: {}".format(event)
190+
assert event is None, f'Unexpected event: {event}'
191191

192192
def get_metrics_data_named(self, name):
193193
results = []
@@ -215,4 +215,4 @@ def _install_traces(self):
215215
provider = TracerProvider()
216216
provider.add_span_processor(SimpleSpanProcessor(self._traces))
217217
set_tracer_provider(provider)
218-
218+

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/common/requests_mocker.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import http.client
1516
import copy
1617
import functools
1718
import io
@@ -61,11 +62,11 @@ def response(self):
6162

6263

6364

64-
def _return_error_status(args: RequestsCallArgs, status_code: int, reason: str):
65+
def _return_error_status(args: RequestsCallArgs, status_code: int, reason: str=None):
6566
result = requests.Response()
6667
result.url = args.request.url
6768
result.status_code = status_code
68-
result.reason = reason
69+
result.reason = reason or http.client.responses.get(status_code)
6970
result.request = args.request
7071
return result
7172

@@ -80,37 +81,39 @@ def _to_response_generator(response):
8081
if isinstance(response, int):
8182
return lambda args: _return_error_status(args, response)
8283
if isinstance(response, requests.Response):
83-
def response_generator(args):
84+
def generate_response_from_response(args):
8485
new_response = copy.deepcopy(response)
8586
new_response.request = args.request
8687
new_response.url = args.request.url
8788
return new_response
88-
return response_generator
89+
return generate_response_from_response
8990
if isinstance(response, dict):
90-
def response_generator(args):
91+
def generate_response_from_dict(args):
9192
result = requests.Response()
9293
result.status_code = 200
9394
result.headers["content-type"] = "application/json"
9495
result.encoding = "utf-8"
9596
result.raw = io.BytesIO(json.dumps(response).encode())
9697
return result
97-
return response_generator
98+
return generate_response_from_dict
9899
raise ValueError(f"Unsupported response type: {type(response)}")
99100

100101

101102
class RequestsMocker:
102103

103-
def install(self):
104+
def __init__(self):
104105
self._original_send = requests.sessions.Session.send
106+
self._calls = []
107+
self._handlers = []
108+
109+
def install(self):
105110
@functools.wraps(requests.sessions.Session.send)
106111
def replacement_send(
107112
s: requests.sessions.Session,
108113
request: requests.PreparedRequest,
109114
**kwargs):
110115
return self._do_send(s, request, **kwargs)
111116
requests.sessions.Session.send = replacement_send
112-
self._calls = []
113-
self._handlers = []
114117

115118
def uninstall(self):
116119
requests.sessions.Session.send = self._original_send
@@ -145,4 +148,3 @@ def _lookup_response_generator(self, args: RequestsCallArgs):
145148
if matcher(args):
146149
return response_generator
147150
return _return_404
148-

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
sys.path.append("../")
2323

24-
from common.base import TestCase
24+
# This needs to go after 'sys.path.append' in order to ensure that 'common'
25+
# can be imported using this naming (when the script is invoked directly).
26+
from common.base import TestCase # pylint: disable=wrong-import-position
2527

2628

2729
def create_valid_response(response_text="The model response", input_tokens=10, output_tokens=20):
@@ -44,9 +46,6 @@ def create_valid_response(response_text="The model response", input_tokens=10, o
4446

4547
class TestGenerateContentAsyncNonstreaming(TestCase):
4648

47-
def setUp(self):
48-
super().setUp()
49-
5049
def configure_valid_response(self, response_text="The model_response", input_tokens=10, output_tokens=20):
5150
self.requests.add_response(create_valid_response(
5251
response_text=response_text,

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
sys.path.append("../")
2323

24-
from common.base import TestCase
24+
# This needs to go after 'sys.path.append' in order to ensure that 'common'
25+
# can be imported using this naming (when the script is invoked directly).
26+
from common.base import TestCase # pylint: disable=wrong-import-position
2527

2628

2729
def create_valid_response(response_text="The model response", input_tokens=10, output_tokens=20):
@@ -44,9 +46,6 @@ def create_valid_response(response_text="The model response", input_tokens=10, o
4446

4547
class TestGenerateContentAsyncStreaming(TestCase):
4648

47-
def setUp(self):
48-
super().setUp()
49-
5049
def configure_valid_response(self, response_text="The model_response", input_tokens=10, output_tokens=20):
5150
self.requests.add_response(create_valid_response(
5251
response_text=response_text,

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
sys.path.append("../")
2424

25-
from common.base import TestCase
25+
# This needs to go after 'sys.path.append' in order to ensure that 'common'
26+
# can be imported using this naming (when the script is invoked directly).
27+
from common.base import TestCase # pylint: disable=wrong-import-position
2628

2729

2830
def create_valid_response(response_text="The model response", input_tokens=10, output_tokens=20):
@@ -46,9 +48,6 @@ def create_valid_response(response_text="The model response", input_tokens=10, o
4648

4749
class TestGenerateContentSyncNonstreaming(TestCase):
4850

49-
def setUp(self):
50-
super().setUp()
51-
5251
def configure_valid_response(self, response_text="The model_response", input_tokens=10, output_tokens=20):
5352
self.requests.add_response(create_valid_response(
5453
response_text=response_text,

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
sys.path.append("../")
2222

23-
from common.base import TestCase
23+
# This needs to go after 'sys.path.append' in order to ensure that 'common'
24+
# can be imported using this naming (when the script is invoked directly).
25+
from common.base import TestCase # pylint: disable=wrong-import-position
2426

2527

2628
def create_valid_response(response_text="The model response", input_tokens=10, output_tokens=20):
@@ -43,9 +45,6 @@ def create_valid_response(response_text="The model response", input_tokens=10, o
4345

4446
class TestGenerateContentSyncStreaming(TestCase):
4547

46-
def setUp(self):
47-
super().setUp()
48-
4948
def configure_valid_response(self, response_text="The model_response", input_tokens=10, output_tokens=20):
5049
self.requests.add_response(create_valid_response(
5150
response_text=response_text,

0 commit comments

Comments
 (0)