Skip to content

Commit 2ae5f6a

Browse files
committed
add tests
1 parent 717ca2d commit 2ae5f6a

2 files changed

Lines changed: 1015 additions & 25 deletions

File tree

tests/test_otel.py

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -950,20 +950,22 @@ def test_error_level_in_span_creation(self, langfuse_client, memory_exporter):
950950
span = langfuse_client.start_span(
951951
name="create-error-span",
952952
level="ERROR",
953-
status_message="Initial error state"
953+
status_message="Initial error state",
954954
)
955955
span.end()
956956

957957
# Get the raw OTEL spans to check the status
958958
raw_spans = [
959-
s for s in memory_exporter.get_finished_spans()
959+
s
960+
for s in memory_exporter.get_finished_spans()
960961
if s.name == "create-error-span"
961962
]
962963
assert len(raw_spans) == 1, "Expected one span"
963964
raw_span = raw_spans[0]
964965

965966
# Verify OTEL span status was set to ERROR
966967
from opentelemetry.trace.status import StatusCode
968+
967969
assert raw_span.status.status_code == StatusCode.ERROR
968970
assert raw_span.status.description == "Initial error state"
969971

@@ -972,7 +974,10 @@ def test_error_level_in_span_creation(self, langfuse_client, memory_exporter):
972974
span_data = spans[0]
973975
attributes = span_data["attributes"]
974976
assert attributes[LangfuseOtelSpanAttributes.OBSERVATION_LEVEL] == "ERROR"
975-
assert attributes[LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE] == "Initial error state"
977+
assert (
978+
attributes[LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE]
979+
== "Initial error state"
980+
)
976981

977982
def test_error_level_in_span_update(self, langfuse_client, memory_exporter):
978983
"""Test that OTEL span status is set to ERROR when updating spans to level='ERROR'."""
@@ -985,14 +990,16 @@ def test_error_level_in_span_update(self, langfuse_client, memory_exporter):
985990

986991
# Get the raw OTEL spans to check the status
987992
raw_spans = [
988-
s for s in memory_exporter.get_finished_spans()
993+
s
994+
for s in memory_exporter.get_finished_spans()
989995
if s.name == "update-error-span"
990996
]
991997
assert len(raw_spans) == 1, "Expected one span"
992998
raw_span = raw_spans[0]
993999

9941000
# Verify OTEL span status was set to ERROR
9951001
from opentelemetry.trace.status import StatusCode
1002+
9961003
assert raw_span.status.status_code == StatusCode.ERROR
9971004
assert raw_span.status.description == "Updated to error state"
9981005

@@ -1001,7 +1008,10 @@ def test_error_level_in_span_update(self, langfuse_client, memory_exporter):
10011008
span_data = spans[0]
10021009
attributes = span_data["attributes"]
10031010
assert attributes[LangfuseOtelSpanAttributes.OBSERVATION_LEVEL] == "ERROR"
1004-
assert attributes[LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE] == "Updated to error state"
1011+
assert (
1012+
attributes[LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE]
1013+
== "Updated to error state"
1014+
)
10051015

10061016
def test_generation_error_level_in_creation(self, langfuse_client, memory_exporter):
10071017
"""Test that OTEL span status is set to ERROR when creating generations with level='ERROR'."""
@@ -1010,20 +1020,22 @@ def test_generation_error_level_in_creation(self, langfuse_client, memory_export
10101020
name="create-error-generation",
10111021
model="gpt-4",
10121022
level="ERROR",
1013-
status_message="Generation failed during creation"
1023+
status_message="Generation failed during creation",
10141024
)
10151025
generation.end()
10161026

10171027
# Get the raw OTEL spans to check the status
10181028
raw_spans = [
1019-
s for s in memory_exporter.get_finished_spans()
1029+
s
1030+
for s in memory_exporter.get_finished_spans()
10201031
if s.name == "create-error-generation"
10211032
]
10221033
assert len(raw_spans) == 1, "Expected one span"
10231034
raw_span = raw_spans[0]
10241035

