Skip to content

Commit a1abb6b

Browse files
committed
Reformat with ruff.
1 parent 29b1594 commit a1abb6b

File tree

7 files changed

+179
-114
lines changed

7 files changed

+179
-114
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@
3131
# Semantic Convention to be defined.
3232
# https://github.com/open-telemetry/semantic-conventions/issues/2185
3333
FUNCTION_TOOL_CALL_START_EVENT_NAME = "function_call.start"
34-
FUNCTION_TOOL_CALL_START_EVENT_ATTRS_POSITIONAL_ARGS_COUNT = "positional_argument_count"
35-
FUNCTION_TOOL_CALL_START_EVENT_ATTRS_KEYWORD_ARGS_COUNT = "keyword_argument_count"
34+
FUNCTION_TOOL_CALL_START_EVENT_ATTRS_POSITIONAL_ARGS_COUNT = (
35+
"positional_argument_count"
36+
)
37+
FUNCTION_TOOL_CALL_START_EVENT_ATTRS_KEYWORD_ARGS_COUNT = (
38+
"keyword_argument_count"
39+
)
3640
FUNCTION_TOOL_CALL_START_EVENT_BODY_POSITIONAL_ARGS = "positional_arguments"
3741
FUNCTION_TOOL_CALL_START_EVENT_BODY_KEYWORD_ARGS = "keyword_arguments"
3842

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,18 @@ def _get_response_property(response: GenerateContentResponse, path: str):
209209
return current_context
210210

211211

212-
def _coerce_config_to_object(config: GenerateContentConfigOrDict) -> GenerateContentConfig:
212+
def _coerce_config_to_object(
213+
config: GenerateContentConfigOrDict,
214+
) -> GenerateContentConfig:
213215
if isinstance(config, GenerateContentConfig):
214216
return config
215217
# Input must be a dictionary; convert by invoking the constructor.
216218
return GenerateContentConfig(**config)
217219

218220

219221
def _wrapped_config_with_tools(
220-
otel_wrapper: OTelWrapper,
221-
config: GenerateContentConfig):
222+
otel_wrapper: OTelWrapper, config: GenerateContentConfig
223+
):
222224
if not config.tools:
223225
return config
224226
result = copy.copy(config)
@@ -250,13 +252,13 @@ def __init__(
250252
)
251253

252254
def wrapped_config(
253-
self,
254-
config: Optional[GenerateContentConfigOrDict]) -> Optional[GenerateContentConfig]:
255+
self, config: Optional[GenerateContentConfigOrDict]
256+
) -> Optional[GenerateContentConfig]:
255257
if config is None:
256258
return None
257259
return _wrapped_config_with_tools(
258-
self._otel_wrapper,
259-
_coerce_config_to_object(config))
260+
self._otel_wrapper, _coerce_config_to_object(config)
261+
)
260262

