Skip to content

Commit ba45d16

Browse files
committed
Default OpenAI agent trace start to now
1 parent eefc31b commit ba45d16

File tree

1 file changed

+16
-2
lines changed
  • instrumentation-genai/opentelemetry-instrumentation-openai-agents/src/opentelemetry/instrumentation/openai_agents

1 file changed

+16
-2
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from dataclasses import dataclass
55
from datetime import datetime
66
from threading import RLock
7+
from time import time_ns
78
from typing import Any
89
from urllib.parse import urlparse
910

@@ -27,6 +28,15 @@ class TracingProcessor: # type: ignore[misc]
2728
from opentelemetry.trace import Span, SpanKind, Tracer, set_span_in_context
2829
from opentelemetry.trace.status import Status, StatusCode
2930

31+
_CLIENT_SPAN_TYPES = frozenset(
32+
{
33+
"generation",
34+
"response",
35+
"speech",
36+
"transcription",
37+
}
38+
)
39+
3040

3141
def _parse_iso8601(timestamp: str | None) -> int | None:
3242
"""Return nanosecond timestamp for ISO8601 string."""
@@ -143,8 +153,10 @@ def _operation_name(self, span_data: Any) -> str:
143153

144154
def _span_kind(self, span_data: Any) -> SpanKind:
145155
span_type = getattr(span_data, "type", None)
146-
if span_type in {"generation", "response", "speech", "transcription"}:
156+
if span_type in _CLIENT_SPAN_TYPES:
147157
return SpanKind.CLIENT
158+
# Tool invocations (e.g. span type "function") execute inside the agent
159+
# runtime, so there is no remote peer to model; we keep them INTERNAL.
148160
return SpanKind.INTERNAL
149161

150162
def _span_name(self, operation: str, attributes: Mapping[str, Any]) -> str:
@@ -330,7 +342,9 @@ def _attributes_for_span(self, span_data: Any) -> dict[str, Any]:
330342

331343
def on_trace_start(self, trace: AgentsTrace) -> None:
332344
attributes = self._base_attributes()
333-
start_time = _parse_iso8601(getattr(trace, "started_at", None))
345+
start_time = (
346+
_parse_iso8601(getattr(trace, "started_at", None)) or time_ns()
347+
)
334348

335349
with self._lock:
336350
span = self._tracer.start_span(

0 commit comments

Comments
 (0)