Skip to content

Commit a816abe

Browse files
fix: resolve circular import with lazy initialization of tracing processor
The circular import was caused by immediate instantiation of AgentexTracingProcessorConfig at module load time, which triggered: tracer.py → tracing_processor_manager → agentex_tracing_processor → adk.utils.client → adk.utils → adk.providers → tracer.py Solution: Implement lazy initialization using the double-checked locking pattern. The default Agentex tracing processor is now initialized on first use (when get_sync_processors() or get_async_processors() is called) rather than at module load time. This breaks the circular dependency while maintaining the same functionality and avoiding any need for commented-out code or workarounds. Tests: Verified with test_function_tool.py - all 10 tests pass
1 parent e4b39b5 commit a816abe

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/agentex/lib/core/tracing/tracing_processor_manager.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
from threading import Lock
22

3-
from agentex.lib.core.tracing.processors.agentex_tracing_processor import (
4-
AgentexAsyncTracingProcessor,
5-
AgentexSyncTracingProcessor,
6-
)
3+
from agentex.lib.types.tracing import TracingProcessorConfig, AgentexTracingProcessorConfig
74
from agentex.lib.core.tracing.processors.sgp_tracing_processor import (
8-
SGPAsyncTracingProcessor,
95
SGPSyncTracingProcessor,
6+
SGPAsyncTracingProcessor,
7+
)
8+
from agentex.lib.core.tracing.processors.agentex_tracing_processor import (
9+
AgentexSyncTracingProcessor,
10+
AgentexAsyncTracingProcessor,
1011
)
1112
from agentex.lib.core.tracing.processors.tracing_processor_interface import (
12-
AsyncTracingProcessor,
1313
SyncTracingProcessor,
14+
AsyncTracingProcessor,
1415
)
15-
from agentex.lib.types.tracing import AgentexTracingProcessorConfig, TracingProcessorConfig
1616

1717

1818
class TracingProcessorManager:
@@ -30,6 +30,7 @@ def __init__(self):
3030
self.sync_processors: list[SyncTracingProcessor] = []
3131
self.async_processors: list[AsyncTracingProcessor] = []
3232
self.lock = Lock()
33+
self._initialized = False
3334

3435
def add_processor_config(self, processor_config: TracingProcessorConfig) -> None:
3536
with self.lock:
@@ -43,10 +44,20 @@ def set_processor_configs(self, processor_configs: list[TracingProcessorConfig])
4344
for processor_config in processor_configs:
4445
self.add_processor_config(processor_config)
4546

47+
def _ensure_initialized(self):
48+
"""Lazily initialize the default Agentex processor on first use to avoid circular imports."""
49+
if not self._initialized:
50+
with self.lock:
51+
if not self._initialized: # Double-check inside lock
52+
self.add_processor_config(AgentexTracingProcessorConfig())
53+
self._initialized = True
54+
4655
def get_sync_processors(self) -> list[SyncTracingProcessor]:
56+
self._ensure_initialized()
4757
return self.sync_processors
4858

4959
def get_async_processors(self) -> list[AsyncTracingProcessor]:
60+
self._ensure_initialized()
5061
return self.async_processors
5162

5263

@@ -58,5 +69,5 @@ def get_async_processors(self) -> list[AsyncTracingProcessor]:
5869
get_sync_tracing_processors = GLOBAL_TRACING_PROCESSOR_MANAGER.get_sync_processors
5970
get_async_tracing_processors = GLOBAL_TRACING_PROCESSOR_MANAGER.get_async_processors
6071

61-
# Add the Agentex tracing processor by default
62-
add_tracing_processor_config(AgentexTracingProcessorConfig())
72+
# Note: The default Agentex tracing processor is initialized lazily on first use
73+
# to avoid circular import issues. See _ensure_initialized() method.

0 commit comments

Comments
 (0)