261263
def start_span_as_current_span(
262264
self, model_name, function_name, end_on_exit=True

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
from opentelemetry.semconv.schemas import Schemas
2222

2323
from .custom_semconv import (
24-
FUNCTION_TOOL_CALL_START_EVENT_NAME,
2524
FUNCTION_TOOL_CALL_END_EVENT_NAME,
25+
FUNCTION_TOOL_CALL_START_EVENT_NAME,
2626
)
2727
from .version import __version__ as _LIBRARY_VERSION
2828

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

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

15-
from typing import Any, Callable, Optional, Union
16-
1715
import functools
1816
import inspect
17+
from typing import Any, Callable, Optional, Union
1918

19+
from google.genai.types import (
20+
ToolListUnion,
21+
ToolListUnionDict,
22+
ToolOrDict,
23+
)
2024

2125
from opentelemetry import trace
2226
from opentelemetry.semconv._incubating.attributes import (
2327
code_attributes,
2428
)
25-
from google.genai.types import (
26-
ToolOrDict,
27-
ToolListUnion,
28-
ToolListUnionDict,
29-
)
29+
3030
from .custom_semconv import (
3131
CODE_MODULE,
32-
FUNCTION_TOOL_CALL_START_EVENT_ATTRS_POSITIONAL_ARGS_COUNT,
32+
FUNCTION_TOOL_CALL_END_EVENT_BODY_RESULT,
3333
FUNCTION_TOOL_CALL_START_EVENT_ATTRS_KEYWORD_ARGS_COUNT,
34-
FUNCTION_TOOL_CALL_START_EVENT_BODY_POSITIONAL_ARGS,
34+
FUNCTION_TOOL_CALL_START_EVENT_ATTRS_POSITIONAL_ARGS_COUNT,
3535
FUNCTION_TOOL_CALL_START_EVENT_BODY_KEYWORD_ARGS,
36-
FUNCTION_TOOL_CALL_END_EVENT_BODY_RESULT,
37-
TOOL_CALL_POSITIONAL_ARG_COUNT,
36+
FUNCTION_TOOL_CALL_START_EVENT_BODY_POSITIONAL_ARGS,
3837
TOOL_CALL_KEYWORD_ARG_COUNT,
38+
TOOL_CALL_POSITIONAL_ARG_COUNT,
3939
)
4040
from .flags import is_content_recording_enabled
4141
from .otel_wrapper import OTelWrapper
4242

43-
4443
ToolFunction = Callable[..., Any]
4544

4645

@@ -50,8 +49,10 @@ def _to_otel_value(python_value):
5049
return None
5150
if isinstance(python_value, list):
5251
return [_to_otel_value(x) for x in python_value]
53-
if isinstance(python_value, dict):
54-
return dict([(key, _to_otel_value(val)) for (key, val) in python_value.items()])
52+
if isinstance(python_value, dict):
53+
return dict(
54+
[(key, _to_otel_value(val)) for (key, val) in python_value.items()]
55+
)
5556
if hasattr(python_value, "model_dump"):
5657
return python_value.model_dump()
5758
if hasattr(python_value, "__dict__"):
@@ -60,16 +61,14 @@ def _to_otel_value(python_value):
6061

6162

6263
def _create_function_span_name(wrapped_function):
63-
"""Constructs the span name for a given local function tool call."""
64+
"""Constructs the span name for a given local function tool call."""
6465
function_name = wrapped_function.__name__
6566
return f"tool_call {function_name}"
6667

6768

6869
def _create_function_span_attributes(
69-
wrapped_function,
70-
function_args,
71-
function_kwargs,
72-
extra_span_attributes):
70+
wrapped_function, function_args, function_kwargs, extra_span_attributes
71+
):
7372
"""Creates the attributes for a tool call function span."""
7473
result = {}
7574
if extra_span_attributes:
@@ -82,10 +81,8 @@ def _create_function_span_attributes(
8281

8382

8483
def _record_function_call_span_attributes(
85-
otel_wrapper,
86-
wrapped_function,
87-
function_args,
88-
function_kwargs):
84+
otel_wrapper, wrapped_function, function_args, function_kwargs
85+
):
8986
"""Records the details about a function invocation as span attributes."""
9087
if not is_content_recording_enabled():
9188
return
@@ -104,49 +101,56 @@ def _record_function_call_span_attributes(
104101

105102

106103
def _record_function_call_event(
107-
otel_wrapper,
108-
wrapped_function,
109-
function_args,
110-
function_kwargs):
104+
otel_wrapper, wrapped_function, function_args, function_kwargs
105+
):
111106
"""Records the details about a function invocation as a log event."""
112107
attributes = {
113-
code_attributes.CODE_FUNCTION_NAME: wrapped_function.__name__,
114-
CODE_MODULE: wrapped_function.__module__,
115-
FUNCTION_TOOL_CALL_START_EVENT_ATTRS_POSITIONAL_ARGS_COUNT: len(function_args),
116-
FUNCTION_TOOL_CALL_START_EVENT_ATTRS_KEYWORD_ARGS_COUNT: len(function_kwargs)
108+
code_attributes.CODE_FUNCTION_NAME: wrapped_function.__name__,
109+
CODE_MODULE: wrapped_function.__module__,
110+
FUNCTION_TOOL_CALL_START_EVENT_ATTRS_POSITIONAL_ARGS_COUNT: len(
111+
function_args
112+
),
113+
FUNCTION_TOOL_CALL_START_EVENT_ATTRS_KEYWORD_ARGS_COUNT: len(
114+
function_kwargs
115+
),
117116
}
118117
body = {}
119118
if is_content_recording_enabled():
120-
body[FUNCTION_TOOL_CALL_START_EVENT_BODY_POSITIONAL_ARGS] = _to_otel_value(function_args)
121-
body[FUNCTION_TOOL_CALL_START_EVENT_BODY_KEYWORD_ARGS] = _to_otel_value(function_kwargs)
119+
body[FUNCTION_TOOL_CALL_START_EVENT_BODY_POSITIONAL_ARGS] = (
120+
_to_otel_value(function_args)
121+
)
122+
body[FUNCTION_TOOL_CALL_START_EVENT_BODY_KEYWORD_ARGS] = (
123+
_to_otel_value(function_kwargs)
124+
)
122125
otel_wrapper.log_function_call_start(attributes, body)
123126

