Skip to main content

micromegas_tracing/spans/
span_name.rs

1//! Static and runtime span names behind one interface, so that guards and futures are written once.
2use crate::dispatch::{
3    on_begin_async_named_scope, on_begin_async_named_scope_dyn, on_begin_named_scope,
4    on_begin_named_scope_dyn, on_end_async_named_scope, on_end_async_named_scope_dyn,
5    on_end_named_scope, on_end_named_scope_dyn,
6};
7use crate::spans::SpanLocation;
8
9mod sealed {
10    pub trait Sealed {}
11    impl Sealed for super::StaticSpanName {}
12    impl<N: AsRef<str>> Sealed for super::DynSpanName<N> {}
13}
14
15pub trait SpanName: sealed::Sealed {
16    fn as_str(&self) -> &str;
17    fn begin_thread(&self, loc: &'static SpanLocation);
18    fn end_thread(&self, loc: &'static SpanLocation);
19    fn begin_async(&self, loc: &'static SpanLocation, parent: u64, depth: u32) -> u64;
20    fn end_async(&self, span_id: u64, parent: u64, loc: &'static SpanLocation, depth: u32);
21}
22
23/// A `&'static str` name. It is a lifetime-free wrapper because a trait implemented only for
24/// `&'static str` makes rustc reject `Send` futures that await an instrumented future
25/// ("implementation is not general enough").
26#[derive(Clone, Copy)]
27pub struct StaticSpanName(pub &'static str);
28
29impl SpanName for StaticSpanName {
30    #[inline(always)]
31    fn as_str(&self) -> &str {
32        self.0
33    }
34
35    #[inline(always)]
36    fn begin_thread(&self, loc: &'static SpanLocation) {
37        on_begin_named_scope(loc, self.0);
38    }
39
40    #[inline(always)]
41    fn end_thread(&self, loc: &'static SpanLocation) {
42        on_end_named_scope(loc, self.0);
43    }
44
45    #[inline(always)]
46    fn begin_async(&self, loc: &'static SpanLocation, parent: u64, depth: u32) -> u64 {
47        on_begin_async_named_scope(loc, self.0, parent, depth)
48    }
49
50    #[inline(always)]
51    fn end_async(&self, span_id: u64, parent: u64, loc: &'static SpanLocation, depth: u32) {
52        on_end_async_named_scope(span_id, parent, loc, self.0, depth);
53    }
54}
55
56/// A span name built at runtime. It is copied into the block that records each event.
57pub struct DynSpanName<N>(pub N);
58
59impl<N: AsRef<str>> SpanName for DynSpanName<N> {
60    #[inline(always)]
61    fn as_str(&self) -> &str {
62        self.0.as_ref()
63    }
64
65    #[inline(always)]
66    fn begin_thread(&self, loc: &'static SpanLocation) {
67        on_begin_named_scope_dyn(loc, self.0.as_ref());
68    }
69
70    #[inline(always)]
71    fn end_thread(&self, loc: &'static SpanLocation) {
72        on_end_named_scope_dyn(loc, self.0.as_ref());
73    }
74
75    #[inline(always)]
76    fn begin_async(&self, loc: &'static SpanLocation, parent: u64, depth: u32) -> u64 {
77        on_begin_async_named_scope_dyn(loc, self.0.as_ref(), parent, depth)
78    }
79
80    #[inline(always)]
81    fn end_async(&self, span_id: u64, parent: u64, loc: &'static SpanLocation, depth: u32) {
82        on_end_async_named_scope_dyn(span_id, parent, loc, self.0.as_ref(), depth);
83    }
84}