|
43 | 43 | get_function_span, |
44 | 44 | ) |
45 | 45 |
|
| 46 | + |
| 47 | +def _span_attributes(span: Any) -> dict[str, Any]: |
| 48 | + attributes = span.attributes |
| 49 | + return dict(attributes) if attributes is not None else {} |
| 50 | + |
| 51 | + |
46 | 52 | # region Test constants |
47 | 53 |
|
48 | 54 |
|
@@ -2043,7 +2049,8 @@ async def test_enable_instrumentation_controls_message_event_emission( |
2043 | 2049 |
|
2044 | 2050 | spans = span_exporter.get_finished_spans() |
2045 | 2051 | assert len(spans) == 1 |
2046 | | - attributes = spans[0].attributes or {} |
| 2052 | + attributes = spans[0].attributes |
| 2053 | + assert attributes is not None |
2047 | 2054 | assert (OtelAttr.INPUT_MESSAGES in attributes) is (enable_sensitive_data and bool(semconv)) |
2048 | 2055 | assert (OtelAttr.OUTPUT_MESSAGES in attributes) is (enable_sensitive_data and bool(semconv)) |
2049 | 2056 |
|
@@ -2528,7 +2535,8 @@ async def test_chat_client_observability_provider_name_under_baseline_semconv( |
2528 | 2535 |
|
2529 | 2536 | spans = span_exporter.get_finished_spans() |
2530 | 2537 | assert len(spans) == 1 |
2531 | | - attributes = spans[0].attributes or {} |
| 2538 | + attributes = spans[0].attributes |
| 2539 | + assert attributes is not None |
2532 | 2540 | assert OtelAttr.SYSTEM in attributes |
2533 | 2541 | assert OtelAttr.PROVIDER_NAME not in attributes |
2534 | 2542 |
|
@@ -4026,7 +4034,8 @@ def test_capture_messages_baseline_semconv_emits_events_only(span_exporter: InMe |
4026 | 4034 |
|
4027 | 4035 | spans = span_exporter.get_finished_spans() |
4028 | 4036 | assert len(spans) == 1 |
4029 | | - attributes = spans[0].attributes or {} |
| 4037 | + attributes = spans[0].attributes |
| 4038 | + assert attributes is not None |
4030 | 4039 | assert OtelAttr.INPUT_MESSAGES not in attributes |
4031 | 4040 | mock_emit.assert_called_once() |
4032 | 4041 | assert mock_emit.call_args.kwargs["event_name"] == "gen_ai.user.message" |
@@ -4061,7 +4070,8 @@ def test_capture_messages_latest_experimental_emits_span_attribute_only_when_eve |
4061 | 4070 |
|
4062 | 4071 | spans = span_exporter.get_finished_spans() |
4063 | 4072 | assert len(spans) == 1 |
4064 | | - attributes = spans[0].attributes or {} |
| 4073 | + attributes = spans[0].attributes |
| 4074 | + assert attributes is not None |
4065 | 4075 | input_messages = json.loads(cast(str, attributes[OtelAttr.INPUT_MESSAGES])) |
4066 | 4076 | assert [msg.get("role") for msg in input_messages] == ["user"] |
4067 | 4077 | assert mock_emit.call_count == 0 |
@@ -4094,7 +4104,8 @@ def test_capture_messages_defaults_emit_both_events_and_span_attribute(span_expo |
4094 | 4104 |
|
4095 | 4105 | spans = span_exporter.get_finished_spans() |
4096 | 4106 | assert len(spans) == 1 |
4097 | | - attributes = spans[0].attributes or {} |
| 4107 | + attributes = spans[0].attributes |
| 4108 | + assert attributes is not None |
4098 | 4109 | input_messages = json.loads(cast(str, attributes[OtelAttr.INPUT_MESSAGES])) |
4099 | 4110 | assert [msg.get("role") for msg in input_messages] == ["user"] |
4100 | 4111 | mock_emit.assert_called_once() |
@@ -4162,7 +4173,8 @@ def test_capture_messages_preserves_custom_finish_reason(span_exporter: InMemory |
4162 | 4173 |
|
4163 | 4174 | assert mock_emit.call_args.kwargs["body"]["finish_reason"] == "guardrail" |
4164 | 4175 | spans = span_exporter.get_finished_spans() |
4165 | | - attributes = spans[0].attributes or {} |
| 4176 | + attributes = spans[0].attributes |
| 4177 | + assert attributes is not None |
4166 | 4178 | output_messages = json.loads(cast(str, attributes[OtelAttr.OUTPUT_MESSAGES])) |
4167 | 4179 | assert output_messages[-1]["finish_reason"] == "guardrail" |
4168 | 4180 |
|
@@ -4199,7 +4211,9 @@ def test_capture_messages_exports_native_otel_event(span_exporter: InMemorySpanE |
4199 | 4211 | record = readable_record.log_record |
4200 | 4212 | assert record.event_name == "gen_ai.user.message" |
4201 | 4213 | assert record.body == {"content": "Test"} |
4202 | | - assert dict(record.attributes or {}) == {"gen_ai.system": "test_provider"} |
| 4214 | + record_attributes = record.attributes |
| 4215 | + assert record_attributes is not None |
| 4216 | + assert dict(record_attributes) == {"gen_ai.system": "test_provider"} |
4203 | 4217 | assert record.trace_id == span_context.trace_id |
4204 | 4218 | assert record.span_id == span_context.span_id |
4205 | 4219 |
|
@@ -5749,7 +5763,7 @@ async def before_run( |
5749 | 5763 | agent_spans = [ |
5750 | 5764 | span |
5751 | 5765 | for span in spans |
5752 | | - if span.attributes and span.attributes.get(OtelAttr.OPERATION.value) == OtelAttr.AGENT_INVOKE_OPERATION |
| 5766 | + if _span_attributes(span).get(OtelAttr.OPERATION.value) == OtelAttr.AGENT_INVOKE_OPERATION |
5753 | 5767 | ] |
5754 | 5768 | assert len(agent_spans) == 1 |
5755 | 5769 |
|
@@ -5813,13 +5827,13 @@ async def after_run( |
5813 | 5827 | agent_spans = [ |
5814 | 5828 | span |
5815 | 5829 | for span in spans |
5816 | | - if span.attributes and span.attributes.get(OtelAttr.OPERATION.value) == OtelAttr.AGENT_INVOKE_OPERATION |
| 5830 | + if _span_attributes(span).get(OtelAttr.OPERATION.value) == OtelAttr.AGENT_INVOKE_OPERATION |
5817 | 5831 | ] |
5818 | 5832 | assert len(agent_spans) == 1 |
5819 | 5833 | chat_spans = [ |
5820 | 5834 | span |
5821 | 5835 | for span in spans |
5822 | | - if span.attributes and span.attributes.get(OtelAttr.OPERATION.value) == OtelAttr.CHAT_COMPLETION_OPERATION |
| 5836 | + if _span_attributes(span).get(OtelAttr.OPERATION.value) == OtelAttr.CHAT_COMPLETION_OPERATION |
5823 | 5837 | ] |
5824 | 5838 | assert len(chat_spans) == 2 |
5825 | 5839 |
|
|
0 commit comments