@@ -606,187 +606,3 @@ def test_update_current_span(sentry_init, capture_events):
606606 "thread.id" : mock .ANY ,
607607 "thread.name" : mock .ANY ,
608608 }
609-
610-
611- class TestConversationIdPropagation :
612- """Tests for conversation_id propagation to AI spans."""
613-
614- def test_conversation_id_propagates_to_span_with_gen_ai_operation_name (
615- self , sentry_init , capture_events
616- ):
617- """Span with gen_ai.operation.name data should get conversation_id."""
618- sentry_init (traces_sample_rate = 1.0 )
619- events = capture_events ()
620-
621- scope = sentry_sdk .get_current_scope ()
622- scope .set_conversation_id ("conv-op-name-test" )
623-
624- with sentry_sdk .start_transaction (name = "test-tx" ):
625- with start_span (op = "http.client" ) as span :
626- span .set_data ("gen_ai.operation.name" , "chat" )
627-
628- (event ,) = events
629- span_data = event ["spans" ][0 ]["data" ]
630- assert span_data .get ("gen_ai.conversation.id" ) == "conv-op-name-test"
631-
632- def test_conversation_id_propagates_to_span_with_ai_op (
633- self , sentry_init , capture_events
634- ):
635- """Span with ai.* op should get conversation_id."""
636- sentry_init (traces_sample_rate = 1.0 )
637- events = capture_events ()
638-
639- scope = sentry_sdk .get_current_scope ()
640- scope .set_conversation_id ("conv-ai-op-test" )
641-
642- with sentry_sdk .start_transaction (name = "test-tx" ):
643- with start_span (op = "ai.chat.completions" ):
644- pass
645-
646- (event ,) = events
647- span_data = event ["spans" ][0 ]["data" ]
648- assert span_data .get ("gen_ai.conversation.id" ) == "conv-ai-op-test"
649-
650- @pytest .mark .parametrize ("stream_gen_ai_spans" , [True , False ])
651- def test_conversation_id_propagates_to_span_with_gen_ai_op (
652- self , sentry_init , capture_events , capture_items , stream_gen_ai_spans
653- ):
654- """Span with gen_ai.* op should get conversation_id."""
655- sentry_init (
656- traces_sample_rate = 1.0 ,
657- stream_gen_ai_spans = stream_gen_ai_spans ,
658- )
659-
660- if stream_gen_ai_spans :
661- items = capture_items ("span" )
662-
663- scope = sentry_sdk .get_current_scope ()
664- scope .set_conversation_id ("conv-gen-ai-op-test" )
665-
666- with sentry_sdk .start_transaction (name = "test-tx" ):
667- with start_span (op = "gen_ai.invoke_agent" ):
668- pass
669-
670- spans = [item .payload for item in items if item .type == "span" ]
671- span_data = spans [0 ]["attributes" ]
672- else :
673- events = capture_events ()
674-
675- scope = sentry_sdk .get_current_scope ()
676- scope .set_conversation_id ("conv-gen-ai-op-test" )
677-
678- with sentry_sdk .start_transaction (name = "test-tx" ):
679- with start_span (op = "gen_ai.invoke_agent" ):
680- pass
681-
682- (event ,) = events
683- span_data = event ["spans" ][0 ]["data" ]
684-
685- assert span_data .get ("gen_ai.conversation.id" ) == "conv-gen-ai-op-test"
686-
687- def test_conversation_id_not_propagated_to_non_ai_span (
688- self , sentry_init , capture_events
689- ):
690- """Non-AI span should NOT get conversation_id."""
691- sentry_init (traces_sample_rate = 1.0 )
692- events = capture_events ()
693-
694- scope = sentry_sdk .get_current_scope ()
695- scope .set_conversation_id ("conv-should-not-appear" )
696-
697- with sentry_sdk .start_transaction (name = "test-tx" ):
698- with start_span (op = "http.client" ) as span :
699- span .set_data ("some.other.data" , "value" )
700-
701- (event ,) = events
702- span_data = event ["spans" ][0 ]["data" ]
703- assert "gen_ai.conversation.id" not in span_data
704-
705- def test_conversation_id_not_propagated_when_not_set (
706- self , sentry_init , capture_events
707- ):
708- """AI span should not have conversation_id if not set on scope."""
709- sentry_init (traces_sample_rate = 1.0 )
710- events = capture_events ()
711-
712- # Ensure no conversation_id is set
713- scope = sentry_sdk .get_current_scope ()
714- scope .remove_conversation_id ()
715-
716- with sentry_sdk .start_transaction (name = "test-tx" ):
717- with start_span (op = "ai.chat.completions" ):
718- pass
719-
720- (event ,) = events
721- span_data = event ["spans" ][0 ]["data" ]
722- assert "gen_ai.conversation.id" not in span_data
723-
724- def test_conversation_id_not_propagated_to_span_without_op (
725- self , sentry_init , capture_events
726- ):
727- """Span without op and without gen_ai.operation.name should NOT get conversation_id."""
728- sentry_init (traces_sample_rate = 1.0 )
729- events = capture_events ()
730-
731- scope = sentry_sdk .get_current_scope ()
732- scope .set_conversation_id ("conv-no-op-test" )
733-
734- with sentry_sdk .start_transaction (name = "test-tx" ):
735- with start_span (name = "unnamed-span" ) as span :
736- span .set_data ("regular.data" , "value" )
737-
738- (event ,) = events
739- span_data = event ["spans" ][0 ]["data" ]
740- assert "gen_ai.conversation.id" not in span_data
741-
742- def test_conversation_id_propagates_with_gen_ai_operation_name_no_op (
743- self , sentry_init , capture_events
744- ):
745- """Span with gen_ai.operation.name but no op should still get conversation_id."""
746- sentry_init (traces_sample_rate = 1.0 )
747- events = capture_events ()
748-
749- scope = sentry_sdk .get_current_scope ()
750- scope .set_conversation_id ("conv-no-op-but-data-test" )
751-
752- with sentry_sdk .start_transaction (name = "test-tx" ):
753- with start_span (name = "unnamed-span" ) as span :
754- span .set_data ("gen_ai.operation.name" , "embedding" )
755-
756- (event ,) = events
757- span_data = event ["spans" ][0 ]["data" ]
758- assert span_data .get ("gen_ai.conversation.id" ) == "conv-no-op-but-data-test"
759-
760- def test_conversation_id_propagates_to_transaction_with_ai_op (
761- self , sentry_init , capture_events
762- ):
763- """Transaction with ai.* op should get conversation_id."""
764- sentry_init (traces_sample_rate = 1.0 )
765- events = capture_events ()
766-
767- scope = sentry_sdk .get_current_scope ()
768- scope .set_conversation_id ("conv-tx-ai-op-test" )
769-
770- with sentry_sdk .start_transaction (op = "ai.workflow" , name = "AI Workflow" ):
771- pass
772-
773- (event ,) = events
774- trace_data = event ["contexts" ]["trace" ]["data" ]
775- assert trace_data .get ("gen_ai.conversation.id" ) == "conv-tx-ai-op-test"
776-
777- def test_conversation_id_not_propagated_to_non_ai_transaction (
778- self , sentry_init , capture_events
779- ):
780- """Non-AI transaction should NOT get conversation_id."""
781- sentry_init (traces_sample_rate = 1.0 )
782- events = capture_events ()
783-
784- scope = sentry_sdk .get_current_scope ()
785- scope .set_conversation_id ("conv-tx-should-not-appear" )
786-
787- with sentry_sdk .start_transaction (op = "http.server" , name = "HTTP Request" ):
788- pass
789-
790- (event ,) = events
791- trace_data = event ["contexts" ]["trace" ]["data" ]
792- assert "gen_ai.conversation.id" not in trace_data
0 commit comments