Start a root span for an agent ``run()`` call. Sets canonical attributes (runner name, GenAI system/model, question length, and any ambient ``agent.run_id`` / ``agent.scenario_id`` from :func:`set_run_context`) and records exceptions on the span before re-raising.
(
runner_name: str,
model: str,
question: str,
)
| 47 | |
| 48 | @contextmanager |
| 49 | def agent_run_span( |
| 50 | runner_name: str, |
| 51 | model: str, |
| 52 | question: str, |
| 53 | ) -> Iterator[Any]: |
| 54 | """Start a root span for an agent ``run()`` call. |
| 55 | |
| 56 | Sets canonical attributes (runner name, GenAI system/model, question |
| 57 | length, and any ambient ``agent.run_id`` / ``agent.scenario_id`` from |
| 58 | :func:`set_run_context`) and records exceptions on the span before |
| 59 | re-raising. |
| 60 | """ |
| 61 | tracer = get_tracer() |
| 62 | run_id = _run_id_var.get() |
| 63 | scenario_id = _scenario_id_var.get() |
| 64 | with tracer.start_as_current_span(f"agent.run {runner_name}") as span: |
| 65 | span.set_attribute("agent.runner", runner_name) |
| 66 | span.set_attribute("gen_ai.system", _system_from_model(model)) |
| 67 | span.set_attribute("gen_ai.request.model", model) |
| 68 | span.set_attribute("agent.question.length", len(question)) |
| 69 | if run_id: |
| 70 | span.set_attribute("agent.run_id", run_id) |
| 71 | if scenario_id: |
| 72 | span.set_attribute("agent.scenario_id", scenario_id) |
| 73 | try: |
| 74 | yield span |
| 75 | except Exception as exc: |
| 76 | span.record_exception(exc) |
| 77 | span.set_status(Status(StatusCode.ERROR, str(exc))) |
| 78 | raise |