Skip to content

Commit c813d08

Browse files
committed
Define span type constants for OpenAI Agents instrumentation
1 parent b3cc03a commit c813d08

File tree

2 files changed

+41
-25
lines changed
  • instrumentation-genai/opentelemetry-instrumentation-openai-agents

2 files changed

+41
-25
lines changed

instrumentation-genai/opentelemetry-instrumentation-openai-agents/src/opentelemetry/instrumentation/openai_agents/span_processor.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,26 @@ class TracingProcessor: # type: ignore[misc]
2828
from opentelemetry.trace import Span, SpanKind, Tracer, set_span_in_context
2929
from opentelemetry.trace.status import Status, StatusCode
3030

31+
SPAN_TYPE_GENERATION = "generation"
32+
SPAN_TYPE_RESPONSE = "response"
33+
SPAN_TYPE_AGENT = "agent"
34+
SPAN_TYPE_AGENT_CREATION = "agent_creation"
35+
SPAN_TYPE_FUNCTION = "function"
36+
SPAN_TYPE_SPEECH = "speech"
37+
SPAN_TYPE_TRANSCRIPTION = "transcription"
38+
SPAN_TYPE_SPEECH_GROUP = "speech_group"
39+
SPAN_TYPE_GUARDRAIL = "guardrail"
40+
SPAN_TYPE_HANDOFF = "handoff"
41+
SPAN_TYPE_MCP_TOOLS = "mcp_tools"
42+
3143
_CLIENT_SPAN_TYPES = frozenset(
3244
{
33-
"generation",
34-
"response",
35-
"speech",
36-
"transcription",
37-
"agent",
38-
"agent_creation",
45+
SPAN_TYPE_GENERATION,
46+
SPAN_TYPE_RESPONSE,
47+
SPAN_TYPE_SPEECH,
48+
SPAN_TYPE_TRANSCRIPTION,
49+
SPAN_TYPE_AGENT,
50+
SPAN_TYPE_AGENT_CREATION,
3951
}
4052
)
4153

@@ -160,21 +172,21 @@ def _operation_name(self, span_data: Any) -> str:
160172
if isinstance(explicit_operation, str)
161173
else None
162174
)
163-
if span_type == "generation":
175+
if span_type == SPAN_TYPE_GENERATION:
164176
if _looks_like_chat(getattr(span_data, "input", None)):
165177
return GenAI.GenAiOperationNameValues.CHAT.value
166178
return GenAI.GenAiOperationNameValues.TEXT_COMPLETION.value
167-
if span_type == "agent":
179+
if span_type == SPAN_TYPE_AGENT:
168180
if normalized_operation in {"create", "create_agent"}:
169181
return GenAI.GenAiOperationNameValues.CREATE_AGENT.value
170182
if normalized_operation in {"invoke", "invoke_agent"}:
171183
return GenAI.GenAiOperationNameValues.INVOKE_AGENT.value
172184
return GenAI.GenAiOperationNameValues.INVOKE_AGENT.value
173-
if span_type == "agent_creation":
185+
if span_type == SPAN_TYPE_AGENT_CREATION:
174186
return GenAI.GenAiOperationNameValues.CREATE_AGENT.value
175-
if span_type == "function":
187+
if span_type == SPAN_TYPE_FUNCTION:
176188
return GenAI.GenAiOperationNameValues.EXECUTE_TOOL.value
177-
if span_type == "response":
189+
if span_type == SPAN_TYPE_RESPONSE:
178190
return GenAI.GenAiOperationNameValues.CHAT.value
179191
return span_type or "operation"
180192

@@ -375,29 +387,29 @@ def _attributes_from_generic(self, span_data: Any) -> dict[str, Any]:
375387

376388
def _attributes_for_span(self, span_data: Any) -> dict[str, Any]:
377389
span_type = getattr(span_data, "type", None)
378-
if span_type == "generation":
390+
if span_type == SPAN_TYPE_GENERATION:
379391
return self._attributes_from_generation(span_data)
380-
if span_type == "response":
392+
if span_type == SPAN_TYPE_RESPONSE:
381393
return self._attributes_from_response(span_data)
382-
if span_type == "agent":
394+
if span_type == SPAN_TYPE_AGENT:
383395
operation = getattr(span_data, "operation", None)
384396
if isinstance(operation, str) and operation.strip().lower() in {
385397
"create",
386398
"create_agent",
387399
}:
388400
return self._attributes_from_agent_creation(span_data)
389401
return self._attributes_from_agent(span_data)
390-
if span_type == "agent_creation":
402+
if span_type == SPAN_TYPE_AGENT_CREATION:
391403
return self._attributes_from_agent_creation(span_data)
392-
if span_type == "function":
404+
if span_type == SPAN_TYPE_FUNCTION:
393405
return self._attributes_from_function(span_data)
394406
if span_type in {
395-
"guardrail",
396-
"handoff",
397-
"speech_group",
398-
"speech",
399-
"transcription",
400-
"mcp_tools",
407+
SPAN_TYPE_GUARDRAIL,
408+
SPAN_TYPE_HANDOFF,
409+
SPAN_TYPE_SPEECH_GROUP,
410+
SPAN_TYPE_SPEECH,
411+
SPAN_TYPE_TRANSCRIPTION,
412+
SPAN_TYPE_MCP_TOOLS,
401413
}:
402414
return self._attributes_from_generic(span_data)
403415
return self._base_attributes()

instrumentation-genai/opentelemetry-instrumentation-openai-agents/tests/stubs/agents/tracing/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
from .spans import Span
1010
from .traces import Trace
1111

12+
SPAN_TYPE_AGENT = "agent"
13+
SPAN_TYPE_FUNCTION = "function"
14+
SPAN_TYPE_GENERATION = "generation"
15+
1216
__all__ = [
1317
"TraceProvider",
1418
"get_trace_provider",
@@ -35,7 +39,7 @@ class AgentSpanData:
3539

3640
@property
3741
def type(self) -> str:
38-
return "agent"
42+
return SPAN_TYPE_AGENT
3943

4044

4145
@dataclass
@@ -46,7 +50,7 @@ class FunctionSpanData:
4650

4751
@property
4852
def type(self) -> str:
49-
return "function"
53+
return SPAN_TYPE_FUNCTION
5054

5155

5256
@dataclass
@@ -59,7 +63,7 @@ class GenerationSpanData:
5963

6064
@property
6165
def type(self) -> str:
62-
return "generation"
66+
return SPAN_TYPE_GENERATION
6367

6468

6569
class _ProcessorFanout(TracingProcessor):

0 commit comments

Comments
 (0)