prima_tracing.rs

Utilities for configuring a tracing subscriber with support for logging and opentelemetry.


Installation

Install from GitHub

toml prima-tracing = { git="https://github.com/primait/prima_tracing.rs", branch="master" }

Cargo features

Example

Simple

```rust use primatracing::{builder, configuresubscriber, initsubscriber}; use tracing::{info, infospan};

fn main() -> std::io::Result<()> { let subscriber = configuresubscriber(builder("simple").withenv("dev".to_string()).build());

let _guard = init_subscriber(subscriber);

let span = info_span!("MySpan");
let _guard = span.enter();

info!("Starting my awesome app");
Ok(())

} ```

JSON output

It works like the simple example, but activating the prima-json-logger automatically uses the JSON format as output

```rust use primatracing::{builder, configuresubscriber, initsubscriber}; use tracing::{info, infospan};

fn main() -> std::io::Result<()> { let subscriber = configuresubscriber(builder("json").withenv("dev".to_string()).build());

let _guard = init_subscriber(subscriber);

let span = info_span!("MySpan");
let _guard = span.enter();

info!("Starting my awesome app");
Ok(())

}

```

Opentelemetry

```rust use primatracing::{builder, configuresubscriber, initsubscriber}; use tracing::{info, infospan};

fn main() -> std::io::Result<()> { let subscriber = configuresubscriber( builder("myapp") .withenv("dev".tostring()) .withversion("1.0".tostring()) .withtelemetry( "http://localhost:9411/api/v2/spans".tostring(), "myapp".tostring(), ) .build(), );

let _guard = init_subscriber(subscriber);

let span = info_span!("MySpan");
let _guard = span.enter();

info!("Starting my awesome app");
Ok(())

}

```

Custom Subscriber

```rust use primatracing::json; use tracing::{info, infospan}; use tracinglog::LogTracer; use tracingsubscriber::{layer::SubscriberExt, EnvFilter};

fn main() -> std::io::Result<()> { let subscriber = tracingsubscriber::Registry::default() .with(EnvFilter::fromdefaultenv()) .with(json::storage::layer()) .with(json::formatter::layer("test".toowned(), "dev".to_owned()));

LogTracer::init().expect("Failed to set logger");
tracing::subscriber::set_global_default(subscriber).expect("Setting default subscriber failed");

let span = info_span!("MySpan");
let _guard = span.enter();

info!("Starting my awesome app");
Ok(())

} ```

Running examples

Simple

sh export RUST_LOG=info cargo run --example simple

Complex (OpenTelemetry)

Run Jaeger locally

sh docker run -d -e COLLECTOR_ZIPKIN_HOST_PORT=:9411 -p6831:6831/udp -p6832:6832/udp -p16686:16686 -p 9411:9411 jaegertracing/all-in-one:latest

Run pong service:

sh export RUST_LOG=info cargo run --features=prima-telemetry --example pong

Run ping service:

sh export RUST_LOG=info cargo run --features=prima-telemetry --example ping

Check health of ping service (which calls pong service)

sh curl http://localhost:8081/check

Open the browser at http://localhost:16686 to inspect the traced request

OpenTelemetry + JSON logger with Datadog correlation IDs

sh RUST_LOG=info cargo run --features=prima-logger-datadog,prima-telemetry --example datadog_json_logger

Custom formatter

sh export RUST_LOG=info cargo run --features=prima-logger-json --example custom_formatter

Custom subscriber with default JSON output

sh export RUST_LOG=info cargo run --features=prima-logger-json --example custom-subscriber

Custom subscriber with custom JSON output

sh export RUST_LOG=info cargo run --features=prima-logger-json --example custom-json-subscriber