@@ -54,10 +54,17 @@ class TestOTelBase:
5454 @pytest .fixture (scope = "function" , autouse = True )
5555 def cleanup_otel (self ):
5656 """Reset OpenTelemetry state between tests."""
57- original_provider = trace_api .get_tracer_provider ()
57+ from opentelemetry .util ._once import Once
58+
59+ trace_api ._TRACER_PROVIDER = None
60+ trace_api ._PROXY_TRACER_PROVIDER = trace_api .ProxyTracerProvider ()
61+ trace_api ._TRACER_PROVIDER_SET_ONCE = Once ()
62+
5863 yield
59- trace_api .set_tracer_provider (original_provider )
6064 LangfuseResourceManager .reset ()
65+ trace_api ._TRACER_PROVIDER = None
66+ trace_api ._PROXY_TRACER_PROVIDER = trace_api .ProxyTracerProvider ()
67+ trace_api ._TRACER_PROVIDER_SET_ONCE = Once ()
6168
6269 @pytest .fixture
6370 def memory_exporter (self ):
@@ -97,7 +104,7 @@ def mock_init(self, **kwargs):
97104 self ,
98105 span_exporter = memory_exporter ,
99106 max_export_batch_size = 512 ,
100- schedule_delay_millis = 5000 ,
107+ schedule_delay_millis = 1 ,
101108 )
102109
103110 monkeypatch .setattr (
@@ -1870,7 +1877,7 @@ def update_random_metadata(thread_id):
18701877 update = random .choice (updates )
18711878
18721879 # Sleep a tiny bit to simulate work and increase chances of thread interleaving
1873- time .sleep (random .uniform (0.001 , 0.01 ))
1880+ time .sleep (random .uniform (0.0005 , 0.001 ))
18741881
18751882 # Apply the update to current_metadata (in a real system, this would update OTEL span)
18761883 with metadata_lock :
@@ -2001,7 +2008,7 @@ def mock_processor_init(self, **kwargs):
20012008 self ,
20022009 span_exporter = exporter ,
20032010 max_export_batch_size = 512 ,
2004- schedule_delay_millis = 5000 ,
2011+ schedule_delay_millis = 1 ,
20052012 )
20062013
20072014 monkeypatch .setattr (
@@ -2118,7 +2125,7 @@ def create_spans_project1():
21182125 metadata = {"project" : "project1" , "index" : i },
21192126 )
21202127 # Small sleep to ensure overlap with other thread
2121- time .sleep (0.01 )
2128+ time .sleep (0.001 )
21222129 span .end ()
21232130
21242131 def create_spans_project2 ():
@@ -2128,7 +2135,7 @@ def create_spans_project2():
21282135 metadata = {"project" : "project2" , "index" : i },
21292136 )
21302137 # Small sleep to ensure overlap with other thread
2131- time .sleep (0.01 )
2138+ time .sleep (0.001 )
21322139 span .end ()
21332140
21342141 # Start threads
@@ -2378,7 +2385,7 @@ def mock_processor_init(self, **kwargs):
23782385 self ,
23792386 span_exporter = exporter ,
23802387 max_export_batch_size = 512 ,
2381- schedule_delay_millis = 5000 ,
2388+ schedule_delay_millis = 1 ,
23822389 )
23832390
23842391 monkeypatch .setattr (
@@ -2757,7 +2764,7 @@ async def async_task(parent_span, task_id):
27572764 child_span = parent_span .start_observation (name = f"async-task-{ task_id } " )
27582765
27592766 # Simulate async work
2760- await asyncio .sleep (0.1 )
2767+ await asyncio .sleep (0.01 )
27612768
27622769 # Update span with results
27632770 child_span .update (
@@ -2948,7 +2955,7 @@ async def test_span_metadata_updates_in_async_context(
29482955
29492956 # Define async tasks that update different parts of metadata
29502957 async def update_temperature ():
2951- await asyncio .sleep (0.1 ) # Simulate some async work
2958+ await asyncio .sleep (0.01 ) # Simulate some async work
29522959 main_span .update (
29532960 metadata = {
29542961 "llm_config" : {
@@ -2960,7 +2967,7 @@ async def update_temperature():
29602967 )
29612968
29622969 async def update_model ():
2963- await asyncio .sleep (0.05 ) # Simulate some async work
2970+ await asyncio .sleep (0.005 ) # Simulate some async work
29642971 main_span .update (
29652972 metadata = {
29662973 "llm_config" : {
@@ -2970,7 +2977,7 @@ async def update_model():
29702977 )
29712978
29722979 async def add_context_length ():
2973- await asyncio .sleep (0.15 ) # Simulate some async work
2980+ await asyncio .sleep (0.015 ) # Simulate some async work
29742981 main_span .update (
29752982 metadata = {
29762983 "llm_config" : {
@@ -2982,7 +2989,7 @@ async def add_context_length():
29822989 )
29832990
29842991 async def update_user_id ():
2985- await asyncio .sleep (0.08 ) # Simulate some async work
2992+ await asyncio .sleep (0.008 ) # Simulate some async work
29862993 main_span .update (
29872994 metadata = {
29882995 "request_info" : {
@@ -3047,7 +3054,7 @@ def test_metrics_and_timing(self, langfuse_client, memory_exporter):
30473054 span = langfuse_client .start_observation (name = "timing-test-span" )
30483055
30493056 # Add a small delay
3050- time .sleep (0.1 )
3057+ time .sleep (0.01 )
30513058
30523059 # End the span
30533060 span .end ()
@@ -3089,10 +3096,10 @@ def test_metrics_and_timing(self, langfuse_client, memory_exporter):
30893096 ) / 1_000_000_000
30903097 assert span_duration_seconds > 0 , "Span duration should be positive"
30913098
3092- # Since we slept for 0.1 seconds, the span duration should be at least 0.05 seconds
3099+ # Since we slept for 0.01 seconds, the span duration should be at least 0.005 seconds
30933100 # but we'll be generous with the upper bound due to potential system delays
3094- assert span_duration_seconds >= 0.05 , (
3095- f"Span duration ({ span_duration_seconds } s) should be at least 0.05s "
3101+ assert span_duration_seconds >= 0.005 , (
3102+ f"Span duration ({ span_duration_seconds } s) should be at least 0.005s "
30963103 )
30973104
30983105
@@ -3349,6 +3356,7 @@ def langfuse_client(self, monkeypatch):
33493356 public_key = "test-public-key" ,
33503357 secret_key = "test-secret-key" ,
33513358 base_url = "http://test-host" ,
3359+ tracing_enabled = False ,
33523360 )
33533361
33543362 return client
0 commit comments