near_o11y/
context.rs

1use tracing::Span;
2use tracing_opentelemetry::OpenTelemetrySpanExt;
3
4/// Wraps an actix message with the current Span's context.
5/// This lets us trace execution across several actix Actors.
6#[derive(actix::Message, Debug)]
7#[rtype(result = "<T as actix::Message>::Result")]
8pub struct WithSpanContext<T: actix::Message> {
9    pub msg: T,
10    pub context: opentelemetry::Context,
11}
12
13impl<T: actix::Message> WithSpanContext<T> {
14    pub fn new(msg: T) -> Self {
15        Self { msg, context: Span::current().context() }
16    }
17}
18
19/// Allows easily attaching the current span's context to a Message.
20pub trait WithSpanContextExt: actix::Message {
21    fn with_span_context(self) -> WithSpanContext<Self>
22    where
23        Self: Sized,
24    {
25        WithSpanContext::<Self>::new(self)
26    }
27}
28impl<T: actix::Message> WithSpanContextExt for T {}