Skip to content

Commit df4bf0d

Browse files
committed
Fix lint and precommit after open-telemetry#3961
I clicked auto merge and it went through before the checks passed. Presumably because the required checks are out of date.
1 parent 3a3981a commit df4bf0d

File tree

4 files changed

+63
-23
lines changed

4 files changed

+63
-23
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@
4141
---
4242
"""
4343

44-
from .instrumentor import GoogleGenAiSdkInstrumentor
4544
from .generate_content import GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY
45+
from .instrumentor import GoogleGenAiSdkInstrumentor
4646
from .version import __version__
4747

48-
__all__ = ["GoogleGenAiSdkInstrumentor", "GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY", "__version__"]
48+
__all__ = [
49+
"GoogleGenAiSdkInstrumentor",
50+
"GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY",
51+
"__version__",
52+
]

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@
2020
import logging
2121
import os
2222
import time
23-
from typing import Any, AsyncIterator, Awaitable, Iterator, Optional, Union, Mapping
23+
from typing import (
24+
Any,
25+
AsyncIterator,
26+
Awaitable,
27+
Iterator,
28+
Mapping,
29+
Optional,
30+
Union,
31+
)
2432

2533
from google.genai.models import AsyncModels, Models
2634
from google.genai.models import t as transformers
@@ -37,6 +45,7 @@
3745
GenerateContentResponse,
3846
)
3947

48+
from opentelemetry import context as context_api
4049
from opentelemetry import trace
4150
from opentelemetry._logs import LogRecord
4251
from opentelemetry.instrumentation._semconv import (
@@ -57,10 +66,10 @@
5766
MessagePart,
5867
OutputMessage,
5968
)
69+
from opentelemetry.util.genai.utils import gen_ai_json_dumps
6070
from opentelemetry.util.types import (
6171
AttributeValue,
6272
)
63-
from opentelemetry.util.genai.utils import gen_ai_json_dumps
6473

6574
from .allowlist_util import AllowList
6675
from .custom_semconv import GCP_GENAI_OPERATION_CONFIG
@@ -73,7 +82,6 @@
7382
)
7483
from .otel_wrapper import OTelWrapper
7584
from .tool_call_wrapper import wrapped as wrapped_tool
76-
from opentelemetry import context as context_api
7785

7886
_logger = logging.getLogger(__name__)
7987

@@ -84,7 +92,10 @@
8492
# Constant used for the value of 'gen_ai.operation.name".
8593
_GENERATE_CONTENT_OP_NAME = "generate_content"
8694

87-
GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY = context_api.create_key("generate_content_extra_attributes_context_key")
95+
GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY = context_api.create_key(
96+
"generate_content_extra_attributes_context_key"
97+
)
98+
8899

89100
class _MethodsSnapshot:
90101
def __init__(self):
@@ -299,7 +310,9 @@ def _create_completion_details_attributes(
299310
return attributes
300311

301312

302-
def _get_extra_generate_content_attributes() -> Optional[Mapping[str, AttributeValue]]:
313+
def _get_extra_generate_content_attributes() -> Optional[
314+
Mapping[str, AttributeValue]
315+
]:
303316
return context_api.get_value(GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY)
304317

305318

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,21 @@
2020
from google.genai.types import GenerateContentConfig, Part
2121
from pydantic import BaseModel, Field
2222

23+
from opentelemetry import context as context_api
2324
from opentelemetry.instrumentation._semconv import (
2425
_OpenTelemetrySemanticConventionStability,
2526
_OpenTelemetryStabilitySignalType,
2627
_StabilityMode,
2728
)
29+
from opentelemetry.instrumentation.google_genai import (
30+
GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY,
31+
)
2832
from opentelemetry.semconv._incubating.attributes import (
2933
gen_ai_attributes,
3034
)
3135
from opentelemetry.util.genai.types import ContentCapturingMode
3236

3337
from .base import TestCase
34-
from opentelemetry import context as context_api
35-
from opentelemetry.instrumentation.google_genai import GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY
3638

3739
# pylint: disable=too-many-public-methods
3840

@@ -103,16 +105,25 @@ def test_generated_span_has_minimal_genai_attributes(self):
103105

104106
def test_generated_span_has_extra_genai_attributes(self):
105107
self.configure_valid_response(text="Yep, it works!")
106-
tok = context_api.attach(context_api.set_value(GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY, {"extra_attribute_key": "extra_attribute_value"}))
108+
tok = context_api.attach(
109+
context_api.set_value(
110+
GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY,
111+
{"extra_attribute_key": "extra_attribute_value"},
112+
)
113+
)
107114
try:
108115
self.generate_content(
109116
model="gemini-2.0-flash", contents="Does this work?"
110117
)
111-
self.otel.assert_has_span_named("generate_content gemini-2.0-flash")
112-
span = self.otel.get_span_named("generate_content gemini-2.0-flash")
113-
self.assertEqual(span.attributes["extra_attribute_key"], "extra_attribute_value")
114-
except:
115-
raise
118+
self.otel.assert_has_span_named(
119+
"generate_content gemini-2.0-flash"
120+
)
121+
span = self.otel.get_span_named(
122+
"generate_content gemini-2.0-flash"
123+
)
124+
self.assertEqual(
125+
span.attributes["extra_attribute_key"], "extra_attribute_value"
126+
)
116127
finally:
117128
context_api.detach(tok)
118129

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414

1515
import unittest
1616

17-
from .base import TestCase
1817
from opentelemetry import context as context_api
19-
from opentelemetry.instrumentation.google_genai import GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY
18+
from opentelemetry.instrumentation.google_genai import (
19+
GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY,
20+
)
21+
22+
from .base import TestCase
2023

2124

2225
class StreamingTestCase(TestCase):
@@ -46,16 +49,25 @@ def test_instrumentation_does_not_break_core_functionality(self):
4649

4750
def test_generated_span_has_extra_genai_attributes(self):
4851
self.configure_valid_response(text="Yep, it works!")
49-
tok = context_api.attach(context_api.set_value(GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY, {"extra_attribute_key": "extra_attribute_value"}))
52+
tok = context_api.attach(
53+
context_api.set_value(
54+
GENERATE_CONTENT_EXTRA_ATTRIBUTES_CONTEXT_KEY,
55+
{"extra_attribute_key": "extra_attribute_value"},
56+
)
57+
)
5058
try:
5159
self.generate_content(
5260
model="gemini-2.0-flash", contents="Does this work?"
5361
)
54-
self.otel.assert_has_span_named("generate_content gemini-2.0-flash")
55-
span = self.otel.get_span_named("generate_content gemini-2.0-flash")
56-
self.assertEqual(span.attributes["extra_attribute_key"], "extra_attribute_value")
57-
except:
58-
raise
62+
self.otel.assert_has_span_named(
63+
"generate_content gemini-2.0-flash"
64+
)
65+
span = self.otel.get_span_named(
66+
"generate_content gemini-2.0-flash"
67+
)
68+
self.assertEqual(
69+
span.attributes["extra_attribute_key"], "extra_attribute_value"
70+
)
5971
finally:
6072
context_api.detach(tok)
6173

0 commit comments

Comments
 (0)