@@ -67,20 +67,32 @@ def _get_caller_component() -> str:
6767
6868
6969def _inject_trace_context (headers : dict [str , str ]) -> None :
70- """Inject UiPath trace context header from the active OTEL span .
70+ """Inject UiPath trace context header.
7171
72- Prefers the LLMOps tool span (from ContextVar) over the raw OTEL span,
73- so the header carries the span ID visible in the LLMOps trace.
72+ Trace ID: uses the agent trace ID from UIPATH_TRACE_ID env var (same
73+ remapping the LLMOps exporter applies), falling back to the OTEL trace ID.
74+ Span ID: uses the LLMOps tool span (via external span provider) so the
75+ span ID matches what's visible in the LLMOps trace UI.
7476 """
7577 from uipath .core .tracing .span_utils import UiPathSpanUtils
7678
79+ from ._config import UiPathConfig
80+ from ._span_utils import _SpanUtils
81+
7782 llmops_span = UiPathSpanUtils .get_external_current_span ()
7883 span = llmops_span or trace .get_current_span ()
7984 ctx = span .get_span_context ()
80- if ctx .trace_id and ctx .span_id :
81- headers [_TRACE_PARENT_HEADER ] = (
82- f"00-{ format_trace_id (ctx .trace_id )} -{ format_span_id (ctx .span_id )} -01"
83- )
85+ if not (ctx .trace_id and ctx .span_id ):
86+ return
87+
88+ config_trace_id = UiPathConfig .trace_id
89+ trace_id = (
90+ _SpanUtils .normalize_trace_id (config_trace_id )
91+ if config_trace_id
92+ else format_trace_id (ctx .trace_id )
93+ )
94+ span_id = format_span_id (ctx .span_id )
95+ headers [_TRACE_PARENT_HEADER ] = f"00-{ trace_id } -{ span_id } -01"
8496
8597
8698class BaseService :
0 commit comments