10251036
# Verify OTEL span status was set to ERROR
10261037
from opentelemetry.trace.status import StatusCode
1038+
10271039
assert raw_span.status.status_code == StatusCode.ERROR
10281040
assert raw_span.status.description == "Generation failed during creation"
10291041

@@ -1032,31 +1044,36 @@ def test_generation_error_level_in_creation(self, langfuse_client, memory_export
10321044
span_data = spans[0]
10331045
attributes = span_data["attributes"]
10341046
assert attributes[LangfuseOtelSpanAttributes.OBSERVATION_LEVEL] == "ERROR"
1035-
assert attributes[LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE] == "Generation failed during creation"
1047+
assert (
1048+
attributes[LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE]
1049+
== "Generation failed during creation"
1050+
)
10361051

10371052
def test_generation_error_level_in_update(self, langfuse_client, memory_exporter):
10381053
"""Test that OTEL span status is set to ERROR when updating generations to level='ERROR'."""
10391054
# Create a normal generation
10401055
generation = langfuse_client.start_generation(
1041-
name="update-error-generation",
1042-
model="gpt-4",
1043-
level="INFO"
1056+
name="update-error-generation", model="gpt-4", level="INFO"
10441057
)
10451058

10461059
# Update it to ERROR level
1047-
generation.update(level="ERROR", status_message="Generation failed during execution")
1060+
generation.update(
1061+
level="ERROR", status_message="Generation failed during execution"
1062+
)
10481063
generation.end()
10491064

10501065
# Get the raw OTEL spans to check the status
10511066
raw_spans = [
1052-
s for s in memory_exporter.get_finished_spans()
1067+
s
1068+
for s in memory_exporter.get_finished_spans()
10531069
if s.name == "update-error-generation"
10541070
]
10551071
assert len(raw_spans) == 1, "Expected one span"
10561072
raw_span = raw_spans[0]
10571073

10581074
# Verify OTEL span status was set to ERROR
10591075
from opentelemetry.trace.status import StatusCode
1076+
10601077
assert raw_span.status.status_code == StatusCode.ERROR
10611078
assert raw_span.status.description == "Generation failed during execution"
10621079

@@ -1065,9 +1082,14 @@ def test_generation_error_level_in_update(self, langfuse_client, memory_exporter
10651082
span_data = spans[0]
10661083
attributes = span_data["attributes"]
10671084
assert attributes[LangfuseOtelSpanAttributes.OBSERVATION_LEVEL] == "ERROR"
1068-
assert attributes[LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE] == "Generation failed during execution"
1085+
assert (
1086+
attributes[LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE]
1087+
== "Generation failed during execution"
1088+
)
10691089

1070-
def test_non_error_levels_dont_set_otel_status(self, langfuse_client, memory_exporter):
1090+
def test_non_error_levels_dont_set_otel_status(
1091+
self, langfuse_client, memory_exporter
1092+
):
10711093
"""Test that non-ERROR levels don't set OTEL span status to ERROR."""
10721094
# Test different non-error levels
10731095
test_levels = ["INFO", "WARNING", "DEBUG", None]
@@ -1084,16 +1106,18 @@ def test_non_error_levels_dont_set_otel_status(self, langfuse_client, memory_exp
10841106

10851107
# Get the raw OTEL spans to check the status
10861108
raw_spans = [
1087-
s for s in memory_exporter.get_finished_spans()
1088-
if s.name == span_name
1109+
s for s in memory_exporter.get_finished_spans() if s.name == span_name
10891110
]
10901111
assert len(raw_spans) == 1, f"Expected one span for {span_name}"
10911112
raw_span = raw_spans[0]
10921113

10931114
# Verify OTEL span status was NOT set to ERROR
10941115
from opentelemetry.trace.status import StatusCode
1116+
10951117
# Default status should be UNSET, not ERROR
1096-
assert raw_span.status.status_code != StatusCode.ERROR, f"Level {level} should not set ERROR status"
1118+
assert (
1119+
raw_span.status.status_code != StatusCode.ERROR
1120+
), f"Level {level} should not set ERROR status"
10971121

10981122
def test_multiple_error_updates(self, langfuse_client, memory_exporter):
10991123
"""Test that multiple ERROR level updates work correctly."""
@@ -1110,14 +1134,16 @@ def test_multiple_error_updates(self, langfuse_client, memory_exporter):
11101134

11111135
# Get the raw OTEL spans to check the status
11121136
raw_spans = [
1113-
s for s in memory_exporter.get_finished_spans()
1137+
s
1138+
for s in memory_exporter.get_finished_spans()
11141139
if s.name == "multi-error-span"
11151140
]
11161141
assert len(raw_spans) == 1, "Expected one span"
11171142
raw_span = raw_spans[0]
11181143

11191144
# Verify OTEL span status shows the last error message
11201145
from opentelemetry.trace.status import StatusCode
1146+
11211147
assert raw_span.status.status_code == StatusCode.ERROR
11221148
assert raw_span.status.description == "Second error"
11231149

@@ -1129,22 +1155,34 @@ def test_error_without_status_message(self, langfuse_client, memory_exporter):
11291155

11301156
# Get the raw OTEL spans to check the status
11311157
raw_spans = [
1132-
s for s in memory_exporter.get_finished_spans()
1158+
s
1159+
for s in memory_exporter.get_finished_spans()
11331160
if s.name == "error-no-message-span"
11341161
]
11351162
assert len(raw_spans) == 1, "Expected one span"
11361163
raw_span = raw_spans[0]
11371164

11381165
# Verify OTEL span status was set to ERROR even without description
11391166
from opentelemetry.trace.status import StatusCode
1167+
11401168
assert raw_span.status.status_code == StatusCode.ERROR
11411169
# Description should be None when no status_message provided
11421170
assert raw_span.status.description is None
11431171

1144-
def test_different_observation_types_error_handling(self, langfuse_client, memory_exporter):
1172+
def test_different_observation_types_error_handling(
1173+
self, langfuse_client, memory_exporter
1174+
):
11451175
"""Test that ERROR level setting works for different observation types."""
11461176
# Test different observation types
1147-
observation_types = ["agent", "tool", "chain", "retriever", "evaluator", "embedding", "guardrail"]
1177+
observation_types = [
1178+
"agent",
1179+
"tool",
1180+
"chain",
1181+
"retriever",
1182+
"evaluator",
1183+
"embedding",
1184+
"guardrail",
1185+
]
11481186

11491187
# Create a parent span for child observations
11501188
with langfuse_client.start_as_current_span(name="error-test-parent") as parent:
@@ -1154,7 +1192,7 @@ def test_different_observation_types_error_handling(self, langfuse_client, memor
11541192
name=f"error-{obs_type}",
11551193
as_type=obs_type,
11561194
level="ERROR",
1157-
status_message=f"{obs_type} failed"
1195+
status_message=f"{obs_type} failed",
11581196
)
11591197
obs.end()
11601198

@@ -1167,8 +1205,13 @@ def test_different_observation_types_error_handling(self, langfuse_client, memor
11671205

11681206
raw_span = obs_spans[0]
11691207
from opentelemetry.trace.status import StatusCode
1170-
assert raw_span.status.status_code == StatusCode.ERROR, f"{obs_type} should have ERROR status"
1171-
assert raw_span.status.description == f"{obs_type} failed", f"{obs_type} should have correct description"
1208+
1209+
assert (
1210+
raw_span.status.status_code == StatusCode.ERROR
1211+
), f"{obs_type} should have ERROR status"
1212+
assert (
1213+
raw_span.status.description == f"{obs_type} failed"
1214+
), f"{obs_type} should have correct description"
11721215

11731216

11741217
class TestAdvancedSpans(TestOTelBase):

0 commit comments

Comments
 (0)