Skip to main content

Overview

Tracing allows you to track the flow of requests through your application. This guide shows you how to set up basic tracing with the OpenTelemetry Rust SDK using the stdout exporter.

Dependencies

Add these dependencies to your Cargo.toml:
Cargo.toml

Complete Example

1

Initialize the Tracer Provider

Set up the tracer provider with a stdout exporter and configure the trace context propagator.
2

Create and Start Spans

Create spans to track operations in your application.
3

Add Span Attributes

Enrich your spans with additional context using attributes.
4

Parent-Child Span Relationships

Create nested spans to represent hierarchical operations.

Span Kinds

OpenTelemetry defines different span kinds to categorize operations:
  • SpanKind::Client - Represents a request to a remote service
  • SpanKind::Server - Represents handling an incoming request
  • SpanKind::Internal - Represents an internal operation
  • SpanKind::Producer - Represents a message producer
  • SpanKind::Consumer - Represents a message consumer
Always call shutdown() on the tracer provider before your application exits to ensure all spans are properly flushed to the exporter.

Output

When you run your application, the stdout exporter will print span data in JSON format to the console, showing:
  • Trace ID and Span ID
  • Span name and kind
  • Start and end timestamps
  • Attributes and events
  • Parent-child relationships
For production use, replace the stdout exporter with an OTLP exporter to send traces to a backend like Jaeger, Zipkin, or a cloud observability platform. See the OTLP HTTP and OTLP gRPC examples.