This repository was archived by the owner on Mar 9, 2026. It is now read-only.
File tree Expand file tree Collapse file tree
tests/unit/pubsub_v1/subscriber Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -200,3 +200,34 @@ def test_opentelemetry_project_id_set_after_create_subscribe_span():
200200 delivery_attempt = 4 ,
201201 )
202202 assert opentelemetry_data .project_id == "projectId"
203+
204+
205+ def test_opentelemetry_context_manager_exits_recording_works_as_expected (span_exporter ):
206+ """Test that __exit__ properly ends process span in normal flow."""
207+ msg = create_message (b"foo" )
208+ opentelemetry_data = SubscribeOpenTelemetry (msg )
209+ msg .opentelemetry_data = opentelemetry_data
210+
211+ # Start the subscribe span
212+ opentelemetry_data .start_subscribe_span (
213+ subscription = "projects/projectId/subscriptions/subscriptionID" ,
214+ exactly_once_enabled = False ,
215+ ack_id = "ack_id" ,
216+ delivery_attempt = 4 ,
217+ )
218+
219+ # Use context manager
220+ with opentelemetry_data as process_span :
221+ assert process_span is not None
222+ # Subscribe span should still be recording
223+ assert opentelemetry_data ._subscribe_span .is_recording ()
224+
225+ # The span has not been completed and is no longer recording
226+ assert not opentelemetry_data ._subscribe_span .is_recording ()
227+
228+ # Verify both spans are finished
229+ spans = span_exporter .get_finished_spans ()
230+ assert len (spans ) == 2
231+ span_names = [span .name for span in spans ]
232+ assert any ("process" in name for name in span_names )
233+ assert any ("subscribe" in name for name in span_names )
You can’t perform that action at this time.
0 commit comments