#open-telemetry #logging-tracing #otlp #logging #tracing

otlp-logger

A simple convenience crate to configure an OpenTelemetry otlp endpoint with Tokio Tracing

4 releases (breaking)

0.4.0 Sep 17, 2024
0.3.0 Apr 21, 2024
0.2.0 Apr 16, 2024
0.1.0 Apr 10, 2024

#402 in Debugging

Download history 145/week @ 2024-09-16 15/week @ 2024-09-23 15/week @ 2024-09-30 1/week @ 2024-10-07

181 downloads per month

Apache-2.0

21KB
240 lines

Crates.io: otlp-logger build LICENSE

otlp-logger

OpenTelemetry Logging with Tokio Tracing

This crate provides a convienent way to initialize the OpenTelemetry logger with otlp endpoint. It uses the opentelemetry and tracing crates to provide structured, context-aware logging for Rust applications.

Simply add the following to your Cargo.toml:

[dependencies]
tracing = "0.1"
otlp-logger = "0.4"
tokio = { version = "1.38", features = ["rt", "macros"] }

Because this crate uses the batching function of the OpenTelemetry SDK, it is required to use the tokio runtime. Due to this requirement, the tokio crate must be added as a dependency in your Cargo.toml file.

In your code initialize the logger with:

#[tokio::main]
async fn main() {
  // Initialize the OpenTelemetry logger using environment variables
  otlp_logger::init().await;
  // ... your application code

  // and optionally call open telemetry logger shutdown to make sure all the
  // data is sent to the configured endpoint before the application exits
  otlp_logger::shutdown();
}

If the OTEL_EXPORTER_OTLP_ENDPOINT environment variable is set, the OpenTelemetry logger will be used. Otherwise, the logger will default to only stdout.

The OpenTelemetry logger can be configured with the following environment variables:

  • OTEL_EXPORTER_OTLP_ENDPOINT: The endpoint to send OTLP data to.
  • OTEL_SERVICE_NAME: The name of the service.
  • OTEL_SERVICE_NAMESPACE: The namespace of the service.
  • OTEL_SERVICE_VERSION: The version of the service.
  • OTEL_SERVICE_INSTANCE_ID: The instance ID of the service.
  • OTEL_DEPLOYMENT_ENVIRONMENT: The deployment environment of the service.

The OpenTelemetry logger can also be configured with the OtlpConfig struct, which can be passed to the init_with_config function. The OtlpConfig struct can be built with the OtlpConfigBuilder struct.

Once the logger is initialized, you can use the tracing macros to log messages. For example:

use tracing::{info, error};

#[tokio::main]
async fn main() {
   otlp_logger::init().await;
   info!("This is an info message");
   error!("This is an error message");
}

Traces and logs are sent to the configured OTLP endpoint. The traces and log levels are configured via the RUST_LOG environment variable. This behavior can be overridden by setting the trace_level or stdout_level fields in the OtlpConfig struct.

use otlp_logger::{OtlpConfigBuilder, LevelFilter};

#[tokio::main]
async fn main() {
  let config = OtlpConfigBuilder::default()
                 .otlp_endpoint("http://localhost:4317".to_string())
                 .trace_level(LevelFilter::INFO)
                 .stdout_level(LevelFilter::ERROR)
                 .build()
                 .expect("failed to create otlp config builder");

  otlp_logger::init_with_config(config).await.expect("failed to initialize logger");

  // ... your application code

  // shutdown the logger
  otlp_logger::shutdown();
}

Current version: 0.4.0

License: Apache-2.0

Dependencies

~12–20MB
~247K SLoC