Rust
Integration
Section titled “Integration”Instrument your Rust applications to send traces, logs, and metrics to Kopai.
Installation
Section titled “Installation”Add to your Cargo.toml:
[dependencies]opentelemetry = "0.31"opentelemetry_sdk = { version = "0.31", features = ["rt-tokio"] }opentelemetry-otlp = { version = "0.31", features = ["http-json"] }tokio = { version = "1", features = ["full"] }Basic Setup
Section titled “Basic Setup”use opentelemetry::global;use opentelemetry::trace::Tracer;use opentelemetry_otlp::{Protocol, WithExportConfig};use opentelemetry_sdk::trace::SdkTracerProvider;use opentelemetry_sdk::Resource;use std::env;
fn init_tracer() -> SdkTracerProvider { let endpoint = env::var("OTEL_EXPORTER_OTLP_ENDPOINT") .unwrap_or_else(|_| "http://localhost:4318".to_string()); let service_name = env::var("OTEL_SERVICE_NAME") .unwrap_or_else(|_| "my-service".to_string());
let exporter = opentelemetry_otlp::SpanExporter::builder() .with_http() .with_protocol(Protocol::HttpJson) .with_endpoint(format!("{}/v1/traces", endpoint)) .build() .expect("Failed to create exporter");
let resource = Resource::builder() .with_service_name(service_name) .build();
let provider = SdkTracerProvider::builder() .with_resource(resource) .with_batch_exporter(exporter) .build();
global::set_tracer_provider(provider.clone()); provider}
fn main() { let provider = init_tracer(); let tracer = global::tracer("my-app");
tracer.in_span("my-operation", |_cx| { // ... your code });
provider.shutdown().expect("Shutdown failed");}Running Your Application
Section titled “Running Your Application”Set the required environment variables and run:
export OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp.kopai.app"export OTEL_SERVICE_NAME="my-service"cargo run --release| Variable | Description |
|---|---|
OTEL_EXPORTER_OTLP_ENDPOINT | Kopai OTLP endpoint |
OTEL_SERVICE_NAME | Name shown in Kopai UI |
Working Example
Section titled “Working Example”For a complete working example: