Skip to content

Commit 375e33c

Browse files
committed
Commit latest changes
1 parent add1ec3 commit 375e33c

File tree

6 files changed

+33
-48
lines changed

6 files changed

+33
-48
lines changed

instrumentation-genai/opentelemetry-instrumentation-vertexai/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ classifiers = [
2525
]
2626
dependencies = [
2727
"opentelemetry-api ~= 1.28",
28-
"opentelemetry-instrumentation == 0.58b0dev",
28+
"opentelemetry-instrumentation ~= 0.58b0",
2929
"opentelemetry-util-genai == 0.1b0.dev",
30-
"opentelemetry-semantic-conventions == 0.58b0dev",
30+
"opentelemetry-semantic-conventions ~= 0.58b0",
3131
]
3232

3333
[project.optional-dependencies]

instrumentation-genai/opentelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai/__init__.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
from typing import Any, Collection
4545

46+
from typing_extensions import assert_never
4647
from wrapt import (
4748
wrap_function_wrapper, # type: ignore[reportUnknownVariableType]
4849
)
@@ -109,18 +110,26 @@ def instrumentation_dependencies(self) -> Collection[str]:
109110

110111
def _instrument(self, **kwargs: Any):
111112
"""Enable VertexAI instrumentation."""
113+
sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode(
114+
_OpenTelemetryStabilitySignalType.GEN_AI,
115+
)
112116
tracer_provider = kwargs.get("tracer_provider")
117+
schema = (
118+
Schemas.V1_28_0.value
119+
if sem_conv_opt_in_mode == _StabilityMode.DEFAULT
120+
else Schemas.V1_36_0
121+
)
113122
tracer = get_tracer(
114123
__name__,
115124
"",
116125
tracer_provider,
117-
schema_url=Schemas.V1_28_0.value,
126+
schema_url=schema,
118127
)
119128
event_logger_provider = kwargs.get("event_logger_provider")
120129
event_logger = get_event_logger(
121130
__name__,
122131
"",
123-
schema_url=Schemas.V1_28_0.value,
132+
schema_url=schema,
124133
event_logger_provider=event_logger_provider,
125134
)
126135
sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode(
@@ -143,10 +152,7 @@ def _instrument(self, **kwargs: Any):
143152
sem_conv_opt_in_mode,
144153
)
145154
else:
146-
# Impossible to reach here, only 2 opt-in modes exist for GEN_AI.
147-
raise ValueError(
148-
f"Sem Conv opt in mode {sem_conv_opt_in_mode} not supported."
149-
)
155+
assert_never()
150156
for client_class, method_name, wrapper in _methods_to_wrap(
151157
method_wrappers
152158
):

