Crates.io | tracing_dynamic |
lib.rs | tracing_dynamic |
version | 0.3.1 |
source | src |
created_at | 2023-10-26 20:24:01.183138 |
updated_at | 2023-10-31 09:23:39.526384 |
description | Utilities for working with Tokio Tracing, with dynamic fields for events and spans |
homepage | https://github.com/BrynCooke/tracing-dynamic |
repository | https://github.com/BrynCooke/tracing-dynamic |
max_upload_size | |
id | 1015160 |
size | 36,376 |
: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:
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"