#tracing-subscriber #tracing #performance #service #cache

tracing-actions

Tracing subscriber that vends action trace structures to a callback

5 unstable releases

0.3.1 Jun 23, 2023
0.3.0 Jun 23, 2023
0.2.0 Jun 21, 2023
0.1.2 Jun 13, 2023
0.1.0 May 9, 2023

#383 in Profiling

47 downloads per month
Used in tracing-actions-otlp

Apache-2.0

29KB
596 lines

tracing-actions

Tracing-actions is a tracing extension for recording service functions.

It gives you a tracing subscriber to which you can supply a callback. The tracing crate is highly general, and that generality is more than some uses need.

If you are trying to send opentelemetry line protocol traces to some service, you need to materialize whole spans to build up OTLP messages. The ActionTraceSubscriber gives you a callback for each completed span. It was made with OTLP in mind, but ActionTraces can be used for any number of other destinations.

Spans used by ActionTraceSubscriber can be optimistically cached, if you use the LazySpanCache. That feature is a simple best-effort racing cache. If 2 threads need a span at the same instant, one gets a cached span and the other makes a new one. They both try to return the new span to the cache upon completion, and if the cache is full the span is simply dropped.


lib.rs:

A measured, convenient approach to building traces.

tracing-actions is a trace recording toolbox. It records your live spans on the heap and offers you a visitor function which is called once per span with the ActionSpan.

Action traces, being heap-allocated, are held in an object pool to mitigate the cost of allocations. While low overhead is a goal of tracing-actions, 0-overhead is not. This is a tool for convenience first and performance second.

Examples

K-log

use tracing_actions;
use log;

// First, we implement k-logging.
struct KLog {
  k: usize,
  n: std::sync::atomic::AtomicUsize,
}
impl tracing_actions::TraceSink for KLog {
    fn sink_trace(&self, action_span: &mut tracing_actions::ActionSpan) {
        if self.n.fetch_add(1, std::sync::atomic::Ordering::Relaxed) % self.k == 0 {
            log::info!("trace: {action_span:?}")
        }
    }
}

// Next, we configure a subscriber.
let level = "debug".parse().unwrap();
let k_logging_subscriber = tracing_actions::ActionTraceSubscriber::new(
    level,
    KLog { k: 42, n: Default::default() },
    tracing_actions::span_constructor::LazySpanCache::default(),
);

// Finally, we install the subscriber.
tracing::subscriber::set_global_default(k_logging_subscriber)
    .expect("I should be able to set the global trace subscriber");

// Now the rest of your application will k-log ActionSpans.

Dependencies

~0.6–1MB
~14K SLoC