|
22 | 22 |
|
23 | 23 | from __future__ import annotations |
24 | 24 |
|
| 25 | +import os |
25 | 26 | from collections.abc import Iterator |
26 | 27 | from contextlib import contextmanager |
27 | 28 |
|
28 | 29 | from inference_catalyst_tracing import AgentSpanHandle, agent_span |
29 | 30 | from opentelemetry import trace |
30 | 31 |
|
31 | 32 | _TRACER_NAME = "halo-engine" |
| 33 | +_SESSION_ID_ENV = "CATALYST_TRACING_CONVERSATION_ID" |
| 34 | + |
| 35 | + |
| 36 | +def _session_id_from_env() -> str | None: |
| 37 | + value = os.environ.get(_SESSION_ID_ENV, "").strip() |
| 38 | + if not value: |
| 39 | + return None |
| 40 | + return value |
32 | 41 |
|
33 | 42 |
|
34 | 43 | @contextmanager |
35 | 44 | def halo_agent_span( |
36 | 45 | *, |
37 | | - name: str, |
| 46 | + span_name: str, |
38 | 47 | system: str = "openai", |
39 | 48 | agent_id: str | None = None, |
40 | 49 | ) -> Iterator[AgentSpanHandle]: |
41 | 50 | """Open an OpenInference AGENT span around a chunk of HALO agent work. |
42 | 51 |
|
43 | | - ``name`` becomes the span's ``agent.name`` and the prefix of its |
44 | | - span name (``f"{name}.run"``). ``agent_id`` becomes the span's |
45 | | - ``agent.id`` — Catalyst groups the Agents view on that field with |
46 | | - ``agent.name`` as a fallback, so passing a stable ``agent_id`` (e.g. |
47 | | - ``"halo"`` for both root + subagent) collapses every HALO run under |
48 | | - a single Agents-tab row regardless of which name the span carries. |
49 | | - ``system`` becomes ``gen_ai.system``. |
| 52 | + ``span_name`` is the OTel operation name. ``agent_id`` is the |
| 53 | + stable agent identity Catalyst groups by, so passing ``"halo"`` |
| 54 | + for both root and subagent spans collapses every HALO run under a |
| 55 | + single Agents-tab row. ``system`` becomes ``gen_ai.system``. |
50 | 56 |
|
51 | 57 | Yields the ``AgentSpanHandle`` from catalyst-tracing. When telemetry |
52 | 58 | is off or the local backend is active, the handle wraps a |
53 | 59 | no-op ``NonRecordingSpan`` and all attribute setters silently no-op. |
54 | 60 | """ |
55 | 61 | tracer = trace.get_tracer(_TRACER_NAME) |
56 | | - with agent_span(tracer, name=name, system=system, agent_id=agent_id) as span: |
| 62 | + with agent_span( |
| 63 | + tracer, |
| 64 | + span_name=span_name, |
| 65 | + system=system, |
| 66 | + agent_id=agent_id, |
| 67 | + session_id=_session_id_from_env(), |
| 68 | + ) as span: |
57 | 69 | yield span |
0 commit comments