README

#OpenTelemetry Trace API

This package is the public trace API. It gives instrumentation code the types needed to describe work as spans without forcing a concrete SDK or exporter on the final application.

A trace is a tree of spans. A span represents one operation: a request handler, database query, cache lookup, queue publish, background job, or any other unit of work worth timing and correlating. Spans carry attributes, events, links, status, and a span context that can be propagated to downstream services.

#Main Types

  • TracerProvider: entry point that creates tracers
  • Tracer: creates spans for one instrumentation scope
  • Span: mutable handle for one in-flight operation
  • SpanBuilder: immutable descriptor for creating a span with kind, start time, attributes, events, and links
  • Event: named timestamped occurrence during a span
  • Link: relationship to another span context, often from queues or fan-in workflows
  • SpanKind, Status, StatusCode: API trace metadata

#Getting A Tracer

Library code usually gets a tracer from the root package or interface/global. Use a stable instrumentation name, typically the library or component name.

///|
fn _trace_readme_get_tracer() -> Tracer {
TracerProvider::noop().tracer("moonbit-community/example-library")
}

Applications register SDK providers through sdk.set_tracer_provider(provider); the SDK facade updates interface/global for API callers.

#Creating Spans

Use start(name) for a simple root span, start_with_context(name, context) when you already have an inbound parent context, or build_with_context() when you need to set span kind, explicit start time, initial attributes, events, or links before the span starts.

///|
async fn _trace_readme_span_lifecycle() -> Unit {
let tracer = TracerProvider::noop().tracer("example")
let builder = tracer
.span_builder("http.request")
.with_kind(Server)
.with_attributes([
@common.KeyValue::new("http.request.method", String("GET")),
])
let span = tracer.build(builder)

span.add_event("handler.start")
span.set_status(Status::ok())
span.end()
}

Always end spans you start. end() is async because real SDK processors may export, queue, or flush data.

#Parent Context And Propagation

Span::context() returns a Context carrying the span context as the active local span. Pass this context to child operations and to propagators for outgoing requests.

No-op spans still preserve an incoming valid parent span context. This allows libraries to propagate trace identity even when the final application has not enabled recording.

  • Attributes describe span dimensions such as route, peer, queue, or database system. Prefer low-cardinality values.
  • Events describe point-in-time facts inside a span, such as retries or exceptions.
  • Links connect a span to another span context without making it the parent. They are useful for batch processing, queues, or fan-in/fan-out workflows.
  • Status communicates final outcome. record_error(message) adds an exception event but intentionally does not set error status for you.

#Builder Reference

  • Event::new(name, timestamp?, attributes?, dropped_attributes_count?) creates one event. The timestamp defaults to current Unix time in nanoseconds.
  • Link::new(span_context, attributes?, dropped_attributes_count?) creates one link. Invalid linked span contexts are ignored when a span is built.
  • SpanBuilder::from_name(name) starts a builder.
  • with_kind(kind) sets client/server/producer/consumer/internal kind.
  • with_start_time(ts) sets an explicit start timestamp.
  • with_attributes(attributes) replaces initial attributes.
  • with_events(events) replaces initial events.
  • with_links(links) replaces initial links.

#Provider Reference

  • TracerProvider::noop() creates non-recording spans.
  • tracer(name) creates a tracer for one instrumentation name.
  • tracer_with_scope(scope) creates a tracer from a full instrumentation scope.

#Span Reference

  • span_context() returns the context that should be propagated downstream.
  • context() returns a Context carrying this span context.
  • is_recording() reports whether mutations are being recorded.
  • has_ended() reports whether the span has ended.
  • add_event(name, attributes?) and add_event_with_timestamp(name, ts,attributes?) add events.
  • set_attribute(attribute) and set_attributes(attributes) update attributes.
  • set_status(status), update_name(new_name), and add_link(span_context,attributes?) mutate span metadata.
  • end() and end_with_timestamp(ts) finish the span.

#No-Op Behavior

When backed by TracerProvider::noop(), spans are non-recording:

  • is_recording() is false
  • mutating methods do not export or store telemetry
  • a valid incoming parent span context is still propagated
  • ending the span only marks the local no-op handle as ended

This makes unconditional library instrumentation safe, while keeping the final application in control of whether telemetry is collected.

#
Event

pub struct Event {
name : String
timestamp_unix_nano : Int64
attributes : Array[
KeyValue
]
dropped_attributes_count : Int
} derive(Eq, ToJson,
Debug
)

Event attached to a span builder.

Events represent timestamped facts that happened during a span, such as a retry, cache miss, or exception. Use attributes for event-specific detail. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#add-events

#
Event::new

fn Event::new(name : StringView, timestamp_unix_nano? : Int64, attributes? : ArrayView[
KeyValue
], dropped_attributes_count? : Int) -> Event

Creates one span event.

The timestamp defaults to the current Unix time in nanoseconds. Events added to a no-op span are ignored. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#add-events

Link attached to a span builder.