124127

125128
def _record_function_call_arguments(
126-
otel_wrapper,
127-
wrapped_function,
128-
function_args,
129-
function_kwargs):
130-
_record_function_call_span_attributes(otel_wrapper, wrapped_function, function_args, function_kwargs)
131-
_record_function_call_event(otel_wrapper, wrapped_function, function_args, function_kwargs)
132-
133-
134-
def _record_function_call_result_event(
135-
otel_wrapper,
136-
wrapped_function,
137-
result):
129+
otel_wrapper, wrapped_function, function_args, function_kwargs
130+
):
131+
_record_function_call_span_attributes(
132+
otel_wrapper, wrapped_function, function_args, function_kwargs
133+
)
134+
_record_function_call_event(
135+
otel_wrapper, wrapped_function, function_args, function_kwargs
136+
)
137+
138+
139+
def _record_function_call_result_event(otel_wrapper, wrapped_function, result):
138140
"""Records the details about a function result as a log event."""
139141
attributes = {
140-
code_attributes.CODE_FUNCTION_NAME: wrapped_function.__name__,
141-
CODE_MODULE: wrapped_function.__module__,
142+
code_attributes.CODE_FUNCTION_NAME: wrapped_function.__name__,
143+
CODE_MODULE: wrapped_function.__module__,
142144
}
143145
body = {}
144146
if is_content_recording_enabled():
145-
body[FUNCTION_TOOL_CALL_END_EVENT_BODY_RESULT] = _to_otel_value(result)
147+
body[FUNCTION_TOOL_CALL_END_EVENT_BODY_RESULT] = _to_otel_value(result)
146148
otel_wrapper.log_function_call_end(attributes, body)
147149

148150

149-
def _record_function_call_result_span_attributes(otel_wrapper, wrapped_function, result):
151+
def _record_function_call_result_span_attributes(
152+
otel_wrapper, wrapped_function, result
153+
):
150154
"""Records the details about a function result as span attributes."""
151155
if not is_content_recording_enabled():
152156
return
@@ -156,59 +160,89 @@ def _record_function_call_result_span_attributes(otel_wrapper, wrapped_function,
156160

157161
def _record_function_call_result(otel_wrapper, wrapped_function, result):
158162
_record_function_call_result_event(otel_wrapper, wrapped_function, result)
159-
_record_function_call_result_span_attributes(otel_wrapper, wrapped_function, result)
163+
_record_function_call_result_span_attributes(
164+
otel_wrapper, wrapped_function, result
165+
)
160166

161167

162168
def _wrap_sync_tool_function(
163169
tool_function: ToolFunction,
164170
otel_wrapper: OTelWrapper,
165171
extra_span_attributes: Optional[dict[str, str]] = None,
166-
**kwargs):
172+
**kwargs,
173+
):
167174
@functools.wraps(tool_function)
168175
def wrapped_function(*args, **kwargs):
169176
span_name = _create_function_span_name(tool_function)
170-
attributes = _create_function_span_attributes(tool_function, args, kwargs, extra_span_attributes)
171-
with otel_wrapper.start_as_current_span(span_name, attributes=attributes):
172-
_record_function_call_arguments(otel_wrapper, tool_function, args, kwargs)
177+
attributes = _create_function_span_attributes(
178+
tool_function, args, kwargs, extra_span_attributes
179+
)
180+
with otel_wrapper.start_as_current_span(
181+
span_name, attributes=attributes
182+
):
183+
_record_function_call_arguments(
184+
otel_wrapper, tool_function, args, kwargs
185+
)
173186
result = tool_function(*args, **kwargs)
174187
_record_function_call_result(otel_wrapper, tool_function, result)
175188
return result
189+
176190
return wrapped_function
177191

