adk_telemetry/spans.rs
1//! Span helpers for common ADK operations
2//!
3//! Provides pre-configured spans for instrumenting agent, model, and tool operations.
4
5use tracing::Span;
6
7/// Create a span for agent execution
8///
9/// # Arguments
10/// * `agent_name` - Name of the agent being executed
11/// * `invocation_id` - Unique ID for this invocation
12///
13/// # Example
14/// ```
15/// use adk_telemetry::agent_run_span;
16/// let span = agent_run_span("my-agent", "inv-123");
17/// let _enter = span.enter();
18/// // Agent execution code here
19/// ```
20pub fn agent_run_span(agent_name: &str, invocation_id: &str) -> Span {
21 tracing::info_span!(
22 "agent.run",
23 agent.name = agent_name,
24 invocation.id = invocation_id,
25 otel.kind = "internal"
26 )
27}
28
29/// Create a span for model API calls
30///
31/// # Arguments
32/// * `model_name` - Name of the LLM model being called
33///
34/// # Example
35/// ```
36/// use adk_telemetry::model_call_span;
37/// let span = model_call_span("gemini-2.0-flash");
38/// let _enter = span.enter();
39/// // Model call code here
40/// ```
41pub fn model_call_span(model_name: &str) -> Span {
42 tracing::info_span!("model.call", model.name = model_name, otel.kind = "client")
43}
44
45/// Create a span for tool execution
46///
47/// # Arguments
48/// * `tool_name` - Name of the tool being executed
49///
50/// # Example
51/// ```
52/// use adk_telemetry::tool_execute_span;
53/// let span = tool_execute_span("weather_tool");
54/// let _enter = span.enter();
55/// // Tool execution code here
56/// ```
57pub fn tool_execute_span(tool_name: &str) -> Span {
58 tracing::info_span!("tool.execute", tool.name = tool_name, otel.kind = "internal")
59}
60
61/// Create a span for callback execution
62///
63/// # Arguments
64/// * `callback_type` - Type of callback (e.g., "before_model", "after_agent")
65///
66/// # Example
67/// ```
68/// use adk_telemetry::callback_span;
69/// let span = callback_span("before_model");
70/// let _enter = span.enter();
71/// // Callback code here
72/// ```
73pub fn callback_span(callback_type: &str) -> Span {
74 tracing::debug_span!(
75 "callback",
76 callback.type = callback_type,
77 )
78}
79
80/// Add common attributes to the current span
81///
82/// # Arguments
83/// * `user_id` - User ID from context
84/// * `session_id` - Session ID from context
85pub fn add_context_attributes(user_id: &str, session_id: &str) {
86 let span = Span::current();
87 span.record("user.id", user_id);
88 span.record("session.id", session_id);
89}