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 yourCargo.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 serviceSpanKind::Server- Represents handling an incoming requestSpanKind::Internal- Represents an internal operationSpanKind::Producer- Represents a message producerSpanKind::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