Links connect this span to another span context without making that span the parent. They are useful for queues, batch jobs, and fan-in/fan-out workflows. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#link

#
Link::new

Creates one span link.

Invalid linked span contexts are ignored later when the span is built. Links do not affect parent/child relationships. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#link

#
Span

pub struct Span {
span_context_fn : () ->
SpanContext

context_fn : () ->
Context

is_recording_fn : () -> Bool
has_ended_fn : () -> Bool
add_event_fn : (StringView, Int64, ArrayView[
KeyValue
]) -> Unit
set_attribute_fn : (
KeyValue
) -> Unit
set_status_fn : (Status) -> Unit
update_name_fn : (StringView) -> Unit
add_link_fn : (
SpanContext
, ArrayView[
KeyValue
]) -> Unit
end_fn : async (Int64) -> Unit
}

Public span handle.

A span represents one in-flight operation. When created from a no-op tracer, the span still propagates a valid parent context but does not record data. End spans once the represented operation is complete. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span

#
Span::add_event

fn Span::add_event(self : Span, name : StringView, attributes? : ArrayView[
KeyValue
]) -> Unit

Adds one event with the current timestamp. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#add-events

#
Span::add_event_with_timestamp

fn Span::add_event_with_timestamp(self : Span, name : StringView, timestamp_unix_nano : Int64, attributes? : ArrayView[
KeyValue
]) -> Unit

Adds one event with an explicit timestamp. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#add-events

Adds one link to another span context. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#add-link

#
Span::context

Returns the context that carries this span as the active span. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#get-context

#
Span::end

async fn Span::end(self : Span) -> Unit

Ends the span with the current timestamp.

Ending an SDK span may notify processors and exporters. Ending a no-op span only updates local state. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#end

#
Span::end_with_timestamp

async fn Span::end_with_timestamp(self : Span, timestamp_unix_nano : Int64) -> Unit

Ends the span with an explicit timestamp.

Ending a no-op span only flips its local ended flag. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#end

#
Span::from_functions

fn Span::from_functions(span_context_fn : () ->
SpanContext
, context_fn : () ->
Context
, is_recording_fn : () -> Bool, has_ended_fn : () -> Bool, add_event_fn : (StringView, Int64, ArrayView[
KeyValue
]) -> Unit, set_attribute_fn : (
KeyValue
) -> Unit, set_status_fn : (Status) -> Unit, update_name_fn : (StringView) -> Unit, add_link_fn : (
SpanContext
, ArrayView[
KeyValue
]) -> Unit, end_fn : async (Int64) -> Unit) -> Span

Builds a span handle from callbacks.

SDK implementations use this to erase their concrete span type into the public API without making the API package depend on the SDK. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span

#
Span::has_ended

fn Span::has_ended(self : Span) -> Bool

Returns whether the span has already ended. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#end

#
Span::is_recording

fn Span::is_recording(self : Span) -> Bool

Returns whether the span is currently recording telemetry.

No-op spans and sampled-out SDK spans report false. Use this to avoid constructing expensive attributes or events. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#isrecording

#
Span::record_error

fn Span::record_error(self : Span, message : StringView) -> Unit

Records an "exception" event carrying exception.message.

This helper does not set the span status automatically. Call set_status(Status::error(...)) if the operation should be marked failed. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#record-exception

#
Span::set_attribute

Sets or replaces one span attribute. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#set-attributes

#
Span::set_attributes

Sets or replaces multiple span attributes. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#set-attributes

#
Span::set_status

fn Span::set_status(self : Span, status : Status) -> Unit

Sets the span status. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#set-status

#
Span::span_context

Returns the span context that should be propagated downstream.

For no-op spans this may be the valid incoming parent context rather than a newly recorded span context. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#get-context

#
Span::update_name

fn Span::update_name(self : Span, new_name : StringView) -> Unit

Replaces the span name. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#updatename

#
SpanBuilder

pub struct SpanBuilder {
span_kind : SpanKind?
name : String
start_time_unix_nano : Int64?
attributes : Array[
KeyValue
]
events : Array[Event]
links : Array[Link]
} derive(Eq, ToJson,
Debug
)

Immutable span-construction descriptor used by Tracer::build*().

Use a builder when a span needs non-default kind, explicit start time, initial attributes, events, or links. For simple spans, Tracer::start() is shorter. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span-creation

#
SpanBuilder::from_name

fn SpanBuilder::from_name(name : StringView) -> SpanBuilder

Starts building a span with the given operation name.

Names should be low-cardinality and describe the operation shape, such as http.request, db.query, or queue.publish, not a user-specific value. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span-creation

#
SpanBuilder::with_attributes

Returns a copy of the builder with the given attribute list.

This replaces previously stored builder attributes. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span-creation

#
SpanBuilder::with_events

fn SpanBuilder::with_events(self : SpanBuilder, events : ArrayView[Event]) -> SpanBuilder

Returns a copy of the builder with the given event list.

