logo
pub trait Span {
    fn add_event_with_timestamp<T>(
        &mut self,
        name: T,
        timestamp: SystemTime,
        attributes: Vec<KeyValue, Global>
    )
   where
        T: Into<Cow<'static, str>>
; fn span_context(&self) -> &SpanContext; fn is_recording(&self) -> bool; fn set_attribute(&mut self, attribute: KeyValue); fn set_status(&mut self, status: Status); fn update_name<T>(&mut self, new_name: T)
   where
        T: Into<Cow<'static, str>>
; fn end_with_timestamp(&mut self, timestamp: SystemTime); fn add_event<T>(&mut self, name: T, attributes: Vec<KeyValue, Global>)
   where
        T: Into<Cow<'static, str>>
, { ... } fn record_error(&mut self, err: &dyn Error) { ... } fn set_attributes(&mut self, attributes: impl IntoIterator<Item = KeyValue>) { ... } fn end(&mut self) { ... } }
Available on crate feature trace only.
Expand description

The interface for a single operation within a trace.

Spans can be nested to form a trace tree. Each trace contains a root span, which typically describes the entire operation and, optionally, one or more sub-spans for its sub-operations.

The span name concisely identifies the work represented by the span, for example, an RPC method name, a function name, or the name of a subtask or stage within a larger computation. The span name should be the most general string that identifies a (statistically) interesting class of spans, rather than individual span instances while still being human-readable. That is, "get_user" is a reasonable name, while "get_user/314159", where "314159" is a user ID, is not a good name due to its high cardinality. Generality should be prioritized over human-readability.

For example, here are potential span names for an endpoint that gets a hypothetical account information:

Span NameGuidance
getToo general
get_account/42Too specific
get_accountGood, and account_id=42 would make a nice Span attribute
get_account/{accountId}Also good (using the “HTTP route”)

The span’s start and end timestamps reflect the elapsed real time of the operation.

For example, if a span represents a request-response cycle (e.g. HTTP or an RPC), the span should have a start time that corresponds to the start time of the first sub-operation, and an end time of when the final sub-operation is complete. This includes:

  • receiving the data from the request
  • parsing of the data (e.g. from a binary or json format)
  • any middleware or additional processing logic
  • business logic
  • construction of the response
  • sending of the response

Child spans (or in some cases events) may be created to represent sub-operations which require more detailed observability. Child spans should measure the timing of the respective sub-operation, and may add additional attributes.

Required Methods

Record an event with a timestamp in the context this span.

Note that the OpenTelemetry project documents certain “standard attributes” that have prescribed semantic meanings and are available via the opentelemetry_semantic_conventions crate.

A reference to the SpanContext for this span.

Returns true if this span is recording information.

Spans will not be recording information after they have ended.

This flag may be true despite the entire trace being sampled out. This allows recording and processing of information about the individual spans without sending it to the backend. An example of this scenario may be recording and processing of all incoming requests for the processing and building of SLA/SLO latency charts while sending only a subset - sampled spans - to the backend.

Set an attribute of this span.

Setting an attribute with the same key as an existing attribute generally overwrites the existing attribute’s value.

Note that the OpenTelemetry project documents certain “standard attributes” that have prescribed semantic meanings and are available via the opentelemetry_semantic_conventions crate.

Sets the status of this Span.

If used, this will override the default span status, which is Status::Unset.

Updates the span’s name.

After this update, any sampling behavior based on the name will depend on the implementation.

Signals that the operation described by this span ended at the given time.

Provided Methods

Record an event in the context this span.

Note that the OpenTelemetry project documents certain “standard attributes” that have prescribed semantic meanings and are available via the opentelemetry_semantic_conventions crate.

Record an error as an event for this span.

An additional call to Span::set_status is required if the status of the span should be set to error, as this method does not change the span status.

If this span is not being recorded then this method does nothing.

Set multiple attributes of this span.

Setting an attribute with the same key as an existing attribute generally overwrites the existing attribute’s value.

Note that the OpenTelemetry project documents certain “standard attributes” that have prescribed semantic meanings and are available via the opentelemetry_semantic_conventions crate.

Signals that the operation described by this span has now ended.

Implementors