Rust
note
If you need an example application to test this integration, please refer to our Rust OpenTelemetry repository.
This guide explains how to configure your Rust application to send logs to Logz.io via OpenTelemetry.
Prerequisites
- A Rust application with logging capabilities.
- An active Logz.io account
- Your log shipping token
- Port
8071
available on your host system
Add Dependencies
To send logs to Logz.io via OpenTelemetry, add the required dependencies to your Cargo.toml
file:
[dependencies]
log = "0.4"
opentelemetry = "0.27"
opentelemetry-appender-log = "0.27"
opentelemetry_sdk = { version = "*", features = ["rt-tokio"] }
opentelemetry-otlp = { version = "*", features = ["http-proto", "reqwest-client", "reqwest-rustls"] }
tokio = { version = "1", features = ["full"] }
Configure Logging and Implement OTLP Communication
Import the following modules into your existing app:
use opentelemetry_appender_log::OpenTelemetryLogBridge;
use opentelemetry_otlp::{WithExportConfig, WithHttpConfig};
use std::collections::HashMap;
Add the exporters and logger provider section to your code:
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let endpoint = "https://otlp-listener.logz.io/v1/logs";
let api_token = "LOGZ_IO_TOKEN";
let logger_provider = opentelemetry_sdk::logs::LoggerProvider::builder()
.with_batch_exporter(
opentelemetry_otlp::LogExporter::builder()
.with_http()
.with_endpoint(endpoint)
.with_headers(HashMap::from([
("Authorization".to_string(), format!("Bearer {}", api_token), ),
("User-Agent".to_string(), format!("logzio-rust-logs-otlp"), ),
]))
.build()?,
opentelemetry_sdk::runtime::Tokio,
)
.build();
let log_bridge = OpenTelemetryLogBridge::new(&logger_provider);
Replace LOGZ_IO_TOKEN
with the shipping token of the account.
If needed, update the https://otlp-listener.logz.io/v1/logs
with the URL of your hosting region.
Run the application and check Logz.io for logs
Run your application with cargo run
. Give your logs some time to get from your system to ours.
Encounter an issue? See our log shipping troubleshooting guide.