Skip to content

Commit 5bed336

Browse files
committed
Add more teest cases for error conditions and fix span name bug
1 parent 874098f commit 5bed336

File tree

6 files changed

+131
-11
lines changed

6 files changed

+131
-11
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,6 @@ def is_content_enabled() -> bool:
134134
def get_span_name(span_attributes: Mapping[str, AttributeValue]):
135135
name = span_attributes[GenAIAttributes.GEN_AI_OPERATION_NAME]
136136
model = span_attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL]
137+
if not model:
138+
return name
137139
return f"{name} {model}"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ interactions:
4343
]
4444
},
4545
"finishReason": 1,
46-
"avgLogprobs": -0.0054909539850134595
46+
"avgLogprobs": -0.005692833348324424
4747
}
4848
],
4949
"usageMetadata": {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
interactions:
2+
- request:
3+
body: |-
4+
{
5+
"contents": [
6+
{
7+
"role": "user",
8+
"parts": [
9+
{
10+
"text": "Say this is a test"
11+
}
12+
]
13+
}
14+
]
15+
}
16+
headers:
17+
Accept:
18+
- '*/*'
19+
Accept-Encoding:
20+
- gzip, deflate
21+
Connection:
22+
- keep-alive
23+
Content-Length:
24+
- '141'
25+
Content-Type:
26+
- application/json
27+
User-Agent:
28+
- python-requests/2.32.3
29+
method: POST
30+
uri: https://us-central1-aiplatform.googleapis.com/v1/projects/fake-project/locations/us-central1/publishers/google/models/gemini-does-not-exist:generateContent?%24alt=json%3Benum-encoding%3Dint
31+
response:
32+
body:
33+
string: |-
34+
{
35+
"error": {
36+
"code": 404,
37+
"message": "Publisher Model `projects/otel-starter-project/locations/us-central1/publishers/google/models/gemini-does-not-exist` not found.",
38+
"status": "NOT_FOUND",
39+
"details": []
40+
}
41+
}
42+
headers:
43+
Content-Type:
44+
- application/json; charset=UTF-8
45+
Transfer-Encoding:
46+
- chunked
47+
Vary:
48+
- Origin
49+
- X-Origin
50+
- Referer
51+
content-length:
52+
- '672'
53+
status:
54+
code: 404
55+
message: Not Found
56+
version: 1

instrumentation-genai/opentelemetry-instrumentation-vertexai/tests/test_chat_completions.py

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from google.api_core.exceptions import BadRequest
2+
from google.api_core.exceptions import BadRequest, NotFound
33
from vertexai.generative_models import (
44
Content,
55
GenerationConfig,
@@ -8,14 +8,15 @@
88
)
99

1010
from opentelemetry.instrumentation.vertexai import VertexAIInstrumentor
11+
from opentelemetry.sdk.trace import ReadableSpan
1112
from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
1213
InMemorySpanExporter,
1314
)
1415
from opentelemetry.trace import StatusCode
1516

1617

1718
@pytest.mark.vcr
18-
def test_vertexai_generate_content(
19+
def test_generate_content(
1920
span_exporter: InMemorySpanExporter,
2021
instrument_with_content: VertexAIInstrumentor,
2122
):
@@ -37,7 +38,65 @@ def test_vertexai_generate_content(
3738

3839

3940
@pytest.mark.vcr
40-
def test_vertexai_generate_content_error(
41+
def test_generate_content_empty_model(
42+
span_exporter: InMemorySpanExporter,
43+
instrument_with_content: VertexAIInstrumentor,
44+
):
45+
model = GenerativeModel("")
46+
try:
47+
model.generate_content(
48+
[
49+
Content(
50+
role="user", parts=[Part.from_text("Say this is a test")]
51+
)
52+
],
53+
)
54+
except ValueError:
55+
pass
56+
57+
spans = span_exporter.get_finished_spans()
58+
assert len(spans) == 1
59+
assert spans[0].name == "chat"
60+
# Captures invalid params
61+
assert dict(spans[0].attributes) == {
62+
"gen_ai.operation.name": "chat",
63+
"gen_ai.request.model": "",
64+
"gen_ai.system": "vertex_ai",
65+
}
66+
assert_span_error(spans[0])
67+
68+
69+
@pytest.mark.vcr
70+
def test_generate_content_missing_model(
71+
span_exporter: InMemorySpanExporter,
72+
instrument_with_content: VertexAIInstrumentor,
73+
):
74+
model = GenerativeModel("gemini-does-not-exist")
75+
try:
76+
model.generate_content(
77+
[
78+
Content(
79+
role="user", parts=[Part.from_text("Say this is a test")]
80+
)
81+
],
82+
)
83+
except NotFound:
84+
pass
85+
86+
spans = span_exporter.get_finished_spans()
87+
assert len(spans) == 1
88+
assert spans[0].name == "chat gemini-does-not-exist"
89+
# Captures invalid params
90+
assert dict(spans[0].attributes) == {
91+
"gen_ai.operation.name": "chat",
92+
"gen_ai.request.model": "gemini-does-not-exist",
93+
"gen_ai.system": "vertex_ai",
94+
}
95+
assert_span_error(spans[0])
96+
97+
98+
@pytest.mark.vcr
99+
def test_generate_content_invalid_temperature(
41100
span_exporter: InMemorySpanExporter,
42101
instrument_with_content: VertexAIInstrumentor,
43102
):
@@ -64,16 +123,11 @@ def test_vertexai_generate_content_error(
64123
"gen_ai.request.temperature": 1000.0,
65124
"gen_ai.system": "vertex_ai",
66125
}
67-
# Sets error status
68-
assert spans[0].status.status_code == StatusCode.ERROR
69-
70-
# Records exception event
71-
assert len(spans[0].events) == 1
72-
assert spans[0].events[0].name == "exception"
126+
assert_span_error(spans[0])
73127

74128

75129
@pytest.mark.vcr()
76-
def test_chat_completion_extra_params(span_exporter, instrument_no_content):
130+
def test_generate_content_extra_params(span_exporter, instrument_no_content):
77131
generation_config = GenerationConfig(
78132
top_k=2,
79133
top_p=0.95,
@@ -105,3 +159,11 @@ def test_chat_completion_extra_params(span_exporter, instrument_no_content):
105159
"gen_ai.request.top_p": 0.949999988079071,
106160
"gen_ai.system": "vertex_ai",
107161
}
162+
163+
164+
def assert_span_error(span: ReadableSpan) -> None:
165+
# Sets error status
166+
assert span.status.status_code == StatusCode.ERROR
167+
# Records exception event
168+
error_events = [e for e in span.events if e.name == "exception"]
169+
assert error_events != []

0 commit comments

Comments
 (0)