178192

179193
def _wrap_async_tool_function(
180194
tool_function: ToolFunction,
181195
otel_wrapper: OTelWrapper,
182196
extra_span_attributes: Optional[dict[str, str]] = None,
183-
**kwargs):
197+
**kwargs,
198+
):
184199
@functools.wraps(tool_function)
185200
async def wrapped_function(*args, **kwargs):
186201
span_name = _create_function_span_name(tool_function)
187-
attributes = _create_function_span_attributes(tool_function, args, kwargs, extra_span_attributes)
188-
with otel_wrapper.start_as_current_span(span_name, attributes=attributes):
189-
_record_function_call_arguments(otel_wrapper, tool_function, args, kwargs)
202+
attributes = _create_function_span_attributes(
203+
tool_function, args, kwargs, extra_span_attributes
204+
)
205+
with otel_wrapper.start_as_current_span(
206+
span_name, attributes=attributes
207+
):
208+
_record_function_call_arguments(
209+
otel_wrapper, tool_function, args, kwargs
210+
)
190211
result = await tool_function(*args, **kwargs)
191212
_record_function_call_result(otel_wrapper, tool_function, result)
192213
return result
214+
193215
return wrapped_function
194216

195217

196-
def _wrap_tool_function(tool_function: ToolFunction, otel_wrapper: OTelWrapper, **kwargs):
218+
def _wrap_tool_function(
219+
tool_function: ToolFunction, otel_wrapper: OTelWrapper, **kwargs
220+
):
197221
if inspect.iscoroutinefunction(tool_function):
198222
return _wrap_async_tool_function(tool_function, otel_wrapper, **kwargs)
199223
return _wrap_sync_tool_function(tool_function, otel_wrapper, **kwargs)
200224

201225

202226
def wrapped(
203-
tool_or_tools: Optional[Union[ToolFunction, ToolOrDict, ToolListUnion, ToolListUnionDict]],
227+
tool_or_tools: Optional[
228+
Union[ToolFunction, ToolOrDict, ToolListUnion, ToolListUnionDict]
229+
],
204230
otel_wrapper: OTelWrapper,
205-
**kwargs):
231+
**kwargs,
232+
):
206233
if tool_or_tools is None:
207234
return None
208235
if isinstance(tool_or_tools, list):
209-
return [wrapped(item, otel_wrapper, **kwargs) for item in tool_or_tools]
236+
return [
237+
wrapped(item, otel_wrapper, **kwargs) for item in tool_or_tools
238+
]
210239
if isinstance(tool_or_tools, dict):
211-
return dict([(key, wrapped(value, otel_wrapper, **kwargs)) for (key, value) in tool_or_tools.items()])
240+
return dict(
241+
[
242+
(key, wrapped(value, otel_wrapper, **kwargs))
243+
for (key, value) in tool_or_tools.items()
244+
]
245+
)
212246
if callable(tool_or_tools):
213247
return _wrap_tool_function(tool_or_tools, otel_wrapper, **kwargs)
214248
return tool_or_tools

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def assert_has_span_named(self, name):
172172

173173
def assert_does_not_have_span_named(self, name):
174174
span = self.get_span_named(name)
175-
assert span is None, f'Found unexpected span named {name}'
175+
assert span is None, f"Found unexpected span named {name}"
176176

177177
def get_event_named(self, event_name):
178178
for event in self.get_finished_logs():

0 commit comments

Comments
 (0)