#tracing #instrumentation #dynamic #fields #logging #field

tracing_dynamic

Utilities for working with Tokio Tracing, with dynamic fields for events and spans

4 releases (2 breaking)

0.3.1 Oct 31, 2023
0.3.0 Oct 31, 2023
0.2.0 Oct 27, 2023
0.1.0 Oct 26, 2023

#39 in #instrumentation

Apache-2.0

23KB
572 lines

tracing-dynamic

⚠️ Read this before using this library ⚠️

This is a small library to allow you to create dynamic attributes on spans and events.

Things to consider before using this:

  • It will leak memory. This happens on each instantiation of SpanFactory or EventFactory. You'll want to create factories sparingly and reuse them.
  • I didn't test it in a real program.
  • It'll be slower than the tracing macros, but you came here for flexibility right?

If after reading the above you want to try it, here's how you can use it:

tracing_dynamic = "0.3.1"
  use tracing_dynamic::{EventFactory, SpanFactory};
  // Some dynamic attributes, these could be read from a file or a database.
  let attrs = vec!["dyn_attr_1", "dyn_attr_2"];
  
  // Factories leak, create these once and reuse them.
  let span_factory = SpanFactory::new(
    "span_name",
    "span_target",
    tracing::Level::INFO,
    None,
    None,
    None,
    &attrs,
  );
  let event_factory = EventFactory::new(
    "event_name",
    "event_target",
    tracing::Level::INFO,
    None,
    None,
    None,
    &attrs,
  );
  
  let subscriber = tracing_subscriber::fmt().pretty().finish();
  tracing::subscriber::with_default(subscriber, || {
  // Create a span with attributes defined at runtime
    let span = span_factory.create()
      .with("dyn_attr_1", &"dyn_attr_1") // Field created at span creation time
      .build();
    let _guard = span.enter();
    span.record("dyn_attr_2", "dyn_attr_2");
    span.record("dyn_attr_4", "dyn_attr_4"); // Not in the original metadata, it'll be ignored.
    
    // Create an event with fields defined at runtime
    event_factory
      .create()
      .with("dyn_attr_1", &"dyn_attr_1")
      .with("dyn_attr_2", &"dyn_attr_2")
      .with("dyn_attr_4", &"dyn_attr_4") // Not in the original metadata, it'll be ignored.
      .finish();
  });

Output

  2023-10-30T08:44:52.178162Z  INFO event_target: dyn_attr_1: "dyn_attr_1", dyn_attr_2: "dyn_attr_2"
    in span_target::span_name with dyn_attr_1: "dyn_attr_1", dyn_attr_2: "dyn_attr_2"

Dependencies

~310–430KB