Crate metrics_tracing_context[][src]

Expand description

Use tracing::span! fields as metrics labels.

The metrics-tracing-context crate provides tools to enable injecting the contextual data maintained via span! macro from the tracing crate into the metrics.

Use

First, set up tracing and metrics crates:

use metrics_util::layers::Layer;
use tracing_subscriber::layer::SubscriberExt;
use metrics_tracing_context::{MetricsLayer, TracingContextLayer};

// Prepare tracing.
let subscriber = mysubscriber.with(MetricsLayer::new());
tracing::subscriber::set_global_default(subscriber).unwrap();

// Prepare metrics.
let recorder = TracingContextLayer::all().layer(myrecorder);
metrics::set_boxed_recorder(Box::new(recorder)).unwrap();

Then emit some metrics within spans and see the labels being injected!

use tracing::{span, Level};
use metrics::counter;

let user = "ferris";
let span = span!(Level::TRACE, "login", user);
let _guard = span.enter();

counter!("login_attempts", 1, "service" => "login_service");

The code above will emit a increment for a login_attempts counter with the following labels:

  • service=login_service
  • user=ferris

Re-exports

pub use label_filter::LabelFilter;

Modules

label_filter

Label filtering.

Structs

MetricsLayer

MetricsLayer is a tracing_subscriber::Layer that captures the span fields and allows them to be later on used as metrics labels.

TracingContext

TracingContext is a metrics::Recorder that injects labels fromtracing::Spans.

TracingContextLayer

TracingContextLayer provides an implementation of a Layer for TracingContext.

Traits

SpanExt

An extention to the tracing::Span, enabling the access to labels.