tracing_dynamic

Crates.iotracing_dynamic
lib.rstracing_dynamic
version0.3.1
sourcesrc
created_at2023-10-26 20:24:01.183138
updated_at2023-10-31 09:23:39.526384
descriptionUtilities for working with Tokio Tracing, with dynamic fields for events and spans
homepagehttps://github.com/BrynCooke/tracing-dynamic
repositoryhttps://github.com/BrynCooke/tracing-dynamic
max_upload_size
id1015160
size36,376
Bryn Cooke (BrynCooke)

documentation

README

tracing-dynamic

:warning: Read this before using this library :warning:

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"

Commit count: 16

cargo fmt