Skip to main content
Get traces from watsonx Orchestrate and export their spans by using a Python script.

References

Classes

Use these classes to handle trace data in the watsonx Orchestrate platform.
Import example
A controller that provides programmatic access to trace operations in Watson Orchestrate’s observability platform. Includes methods to search, fetch, and export traces, with optional pagination and CLI‑oriented progress logging.

Methods

get_client(self) -> TracesClient

Return the underlying TracesClient, creating it if necessary.Returns:
TracesClient
An authenticated client bound to the active environment.

fetch_trace_spans(self, trace_id: str, page_size: int = 100, fetch_all: bool = True, show_progress: bool = False) -> SpansResponse

Fetch spans for a specific trace IDParameters:
trace_id
string
A 32‑character hexadecimal trace ID.
page_size
int
Number of spans per page (1–1000). Default is 100.
fetch_all
bool
When True, retrieves spans across all pages. Default is True.
show_progress
bool
When True, logs progress through the logger. Default is False.
Returns:
SpansResponse
Contains spans and paging metadata. Depending on the platform:
  • Legacy: spans_response.spans (list of span objects) with total_count.
  • New (OTLP-style): spans_response.traceData.resourceSpans with scopeSpans[..].spans[..] (dicts).

export_trace_to_json(self, trace_id: str, output_file: Optional[str] = None, pretty: bool = True, page_size: int = 50) -> tuple[SpansResponse, str]

Fetch spans for a trace ID and export the data in JSON format. Uses TraceExporter for serialization and optionally writes to a file.Parameters:
trace_id
string
A 32‑character hexadecimal trace ID.
output_file
string (optional)
A file path for the JSON output. When None, JSON is returned as a string only. Default is None.
pretty
bool
Indented JSON for readability. Default is True.
page_size
int
Number of spans per page. Default is 50.
Returns:
tuple
  • SpansResponse: The spans fetched for programmatic use.
  • string: The JSON output, suitable for CLI display or storage.

search_traces(self, filters: Optional[TraceFilters] = None, sort: Optional[TraceSort] = None, page_size: int = 50, include_root_spans: bool = False, fetch_all: bool = True, show_progress: bool = False) -> TraceSearchResponse

Search traces using optional time filters and additional criteria such as service metadata, IDs, or span count ranges. Supports embedding root‑span data to quickly inspect error‑related attributes.Parameters:
filters
TraceFilters (optional)
Search criteria. Typically includes a time window:
  • start_time (str, RFC 3339 with Z)
  • end_time (str, RFC 3339 with Z) Optional fields:
  • service_names
  • agent_ids
  • agent_names
  • user_ids
  • session_ids
  • SpanCountRange Default is None.
sort
TraceSort (optional)
Sorting options (e.g., field=“start_time”, direction=“desc”). Default is None.
page_size
int
Results per page (1–100). Default is 100.
include_root_spans
bool
If True, embeds root span data in summaries (useful for filtering error traces by root_span.status.code). Default is False.
show_progress
bool
If True, logs progress messages via logger. Default is False.
Returns:
TraceSearchResponse
Contains traceSummaries and pagination details. Field names may vary depending on the model; traceSummaries is the typical structure.

Models

Use these models and the client to work with trace data in the watsonx Orchestrate platform.
Import example
Span context containing trace and span identifier.Attributes
trace_id
string
A 32‑character hexadecimal trace ID.
span_id
string
A 16‑character hexadecimal span ID.
trace_state
string
Optional vendor‑specific trace metadata, such as W3C tracestate‑style key/value entries.
Execution status of the span.Attributes
status_code
string
One of UNSET, OK, ERROR.
message
string
Optional human‑readable context about the status.
Event that occurred during the span.Attributes
name
string
Event name.
timestamp
string
ISO 8601 timestamp in UTC.
attributes
object
Optional event attributes.
OpenTelemetry‑compliant span object.Attributes
name
string
Human-readable operation name.
context
SpanContext
Trace and span identifiers.
parent_id
string
Parent span ID; None for root spans.
kind
string
Span kind such as INTERNAL, SERVER, or CLIENT.
start_time
string
ISO 8601 timestamp in UTC.
end_time
string
ISO 8601 timestamp in UTC.
status
SpanStatus
Execution status.
attributes
object
Arbitrary key-value attributes.
events
array
Events recorded during the span.
Trace data containing resource spans.Attributes
resourceSpans
array
OpenTelemetry‑style structure, such as resourceSpans[].scopeSpans[].spans[*].
Response from the get spans API. Supports two response shapes:
  • Structured: traceData.resourceSpans, an OpenTelemetry‑style grouping
  • Legacy: a flat spans array with pagination metadata
Attributes
traceData
TraceData
Structured trace data.
spans
array
Legacy spans array.
nextCursor
any
Pagination cursor using the internal camelCase field name.
totalCount
int
Reported total number of spans, using the internal camelCase field name.
Range used to filter traces by their span count.
Filters used to search traces.Accepts ISO 8601 strings or Python datetime objects.
A field serializer converts datetime values to strings with a trailing Z.
Attributes
start_time
string|datetime
Start time for filtering.
end_time
string|datetime
End time for filtering.
service_names
array
Service name filter. Supports multiple values and can be specified multiple times.
agent_ids
array
Agent ID filter. Allows selecting multiple agent identifiers and can be repeated as needed.
agent_names
array
Agent name filter. Accepts several agent names and may be provided multiple times.
user_ids
array
User ID filter. Supports filtering by one or more user identifiers, with repeatable entries.
session_ids
array
Session ID filter. Enables filtering by multiple session identifiers and can be included multiple times.
span_count_range
SpanCountRange
Thresholds for minimum and maximum span counts.
Sort configuration for trace searches.
Request body used to search traces.
Status found in the root span, which may differ from the SpanStatus schema.
Root‑span data included in a trace summary.
Summary data for a trace.
Response from the search‑traces API.Attributes
generatedAt
string
Time the response was generated.
originalQuery
object
Echoed search parameters.
traceSummaries
array
Summaries for each trace.
nextCursor
any
Pagination cursor.
totalCount
int
Total number of matching traces.