Skip to content

Rust

Terminal window
cargo add opentelemetry opentelemetry_sdk opentelemetry-otlp opentelemetry-appender-tracing
cargo add tracing tracing-subscriber tracing-opentelemetry

Add this at the start of main. It sends tracing events as logs and tracing spans as traces:

let spans = SpanExporter::builder().with_http().build().unwrap();
let tracer_provider = SdkTracerProvider::builder().with_batch_exporter(spans).build();
let metrics = MetricExporter::builder().with_http().build().unwrap();
let meter_provider = SdkMeterProvider::builder().with_periodic_exporter(metrics).build();
let logs = LogExporter::builder().with_http().build().unwrap();
let logger_provider = SdkLoggerProvider::builder().with_batch_exporter(logs).build();
global::set_meter_provider(meter_provider.clone());
tracing_subscriber::registry()
.with(OpenTelemetryTracingBridge::new(&logger_provider))
.with(tracing_opentelemetry::layer().with_tracer(tracer_provider.tracer("checkout")))
.init();

Before your program exits, call shutdown() on the three providers so buffered data is sent.

Set these where your app runs. Replace YOUR_INGEST_KEY with your key and checkout with your service name.

Terminal window
export OTEL_SERVICE_NAME=checkout
export OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=production
export OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp.siteqwality.com
export OTEL_EXPORTER_OTLP_HEADERS=Authorization=YOUR_INGEST_KEY
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf

Keep using the tracing macros. Events inside a span are linked to its trace:

tracing::info!(order_id = 42, "order placed");
let orders = global::meter("checkout").u64_counter("orders.placed").build();
orders.add(1, &[]);

Open Logs, Traces or Metrics. New data usually shows up within a minute, and a page that says it is waiting for data switches to your data on its own.

The code on this page comes from a runnable example that is checked against our ingest rules every month.

Nothing arriving? Check that the key is an ingest key for this account, that your account has early access, and the status codes in Ingest routes and limits.