Skip to main content
This guide walks you through creating a simple Rust application that emits traces, metrics, and logs using OpenTelemetry. We’ll use the stdout exporter so you can see the telemetry data directly in your console.

Create a new project

1

Create a new Rust project

2

Add dependencies

Add the following to your Cargo.toml:
Cargo.toml
3

Write the code

Replace the contents of src/main.rs with the complete example below.
4

Run the application

You’ll see traces, metrics, and logs printed to stdout in JSON format.

Complete example

This example demonstrates all three telemetry signals:
src/main.rs

Understanding the example

Let’s break down the key components:

Resource

A resource identifies your service and provides context for all telemetry:

Providers

Providers manage the lifecycle of telemetry pipelines:
  • TracerProvider - Creates tracers and manages span exporters
  • MeterProvider - Creates meters and manages metric exporters
  • LoggerProvider - Manages log exporters and bridges to logging frameworks
Always call shutdown() on providers when your application exits to ensure all telemetry is flushed.

Creating spans

Spans represent units of work in distributed tracing:

Recording metrics

Metrics track measurements over time:

Emitting logs

Logs provide structured event data:

Metrics example

Here’s a focused example showing different metric instrument types:
src/main.rs

Using OTLP exporter

To send telemetry to an OpenTelemetry Collector or OTLP-compatible backend:
1

Update dependencies

Cargo.toml
2

Replace exporter initialization

3

Configure endpoint (optional)

By default, the OTLP exporter sends to http://localhost:4318. Configure a different endpoint:
Use with_batch_exporter() for production to export telemetry in batches, reducing overhead compared to with_simple_exporter().

Environment variables

Configure OTLP exporters using environment variables:

Common patterns

Error handling

Context propagation

Instrumentation scope

Next steps

API Reference

Explore the complete API documentation

Examples

Browse more examples in the GitHub repository

OTLP Configuration

Learn about OTLP exporter configuration

Semantic Conventions

Use standard attribute names and values