3232from opentelemetry .trace .propagation import set_span_in_context
3333from opentelemetry .trace .status import Status , StatusCode
3434from opentelemetry .util .genai .types import (
35+ AgentCreation ,
3536 Error ,
3637 InputMessage ,
3738 LLMInvocation ,
3839 MessagePart ,
3940 OutputMessage ,
41+ _BaseAgent ,
4042)
4143from opentelemetry .util .genai .utils import (
4244 ContentCapturingMode ,
4749)
4850
4951
52+ def _agent_attr (name : str , fallback : str ) -> str :
53+ """Get a semconv attribute, falling back to a string literal if not yet in the package."""
54+ return getattr (GenAI , name , fallback )
55+
56+
57+ _GEN_AI_AGENT_NAME = _agent_attr ("GEN_AI_AGENT_NAME" , "gen_ai.agent.name" )
58+ _GEN_AI_AGENT_ID = _agent_attr ("GEN_AI_AGENT_ID" , "gen_ai.agent.id" )
59+ _GEN_AI_AGENT_DESCRIPTION = _agent_attr (
60+ "GEN_AI_AGENT_DESCRIPTION" , "gen_ai.agent.description"
61+ )
62+ _GEN_AI_AGENT_VERSION = _agent_attr (
63+ "GEN_AI_AGENT_VERSION" , "gen_ai.agent.version"
64+ )
65+
66+
5067def _get_llm_common_attributes (
5168 invocation : LLMInvocation ,
5269) -> dict [str , Any ]:
@@ -279,6 +296,75 @@ def _get_llm_response_attributes(
279296 return {key : value for key , value in optional_attrs if value is not None }
280297
281298
299+ def _get_base_agent_common_attributes (
300+ agent : _BaseAgent ,
301+ ) -> dict [str , Any ]:
302+ """Get common attributes shared by all agent operations (invoke_agent, create_agent)."""
303+ optional_attrs = (
304+ (GenAI .GEN_AI_REQUEST_MODEL , agent .model ),
305+ (GenAI .GEN_AI_PROVIDER_NAME , agent .provider ),
306+ (_GEN_AI_AGENT_NAME , agent .name ),
307+ (_GEN_AI_AGENT_ID , agent .agent_id ),
308+ (_GEN_AI_AGENT_DESCRIPTION , agent .description ),
309+ (_GEN_AI_AGENT_VERSION , agent .version ),
310+ (server_attributes .SERVER_ADDRESS , agent .server_address ),
311+ (server_attributes .SERVER_PORT , agent .server_port ),
312+ )
313+
314+ return {
315+ GenAI .GEN_AI_OPERATION_NAME : agent .operation_name ,
316+ ** {key : value for key , value in optional_attrs if value is not None },
317+ }
318+
319+
320+ def _get_base_agent_span_name (agent : _BaseAgent ) -> str :
321+ """Get the span name for any agent operation."""
322+ if agent .name :
323+ return f"{ agent .operation_name } { agent .name } "
324+ return agent .operation_name
325+
326+
327+ def _get_creation_common_attributes (
328+ creation : AgentCreation ,
329+ ) -> dict [str , Any ]:
330+ """Get common agent creation attributes."""
331+ return _get_base_agent_common_attributes (creation )
332+
333+
334+ def _get_creation_span_name (creation : AgentCreation ) -> str :
335+ """Get the span name for an agent creation."""
336+ return _get_base_agent_span_name (creation )
337+
338+
339+ def _apply_creation_finish_attributes (
340+ span : Span , creation : AgentCreation
341+ ) -> None :
342+ """Apply attributes common to agent creation finish() paths."""
343+ span .update_name (_get_creation_span_name (creation ))
344+
345+ attributes : dict [str , Any ] = {}
346+ attributes .update (_get_creation_common_attributes (creation ))
347+
348+ # System instructions (Opt-In)
349+ if (
350+ is_experimental_mode ()
351+ and get_content_capturing_mode ()
352+ in (
353+ ContentCapturingMode .SPAN_ONLY ,
354+ ContentCapturingMode .SPAN_AND_EVENT ,
355+ )
356+ and creation .system_instructions
357+ ):
358+ attributes [GenAI .GEN_AI_SYSTEM_INSTRUCTIONS ] = gen_ai_json_dumps (
359+ [asdict (p ) for p in creation .system_instructions ]
360+ )
361+
362+ attributes .update (creation .attributes )
363+
364+ if attributes :
365+ span .set_attributes (attributes )
366+
367+
282368__all__ = [
283369 "_apply_llm_finish_attributes" ,
284370 "_apply_error_attributes" ,
@@ -287,4 +373,13 @@ def _get_llm_response_attributes(
287373 "_get_llm_response_attributes" ,
288374 "_get_llm_span_name" ,
289375 "_maybe_emit_llm_event" ,
376+ "_GEN_AI_AGENT_NAME" ,
377+ "_GEN_AI_AGENT_ID" ,
378+ "_GEN_AI_AGENT_DESCRIPTION" ,
379+ "_GEN_AI_AGENT_VERSION" ,
380+ "_get_base_agent_common_attributes" ,
381+ "_get_base_agent_span_name" ,
382+ "_apply_creation_finish_attributes" ,
383+ "_get_creation_common_attributes" ,
384+ "_get_creation_span_name" ,
290385]
0 commit comments