This replaces previously stored builder events. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span-creation

#
SpanBuilder::with_kind

fn SpanBuilder::with_kind(self : SpanBuilder, span_kind : SpanKind) -> SpanBuilder

Returns a copy of the builder with the given span kind. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span-creation
fn SpanBuilder::with_links(self : SpanBuilder, links : ArrayView[Link]) -> SpanBuilder

Returns a copy of the builder with the given link list.

This replaces previously stored builder links. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#specifying-links

#
SpanBuilder::with_start_time

fn SpanBuilder::with_start_time(self : SpanBuilder, start_time_unix_nano : Int64) -> SpanBuilder

Returns a copy of the builder with an explicit start timestamp. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span-creation

#
SpanKind

pub(all) enum SpanKind {
Internal
Client
Server
Producer
Consumer
} derive(Compare, Eq, Hash, ToJson,
Debug
)

OpenTelemetry span kind.

Use Server for inbound request handlers, Client for outbound requests, Producer/Consumer for messaging, and Internal for local work. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#spankind

#
Status

pub struct Status {
code : StatusCode
description : String?
} derive(Eq, ToJson,
Debug
)

Span status with a code and optional description.

Status represents the final outcome of the operation. Recording an exception event does not automatically set status; call Span::set_status() when the operation should be marked as an error. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#set-status
impl Default for Status

#
Status::error

fn Status::error(description? : String?) -> Status

Returns an error status with an optional description. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#set-status

#
Status::new

fn Status::new(code : StatusCode, description? : String?) -> Status

Creates a span status. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#set-status

#
Status::ok

fn Status::ok() -> Status

Returns an ok status. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#set-status

#
Status::unset

fn Status::unset() -> Status

Returns the default unset status. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#set-status

#
StatusCode

pub(all) enum StatusCode {
Unset
Ok
Error
} derive(Compare, Eq, Hash, ToJson,
Debug
)

Span status code: unset, ok, or error. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#set-status

#
Tracer

Public tracer handle.

A tracer creates spans for one instrumentation scope. Tracers are cheap handles; libraries normally keep or reacquire one by instrumentation name. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#tracer

#
Tracer::build

fn Tracer::build(self : Tracer, builder : SpanBuilder) -> Span

Builds a span from a builder with an empty parent context. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span-creation

#
Tracer::build_with_context

fn Tracer::build_with_context(self : Tracer, builder : SpanBuilder, parent_context :
Context
) -> Span

Builds a span from a builder and parent context.

No-op tracers still return a span handle whose context propagates the parent span context. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span-creation

#
Tracer::from_functions

Builds a tracer from a span-building callback. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#tracer

#
Tracer::in_span

async fn[T] Tracer::in_span(self : Tracer, name : StringView, f : async (
Context
) -> T) -> T

Runs f inside a root span and returns its result.

The span is ended after f returns successfully. If f can fail, record error status or events before returning from the callback. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span-creation

#
Tracer::in_span_with_builder

Runs f inside a span built from builder.

The returned child context carries the new span context. The span is ended after f returns successfully. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span-creation

#
Tracer::in_span_with_context

async fn[T] Tracer::in_span_with_context(self : Tracer, name : StringView, parent_context :
Context
, f : async (
Context
) -> T) -> T

Runs f inside a span with an explicit parent context.

The span is ended after f returns successfully. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span-creation

#
Tracer::span_builder

fn Tracer::span_builder(self : Tracer, name : StringView) -> SpanBuilder

Returns a fresh span builder for name.

The returned builder is independent of the tracer until passed to build() or build_with_context(). Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span-creation

#
Tracer::start

fn Tracer::start(self : Tracer, name : StringView) -> Span

Starts a root span with the given name.

Use start_with_context() when handling an inbound request with an extracted parent context. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span-creation

#
Tracer::start_with_context

fn Tracer::start_with_context(self : Tracer, name : StringView, parent_context :
Context
) -> Span

Starts a span with an explicit parent context.

This is the usual entry point after extracting inbound propagation headers or when passing a parent context through asynchronous work. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#span-creation

#
TracerProvider

Public tracer provider.

The default provider is no-op. Applications install SDK-backed providers through the SDK facade while library code depends only on this API type. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#tracerprovider

#
TracerProvider::from_functions

Builds a tracer provider from a scope-to-tracer callback. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#tracerprovider

#
TracerProvider::noop

Returns a no-op tracer provider.

Tracers from this provider create non-recording spans. Valid parent context is still propagated so downstream services can continue an existing trace. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#behavior-of-the-api-in-the-absence-of-an-installed-sdk

#
TracerProvider::tracer

fn TracerProvider::tracer(self : TracerProvider, name : StringView) -> Tracer

Returns a tracer for one instrumentation name.

If the provider is no-op, the returned tracer creates non-recording spans. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#get-a-tracer

#
TracerProvider::tracer_with_scope

Returns a tracer for a fully constructed instrumentation scope. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#get-a-tracer

Source Files