Skip to content

Commit 549d726

Browse files
NiteshDhanpalclaude
andcommitted
refactor(tracing): probe expected backend first in warn_on_backend_drift; rewrap docstring
- warn_on_backend_drift probes the expected backend first and returns early when it is live, so the healthy common path skips the second probe -- and avoids re-attempting the import of a backend that isn't installed (failed imports aren't cached in sys.modules, so the finder cost otherwise recurs every span). Behavior is unchanged. - Rewrap the tag_ambient_obs_span docstring paragraph a prior edit left with one overlong line. Addresses review nits. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 35b7701 commit 549d726

2 files changed

Lines changed: 24 additions & 23 deletions

File tree

‎src/agentex/lib/core/tracing/obs_ids.py‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -119,29 +119,29 @@ def warn_on_backend_drift(expect_otel: bool = False) -> None:
119119
120120
Not a hard failure: obs stays fail-open (the caller still reads and falls back,
121121
so no correlation is lost). The warning is deduped per direction, so a standing
122-
mismatch logs once, not once per span. Never raises."""
122+
mismatch logs once, not once per span. Probes the expected backend first and
123+
returns early when it is live, so the healthy common path never touches the
124+
other backend. Never raises."""
123125
try:
124-
otel = _lgtm_ids()
125-
ddt = _ddtrace_ids()
126126
if expect_otel or get_obs_mode() == LGTM:
127-
expected, expected_live = "otel", otel
128-
other_live = ddt
127+
expected, expected_probe, other_probe, actual = "otel", _lgtm_ids, _ddtrace_ids, "ddtrace"
129128
else:
130-
expected, expected_live = "ddtrace", ddt
131-
other_live = otel
132-
if expected_live is None and other_live is not None:
133-
actual = "ddtrace" if expected == "otel" else "otel"
134-
if (expected, actual) not in _WARNED_DRIFT:
135-
_WARNED_DRIFT.add((expected, actual))
136-
_log.warning(
137-
"obs backend drift: expected %s here (SGP_OBS_MODE=%s%s) but the "
138-
"active span is %s; correlating against %s. Check SGP_OBS_MODE and "
139-
"the running instrumentation.",
140-
expected,
141-
get_obs_mode(),
142-
", temporal path" if expect_otel else "",
143-
actual,
144-
actual,
145-
)
129+
expected, expected_probe, other_probe, actual = "ddtrace", _ddtrace_ids, _lgtm_ids, "otel"
130+
if expected_probe() is not None:
131+
return # expected backend is live -> healthy; skip the other probe
132+
if other_probe() is None:
133+
return # nothing live at all -> uninstrumented path, not drift
134+
if (expected, actual) not in _WARNED_DRIFT:
135+
_WARNED_DRIFT.add((expected, actual))
136+
_log.warning(
137+
"obs backend drift: expected %s here (SGP_OBS_MODE=%s%s) but the "
138+
"active span is %s; correlating against %s. Check SGP_OBS_MODE and "
139+
"the running instrumentation.",
140+
expected,
141+
get_obs_mode(),
142+
", temporal path" if expect_otel else "",
143+
actual,
144+
actual,
145+
)
146146
except Exception: # obs must never fail an app call
147147
pass

‎src/agentex/lib/core/tracing/obs_span.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,9 @@ def tag_ambient_obs_span(
259259
Used inside the SDK's dispatched start-span/end-span activities (see
260260
``trace._in_tracing_dispatch_activity``): there we must NOT open our own
261261
wrapper span, because start_span/end_span run as separate activities on
262-
possibly different workers and the wrapper could never be closed. Instead we lean on the span the Temporal OTel ``TracingInterceptor``
263-
already made active for this activity and just add
262+
possibly different workers and the wrapper could never be closed. Instead we
263+
lean on the span the Temporal OTel ``TracingInterceptor`` already made active
264+
for this activity and just add
264265
``agentex.business_span_id`` / ``agentex.business_trace_id`` so the obs -> business
265266
pivot still works. Best-effort; never raises.
266267

0 commit comments

Comments
 (0)