Skip to content

Commit 3d93c17

Browse files
authored
Set Status (#632)
1 parent 6950dba commit 3d93c17

File tree

8 files changed

+49
-1
lines changed

8 files changed

+49
-1
lines changed

src/judgeval/v1/instrumentation/llm/llm_anthropic/messages.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Tuple,
1313
)
1414

15+
from opentelemetry.trace import Status, StatusCode
1516
from judgeval.judgment_attribute_keys import AttributeKeys
1617
from judgeval.utils.serialize import safe_serialize
1718
from judgeval.utils.wrappers import (
@@ -130,6 +131,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
130131
span = ctx.get("span")
131132
if span:
132133
span.record_exception(error)
134+
span.set_status(Status(StatusCode.ERROR))
133135

134136
def finally_hook(ctx: Dict[str, Any]) -> None:
135137
span = ctx.get("span")
@@ -209,6 +211,7 @@ def error_hook_inner(inner_ctx: Dict[str, Any], error: Exception) -> None:
209211
span = ctx.get("span")
210212
if span:
211213
span.record_exception(error)
214+
span.set_status(Status(StatusCode.ERROR))
212215

213216
def finally_hook_inner(inner_ctx: Dict[str, Any]) -> None:
214217
span = ctx.get("span")
@@ -229,6 +232,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
229232
span = ctx.get("span")
230233
if span:
231234
span.record_exception(error)
235+
span.set_status(Status(StatusCode.ERROR))
232236

233237
return mutable_wrap_sync(
234238
original_func,
@@ -298,6 +302,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
298302
span = ctx.get("span")
299303
if span:
300304
span.record_exception(error)
305+
span.set_status(Status(StatusCode.ERROR))
301306

302307
def finally_hook(ctx: Dict[str, Any]) -> None:
303308
span = ctx.get("span")
@@ -378,6 +383,7 @@ def error_hook_inner(inner_ctx: Dict[str, Any], error: Exception) -> None:
378383
span = ctx.get("span")
379384
if span:
380385
span.record_exception(error)
386+
span.set_status(Status(StatusCode.ERROR))
381387

382388
def finally_hook_inner(inner_ctx: Dict[str, Any]) -> None:
383389
span = ctx.get("span")
@@ -398,6 +404,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
398404
span = ctx.get("span")
399405
if span:
400406
span.record_exception(error)
407+
span.set_status(Status(StatusCode.ERROR))
401408

402409
return mutable_wrap_async(
403410
original_func,

src/judgeval/v1/instrumentation/llm/llm_anthropic/messages_stream.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
AsyncGenerator,
88
)
99

10+
from opentelemetry.trace import Status, StatusCode
1011
from judgeval.judgment_attribute_keys import AttributeKeys
1112
from judgeval.utils.serialize import safe_serialize
1213
from judgeval.utils.wrappers import (
@@ -88,6 +89,7 @@ def error_hook_inner(inner_ctx: Dict[str, Any], error: Exception) -> None:
8889
span = ctx.get("span")
8990
if span:
9091
span.record_exception(error)
92+
span.set_status(Status(StatusCode.ERROR))
9193

9294
def finally_hook_inner(inner_ctx: Dict[str, Any]) -> None:
9395
pass
@@ -154,6 +156,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
154156
span = ctx.get("span")
155157
if span:
156158
span.record_exception(error)
159+
span.set_status(Status(StatusCode.ERROR))
157160

158161
wrapped = mutable_wrap_sync(
159162
original_func,
@@ -225,6 +228,7 @@ def error_hook_inner(inner_ctx: Dict[str, Any], error: Exception) -> None:
225228
span = ctx.get("span")
226229
if span:
227230
span.record_exception(error)
231+
span.set_status(Status(StatusCode.ERROR))
228232

229233
def finally_hook_inner_sync(inner_ctx: Dict[str, Any]) -> None:
230234
pass
@@ -291,6 +295,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
291295
span = ctx.get("span")
292296
if span:
293297
span.record_exception(error)
298+
span.set_status(Status(StatusCode.ERROR))
294299

295300
wrapped = mutable_wrap_sync(
296301
original_func,

src/judgeval/v1/instrumentation/llm/llm_google/generate_content.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Tuple,
88
)
99

10+
from opentelemetry.trace import Status, StatusCode
1011
from judgeval.judgment_attribute_keys import AttributeKeys
1112
from judgeval.utils.serialize import safe_serialize
1213
from judgeval.utils.wrappers import immutable_wrap_sync
@@ -102,6 +103,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
102103
span = ctx.get("span")
103104
if span:
104105
span.record_exception(error)
106+
span.set_status(Status(StatusCode.ERROR))
105107

106108
def finally_hook(ctx: Dict[str, Any]) -> None:
107109
span = ctx.get("span")

src/judgeval/v1/instrumentation/llm/llm_openai/beta_chat_completions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
TypeVar,
1010
)
1111

12+
from opentelemetry.trace import Status, StatusCode
1213
from judgeval.judgment_attribute_keys import AttributeKeys
1314
from judgeval.utils.serialize import safe_serialize
1415
from judgeval.utils.wrappers import (
@@ -103,6 +104,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
103104
span = ctx.get("span")
104105
if span:
105106
span.record_exception(error)
107+
span.set_status(Status(StatusCode.ERROR))
106108

107109
def finally_hook(ctx: Dict[str, Any]) -> None:
108110
span = ctx.get("span")
@@ -194,6 +196,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
194196
span = ctx.get("span")
195197
if span:
196198
span.record_exception(error)
199+
span.set_status(Status(StatusCode.ERROR))
197200

198201
def finally_hook(ctx: Dict[str, Any]) -> None:
199202
span = ctx.get("span")

src/judgeval/v1/instrumentation/llm/llm_openai/chat_completions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
)
1515
from packaging import version
1616

17+
from opentelemetry.trace import Status, StatusCode
1718
from judgeval.judgment_attribute_keys import AttributeKeys
1819
from judgeval.utils.serialize import safe_serialize
1920
from judgeval.utils.wrappers import (
@@ -126,6 +127,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
126127
span = ctx.get("span")
127128
if span:
128129
span.record_exception(error)
130+
span.set_status(Status(StatusCode.ERROR))
129131

130132
def finally_hook(ctx: Dict[str, Any]) -> None:
131133
span = ctx.get("span")
@@ -228,6 +230,7 @@ def error_hook_inner(inner_ctx: Dict[str, Any], error: Exception) -> None:
228230
span = ctx.get("span")
229231
if span:
230232
span.record_exception(error)
233+
span.set_status(Status(StatusCode.ERROR))
231234

232235
def finally_hook_inner(inner_ctx: Dict[str, Any]) -> None:
233236
span = ctx.get("span")
@@ -248,6 +251,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
248251
span = ctx.get("span")
249252
if span:
250253
span.record_exception(error)
254+
span.set_status(Status(StatusCode.ERROR))
251255

252256
return mutable_wrap_sync(
253257
original_func,
@@ -337,6 +341,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
337341
span = ctx.get("span")
338342
if span:
339343
span.record_exception(error)
344+
span.set_status(Status(StatusCode.ERROR))
340345

341346
def finally_hook(ctx: Dict[str, Any]) -> None:
342347
span = ctx.get("span")
@@ -440,6 +445,7 @@ def error_hook_inner(inner_ctx: Dict[str, Any], error: Exception) -> None:
440445
span = ctx.get("span")
441446
if span:
442447
span.record_exception(error)
448+
span.set_status(Status(StatusCode.ERROR))
443449

444450
def finally_hook_inner(inner_ctx: Dict[str, Any]) -> None:
445451
span = ctx.get("span")
@@ -460,6 +466,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
460466
span = ctx.get("span")
461467
if span:
462468
span.record_exception(error)
469+
span.set_status(Status(StatusCode.ERROR))
463470

464471
return mutable_wrap_async(
465472
original_func,

src/judgeval/v1/instrumentation/llm/llm_openai/responses.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
TypeVar,
1414
)
1515

16+
from opentelemetry.trace import Status, StatusCode
1617
from judgeval.judgment_attribute_keys import AttributeKeys
1718
from judgeval.utils.serialize import safe_serialize
1819
from judgeval.utils.wrappers import (
@@ -117,6 +118,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
117118
span = ctx.get("span")
118119
if span:
119120
span.record_exception(error)
121+
span.set_status(Status(StatusCode.ERROR))
120122

121123
def finally_hook(ctx: Dict[str, Any]) -> None:
122124
span = ctx.get("span")
@@ -221,6 +223,7 @@ def error_hook_inner(inner_ctx: Dict[str, Any], error: Exception) -> None:
221223
span = ctx.get("span")
222224
if span:
223225
span.record_exception(error)
226+
span.set_status(Status(StatusCode.ERROR))
224227

225228
def finally_hook_inner(inner_ctx: Dict[str, Any]) -> None:
226229
span = ctx.get("span")
@@ -241,6 +244,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
241244
span = ctx.get("span")
242245
if span:
243246
span.record_exception(error)
247+
span.set_status(Status(StatusCode.ERROR))
244248

245249
return mutable_wrap_sync(
246250
original_func,
@@ -330,6 +334,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
330334
span = ctx.get("span")
331335
if span:
332336
span.record_exception(error)
337+
span.set_status(Status(StatusCode.ERROR))
333338

334339
def finally_hook(ctx: Dict[str, Any]) -> None:
335340
span = ctx.get("span")
@@ -436,6 +441,7 @@ def error_hook_inner(inner_ctx: Dict[str, Any], error: Exception) -> None:
436441
span = ctx.get("span")
437442
if span:
438443
span.record_exception(error)
444+
span.set_status(Status(StatusCode.ERROR))
439445

440446
def finally_hook_inner(inner_ctx: Dict[str, Any]) -> None:
441447
span = ctx.get("span")
@@ -456,6 +462,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
456462
span = ctx.get("span")
457463
if span:
458464
span.record_exception(error)
465+
span.set_status(Status(StatusCode.ERROR))
459466

460467
return mutable_wrap_async(
461468
original_func,

src/judgeval/v1/instrumentation/llm/llm_together/chat_completions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
AsyncGenerator,
1212
)
1313

14+
from opentelemetry.trace import Status, StatusCode
1415
from judgeval.judgment_attribute_keys import AttributeKeys
1516
from judgeval.utils.serialize import safe_serialize
1617
from judgeval.utils.wrappers import (
@@ -101,6 +102,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
101102
span = ctx.get("span")
102103
if span:
103104
span.record_exception(error)
105+
span.set_status(Status(StatusCode.ERROR))
104106

105107
def finally_hook(ctx: Dict[str, Any]) -> None:
106108
span = ctx.get("span")
@@ -179,6 +181,7 @@ def error_hook_inner(inner_ctx: Dict[str, Any], error: Exception) -> None:
179181
span = ctx.get("span")
180182
if span:
181183
span.record_exception(error)
184+
span.set_status(Status(StatusCode.ERROR))
182185

183186
def finally_hook_inner(inner_ctx: Dict[str, Any]) -> None:
184187
span = ctx.get("span")
@@ -199,6 +202,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
199202
span = ctx.get("span")
200203
if span:
201204
span.record_exception(error)
205+
span.set_status(Status(StatusCode.ERROR))
202206

203207
return mutable_wrap_sync(
204208
original_func,
@@ -267,6 +271,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
267271
span = ctx.get("span")
268272
if span:
269273
span.record_exception(error)
274+
span.set_status(Status(StatusCode.ERROR))
270275

271276
def finally_hook(ctx: Dict[str, Any]) -> None:
272277
span = ctx.get("span")
@@ -346,6 +351,7 @@ def error_hook_inner(inner_ctx: Dict[str, Any], error: Exception) -> None:
346351
span = ctx.get("span")
347352
if span:
348353
span.record_exception(error)
354+
span.set_status(Status(StatusCode.ERROR))
349355

350356
def finally_hook_inner(inner_ctx: Dict[str, Any]) -> None:
351357
span = ctx.get("span")
@@ -366,6 +372,7 @@ def error_hook(ctx: Dict[str, Any], error: Exception) -> None:
366372
span = ctx.get("span")
367373
if span:
368374
span.record_exception(error)
375+
span.set_status(Status(StatusCode.ERROR))
369376

370377
return mutable_wrap_async(
371378
original_func,

src/tests/v1/instrumentation/llm/utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from typing import Any, Dict
44
from opentelemetry.sdk.trace import ReadableSpan
5+
from opentelemetry.trace import StatusCode
56

67
from judgeval.judgment_attribute_keys import AttributeKeys
78

@@ -87,12 +88,14 @@ def verify_span_attributes_comprehensive(
8788
def assert_span_has_exception(
8889
span: ReadableSpan,
8990
expected_span_name: str,
91+
check_status: bool = True,
9092
) -> None:
91-
"""Assert that a span has exception events recorded.
93+
"""Assert that a span has exception events recorded and error status set.
9294
9395
Args:
9496
span: The span to validate
9597
expected_span_name: Expected span name
98+
check_status: Whether to verify span status is set to ERROR (default: True)
9699
"""
97100
assert span is not None
98101
assert span.name == expected_span_name
@@ -101,3 +104,10 @@ def assert_span_has_exception(
101104
if span.events:
102105
event_names = [event.name for event in span.events]
103106
assert any("exception" in name.lower() for name in event_names)
107+
108+
# Verify span status is set to ERROR
109+
if check_status:
110+
assert span.status is not None, "Span status should be set when an error occurs"
111+
assert span.status.status_code == StatusCode.ERROR, (
112+
f"Expected span status to be ERROR, got {span.status.status_code}"
113+
)

0 commit comments

Comments
 (0)