micromegas_tracing/
guards.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! RAII-style guards
use std::{marker::PhantomData, sync::Arc};

use crate::{
    dispatch::{
        flush_log_buffer, flush_metrics_buffer, flush_thread_buffer, init_event_dispatch,
        init_thread_stream, on_begin_async_named_scope, on_begin_async_scope, on_begin_named_scope,
        on_begin_scope, on_end_async_named_scope, on_end_async_scope, on_end_named_scope,
        on_end_scope, shutdown_dispatch,
    },
    errors::Result,
    event::EventSink,
    panic_hook::init_panic_hook,
    spans::{SpanLocation, SpanMetadata},
};

pub struct TracingSystemGuard {}

impl TracingSystemGuard {
    pub fn new(
        logs_buffer_size: usize,
        metrics_buffer_size: usize,
        threads_buffer_size: usize,
        sink: Arc<dyn EventSink>,
    ) -> Result<Self> {
        init_telemetry(
            logs_buffer_size,
            metrics_buffer_size,
            threads_buffer_size,
            sink,
        )?;
        Ok(Self {})
    }
}

impl Drop for TracingSystemGuard {
    fn drop(&mut self) {
        shutdown_telemetry();
    }
}

pub fn init_telemetry(
    logs_buffer_size: usize,
    metrics_buffer_size: usize,
    threads_buffer_size: usize,
    sink: Arc<dyn EventSink>,
) -> Result<()> {
    init_event_dispatch(
        logs_buffer_size,
        metrics_buffer_size,
        threads_buffer_size,
        sink,
    )?;
    init_panic_hook();
    Ok(())
}

pub fn shutdown_telemetry() {
    flush_log_buffer();
    flush_metrics_buffer();
    shutdown_dispatch();
}

pub struct TracingThreadGuard {
    _dummy_ptr: *mut u8,
}

impl TracingThreadGuard {
    pub fn new() -> Self {
        init_thread_stream();
        Self {
            _dummy_ptr: std::ptr::null_mut(),
        }
    }
}

impl Drop for TracingThreadGuard {
    fn drop(&mut self) {
        flush_thread_buffer();
    }
}

//not used at the time of writing, but clippy wants it
impl Default for TracingThreadGuard {
    fn default() -> Self {
        Self::new()
    }
}

// sync scope guard
pub struct ThreadSpanGuard {
    thread_span_desc: &'static SpanMetadata,
    _dummy_ptr: PhantomData<*mut u8>, // to mark the object as !Send
}

impl ThreadSpanGuard {
    pub fn new(thread_span_desc: &'static SpanMetadata) -> Self {
        let guard = Self {
            thread_span_desc,
            _dummy_ptr: std::marker::PhantomData {},
        };
        on_begin_scope(thread_span_desc);
        guard
    }
}

impl Drop for ThreadSpanGuard {
    fn drop(&mut self) {
        on_end_scope(self.thread_span_desc);
    }
}

pub struct ThreadNamedSpanGuard {
    thread_span_location: &'static SpanLocation,
    name: &'static str,
    _dummy_ptr: PhantomData<*mut u8>, // to mark the object as !Send
}

impl ThreadNamedSpanGuard {
    pub fn new(thread_span_location: &'static SpanLocation, name: &'static str) -> Self {
        let guard = Self {
            thread_span_location,
            name,
            _dummy_ptr: std::marker::PhantomData {},
        };
        on_begin_named_scope(guard.thread_span_location, guard.name);
        guard
    }
}

impl Drop for ThreadNamedSpanGuard {
    fn drop(&mut self) {
        on_end_named_scope(self.thread_span_location, self.name);
    }
}

// async scope guard
pub struct AsyncSpanGuard {
    span_desc: &'static SpanMetadata,
    span_id: u64,
}

impl AsyncSpanGuard {
    pub fn new(span_desc: &'static SpanMetadata) -> Self {
        let span_id = on_begin_async_scope(span_desc);
        Self { span_desc, span_id }
    }
}

impl Drop for AsyncSpanGuard {
    fn drop(&mut self) {
        on_end_async_scope(self.span_id, self.span_desc);
    }
}

pub struct AsyncNamedSpanGuard {
    span_location: &'static SpanLocation,
    name: &'static str,
    span_id: u64,
}

impl AsyncNamedSpanGuard {
    pub fn new(span_location: &'static SpanLocation, name: &'static str) -> Self {
        let span_id = on_begin_async_named_scope(span_location, name);
        Self {
            span_location,
            name,
            span_id,
        }
    }
}

impl Drop for AsyncNamedSpanGuard {
    fn drop(&mut self) {
        on_end_async_named_scope(self.span_id, self.span_location, self.name);
    }
}