@@ -17,6 +17,7 @@ class _HasEnqueuedAt(Protocol):
1717
1818
1919_metrics_enabled : bool | None = None
20+ _tracing = None # lazy-loaded tracing_metrics module (loads OTel on first use)
2021
2122
2223def is_metrics_enabled () -> bool :
@@ -28,6 +29,16 @@ def is_metrics_enabled() -> bool:
2829 return _metrics_enabled
2930
3031
32+ def _tracing_module ():
33+ """Return lazy-loaded ``tracing_metrics`` module (loads OTel on first use)."""
34+ global _tracing
35+ if _tracing is None :
36+ from agentex .lib .core .observability import tracing_metrics
37+
38+ _tracing = tracing_metrics
39+ return _tracing
40+
41+
3142def monotonic_if_enabled () -> float | None :
3243 """Return ``time.monotonic()`` when metrics are enabled, else ``None``."""
3344 if not is_metrics_enabled ():
@@ -39,9 +50,9 @@ def record_span_enqueued(event_type: str) -> None:
3950 if not is_metrics_enabled ():
4051 return
4152 try :
42- from agentex . lib . core . observability . tracing_metrics import get_tracing_metrics
43-
44- get_tracing_metrics (). span_events_enqueued . add ( 1 , { "event_type" : event_type } )
53+ _tracing_module (). get_tracing_metrics (). span_events_enqueued . add (
54+ 1 , { "event_type" : event_type }
55+ )
4556 except Exception :
4657 pass
4758
@@ -50,9 +61,7 @@ def record_span_dropped(reason: str) -> None:
5061 if not is_metrics_enabled ():
5162 return
5263 try :
53- from agentex .lib .core .observability .tracing_metrics import get_tracing_metrics
54-
55- get_tracing_metrics ().span_events_dropped .add (1 , {"reason" : reason })
64+ _tracing_module ().get_tracing_metrics ().span_events_dropped .add (1 , {"reason" : reason })
5665 except Exception :
5766 pass
5867
@@ -65,9 +74,7 @@ def record_batch_coalesced(
6574 if not is_metrics_enabled ():
6675 return
6776 try :
68- from agentex .lib .core .observability .tracing_metrics import get_tracing_metrics
69-
70- metrics = get_tracing_metrics ()
77+ metrics = _tracing_module ().get_tracing_metrics ()
7178 metrics .queue_depth .record (max (queue_depth , 0 ))
7279 metrics .batch_items .record (len (batch_items ))
7380
@@ -87,10 +94,8 @@ def record_batch_phase(*, phase: str, size: int, duration_ms: float) -> None:
8794 if not is_metrics_enabled ():
8895 return
8996 try :
90- from agentex .lib .core .observability .tracing_metrics import get_tracing_metrics
91-
9297 attrs = {"phase" : phase }
93- metrics = get_tracing_metrics ()
98+ metrics = _tracing_module (). get_tracing_metrics ()
9499 metrics .batch_size .record (size , attrs )
95100 metrics .batch_drain_duration .record (duration_ms , attrs )
96101 except Exception :
@@ -101,10 +106,8 @@ def record_export_success(*, event_type: str, span_count: int, processor: str =
101106 if not is_metrics_enabled ():
102107 return
103108 try :
104- from agentex .lib .core .observability .tracing_metrics import get_tracing_metrics
105-
106109 attrs = {"processor" : processor , "event_type" : event_type }
107- metrics = get_tracing_metrics ()
110+ metrics = _tracing_module (). get_tracing_metrics ()
108111 metrics .export_batches .add (1 , attrs )
109112 metrics .export_spans .add (span_count , attrs )
110113 except Exception :
@@ -121,21 +124,16 @@ def record_export_failure(
121124 if not is_metrics_enabled ():
122125 return
123126 try :
124- from agentex .lib .core .observability .tracing_metrics import (
125- processor_label ,
126- get_tracing_metrics ,
127- classify_export_error ,
128- )
129-
130- error_class , http_code = classify_export_error (exc )
131- proc = processor_label (processor )
127+ tm = _tracing_module ()
128+ error_class , http_code = tm .classify_export_error (exc )
129+ proc = tm .processor_label (processor )
132130 attrs = {
133131 "processor" : proc ,
134132 "event_type" : event_type ,
135133 "http_code" : http_code ,
136134 "error_class" : error_class ,
137135 }
138- metrics = get_tracing_metrics ()
136+ metrics = tm . get_tracing_metrics ()
139137 metrics .export_batch_failures .add (1 , attrs )
140138 metrics .export_span_failures .add (span_count , attrs )
141139 except Exception :
@@ -146,9 +144,7 @@ def record_shutdown_timeout(*, remaining_items: int) -> None:
146144 if not is_metrics_enabled ():
147145 return
148146 try :
149- from agentex .lib .core .observability .tracing_metrics import get_tracing_metrics
150-
151- metrics = get_tracing_metrics ()
147+ metrics = _tracing_module ().get_tracing_metrics ()
152148 metrics .shutdown_timeouts .add (1 )
153149 metrics .shutdown_remaining_items .record (max (remaining_items , 0 ))
154150 except Exception :
0 commit comments