A Span represents a timed operation within a trace.
Spans form a tree structure through parent-child relationships and contain:
- Timing information (start_time, end_time)
- Hierarchical information (span_id, parent_span_id)
- Metadata (attributes)
- Point-in-time events
- Status (ok, error)
Summary
Functions
Adds an event to the span.
Returns the duration of the span in microseconds. Returns nil if the span has not been finished.
Marks the span as finished by setting its end_time.
Creates a new span with the given name.
Creates a new span with the given name and parent span ID.
Adds or merges attributes into the span.
Sets the capture/persistence posture for the span.
Sets the status of the span.
Types
@type status() :: :ok | :error
@type t() :: %AITrace.Span{ attributes: map(), clock_domain: map(), end_time: integer() | nil, end_wall_time: DateTime.t() | nil, events: [AITrace.Event.t()], name: String.t(), parent_span_id: String.t() | nil, parent_span_id_source: map() | nil, persistence_posture: AITrace.PersistencePosture.t(), span_id: String.t(), span_id_source: map(), start_time: integer(), start_wall_time: DateTime.t(), status: status() }
Functions
@spec add_event(t(), AITrace.Event.t()) :: t()
Adds an event to the span.
Examples
iex> span = AITrace.Span.new("operation")
iex> event = %AITrace.Event{name: "cache_hit", timestamp: System.monotonic_time(:microsecond)}
iex> span = AITrace.Span.add_event(span, event)
iex> length(span.events)
1
Returns the duration of the span in microseconds. Returns nil if the span has not been finished.
Examples
iex> span = AITrace.Span.new("operation") |> AITrace.Span.finish()
iex> is_integer(AITrace.Span.duration(span))
true
Marks the span as finished by setting its end_time.
Examples
iex> span = AITrace.Span.new("operation")
iex> finished = AITrace.Span.finish(span)
iex> is_integer(finished.end_time)
true
@spec generate_id() :: String.t()
Creates a new span with the given name.
Examples
iex> span = AITrace.Span.new("llm_call")
iex> is_binary(span.span_id)
true
iex> span.name
"llm_call"
Creates a new span with the given name and parent span ID.
Examples
iex> span = AITrace.Span.new("child_operation", "parent_123")
iex> span.parent_span_id
"parent_123"
Adds or merges attributes into the span.
Examples
iex> span = AITrace.Span.new("operation")
iex> span = AITrace.Span.with_attributes(span, %{user_id: 42})
iex> span.attributes
%{user_id: 42}
Sets the capture/persistence posture for the span.
Sets the status of the span.
Examples
iex> span = AITrace.Span.new("operation")
iex> span = AITrace.Span.with_status(span, :error)
iex> span.status
:error