instrumentation-genai/opentelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai/patch.py

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,6 @@ def __init__(
134134
self.capture_content = capture_content
135135
self.sem_conv_opt_in_mode = sem_conv_opt_in_mode
136136

137-
# Deprecations:
138-
# - `gen_ai.system.message` event - use `gen_ai.system_instructions` or
139-
# `gen_ai.input.messages` attributes instead.
140-
# - `gen_ai.user.message`, `gen_ai.assistant.message`, `gen_ai.tool.message` events
141-
# (use `gen_ai.input.messages` attribute instead)
142-
# - `gen_ai.choice` event (use `gen_ai.output.messages` attribute instead)
143137
@contextmanager
144138
def _with_new_instrumentation(
145139
self,
@@ -166,9 +160,10 @@ def _with_new_instrumentation(
166160

167161
def handle_response(
168162
response: prediction_service.GenerateContentResponse
169-
| prediction_service_v1beta1.GenerateContentResponse,
163+
| prediction_service_v1beta1.GenerateContentResponse
164+
| None,
170165
) -> None:
171-
if span.is_recording():
166+
if span.is_recording() and response:
172167
# When streaming, this is called multiple times so attributes would be
173168
# overwritten. In practice, it looks the API only returns the interesting
174169
# attributes on the last streamed response. However, I couldn't find
@@ -266,20 +261,11 @@ def generate_content(
266261
with self._with_new_instrumentation(
267262
capture_content, instance, args, kwargs
268263
) as handle_response:
264+
response = None
269265
try:
270266
response = wrapped(*args, **kwargs)
271-
except Exception as e:
272-
api_endpoint: str = instance.api_endpoint # type: ignore[reportUnknownMemberType]
273-
self.event_logger.emit(
274-
create_operation_details_event(
275-
params=_extract_params(*args, **kwargs),
276-
response=None,
277-
capture_content=capture_content,
278-
api_endpoint=api_endpoint,
279-
)
280-
)
281-
raise e
282-
handle_response(response)
267+
finally:
268+
handle_response(response)
283269
return response
284270

285271
async def agenerate_content(
@@ -312,18 +298,9 @@ async def agenerate_content(
312298
with self._with_new_instrumentation(
313299
capture_content, instance, args, kwargs
314300
) as handle_response:
301+
response = None
315302
try:
316303
response = await wrapped(*args, **kwargs)
317-
except Exception as e:
318-
api_endpoint: str = instance.api_endpoint # type: ignore[reportUnknownMemberType]
319-
self.event_logger.emit(
320-
create_operation_details_event(
321-
params=_extract_params(*args, **kwargs),
322-
response=None,
323-
capture_content=capture_content,
324-
api_endpoint=api_endpoint,
325-
)
326-
)
327-
raise e
328-
handle_response(response)
304+
finally:
305+
handle_response(response)
329306
return response

instrumentation-genai/opentelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai/utils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def create_operation_details_event(
329329
}:
330330
return event
331331
if params.system_instruction:
332-
attributes["gen_ai.system_instructions"] = [
332+
attributes[GenAIAttributes.GEN_AI_SYSTEM_INSTRUCTIONS] = [
333333
{
334334
"type": "text",
335335
"content": "\n".join(
@@ -338,12 +338,12 @@ def create_operation_details_event(
338338
}
339339
]
340340
if params.contents:
341-
attributes["gen_ai.input.messages"] = [
341+
attributes[GenAIAttributes.GEN_AI_INPUT_MESSAGES] = [
342342
asdict(_convert_content_to_message(content))
343343
for content in params.contents
344344
]
345345
if response and response.candidates:
346-
attributes["gen_ai.output.messages"] = [
346+
attributes[GenAIAttributes.GEN_AI_OUTPUT_MESSAGES] = [
347347
asdict(x) for x in _convert_response_to_output_messages(response)
348348
]
349349
return event
@@ -370,7 +370,6 @@ def _convert_content_to_message(
370370
content: content.Content | content_v1beta1.Content,
371371
) -> InputMessage:
372372
parts: MessagePart = []
373-
message = InputMessage(role=content.role, parts=parts)
374373
for idx, part in enumerate(content.parts):
375374
if "function_response" in part:
376375
part = part.function_response
@@ -399,7 +398,7 @@ def _convert_content_to_message(
399398
)
400399
dict_part["type"] = type(part)
401400
parts.append(dict_part)
402-
return message
401+
return InputMessage(role=content.role, parts=parts)
403402

404403

405404
def response_to_events(

instrumentation-genai/opentelemetry-instrumentation-vertexai/tests/requirements.oldest.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ vcrpy==6.0.2
6464
wrapt==1.16.0
6565
yarl==1.15.2
6666
zipp==3.20.2
67-
6867
# when updating, also update in pyproject.toml
69-
-e opentelemetry-instrumentation
68+
opentelemetry-api==1.37
69+
opentelemetry-sdk==1.37
70+
opentelemetry-semantic-conventions==0.58b0
71+
opentelemetry-instrumentation==0.58b0
72+
73+
7074
-e instrumentation-genai/opentelemetry-instrumentation-vertexai[instruments]
7175
-e util/opentelemetry-util-genai

tox.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,6 @@ deps =
458458
openai-latest: -r {toxinidir}/instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/requirements.latest.txt
459459
lint-instrumentation-openai-v2: -r {toxinidir}/instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/requirements.oldest.txt
460460

461-
vertexai-oldest: {[testenv]test_deps}
462461
vertexai-oldest: -r {toxinidir}/instrumentation-genai/opentelemetry-instrumentation-vertexai/tests/requirements.oldest.txt
463462
vertexai-latest: {[testenv]test_deps}
464463
vertexai-latest: -r {toxinidir}/instrumentation-genai/opentelemetry-instrumentation-vertexai/tests/requirements.latest.txt

0 commit comments

Comments
 (0)