Install from crates.io
toml
prima-tracing = "0.4.0"
prima-logger-json
outputs traces to standard output in JSON formatprima-logger-datadog
extends prima-logger-json
output
with trace and span information allowing
Datadog to connect logs and tracesprima-telemetry
exports OpenTelemetry traces using the opentelemetry-otlp exporterrt-tokio-current-thread
configures the OpenTelemetry tracer to use Tokio’s current thread runtime
(e.g. actix_web::main
). Without this feature, the Tokio multi-thread runtime is used by default.```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(())
} ```
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(())
}
```
You need to have an OpenTelemetry collector (such as Jaeger) running locally.
```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:55681/v1/traces".tostring(), "myapp".tostring(), ) .build(), );
let _guard = init_subscriber(subscriber);
let span = info_span!("MySpan");
let _guard = span.enter();
info!("Starting my awesome app");
Ok(())
}
```
```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(())
} ```
sh
RUST_LOG=info cargo run --example simple
Run Jaeger locally
sh
docker run --rm -d -p 16686:16686 -p 55681:55681 jaegertracing/opentelemetry-all-in-one:latest
Run pong service:
sh
RUST_LOG=info cargo run --features=prima-telemetry --example pong
Run ping service:
sh
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
sh
RUST_LOG=info cargo run --features=prima-logger-datadog,prima-telemetry --example datadog_json_logger
sh
RUST_LOG=info cargo run --features=prima-logger-json --example custom_formatter
sh
RUST_LOG=info cargo run --features=prima-logger-json --example custom-subscriber
sh
RUST_LOG=info cargo run --features=prima-logger-json --example custom-json-subscriber