Skip to main content

micromegas_tracing/
guards.rs

1//! RAII-style guards
2use std::{collections::HashMap, marker::PhantomData, sync::Arc};
3
4use crate::{
5    dispatch::{
6        flush_log_buffer, flush_metrics_buffer, flush_thread_buffer, init_event_dispatch,
7        init_thread_stream, on_begin_named_scope, on_begin_scope, on_end_named_scope, on_end_scope,
8        shutdown_dispatch,
9    },
10    errors::Result,
11    event::EventSink,
12    panic_hook::init_panic_hook,
13    spans::{SpanLocation, SpanMetadata},
14};
15
16pub struct TracingSystemGuard {}
17
18impl TracingSystemGuard {
19    /// Instantiates a new system guard that initializes the telemetry system.
20    pub fn new(
21        logs_buffer_size: usize,
22        metrics_buffer_size: usize,
23        threads_buffer_size: usize,
24        sink: Arc<dyn EventSink>,
25        process_properties: HashMap<String, String>,
26        cpu_tracing_enabled: bool,
27    ) -> Result<Self> {
28        init_telemetry(
29            logs_buffer_size,
30            metrics_buffer_size,
31            threads_buffer_size,
32            sink,
33            process_properties,
34            cpu_tracing_enabled,
35        )?;
36        Ok(Self {})
37    }
38}
39
40impl Drop for TracingSystemGuard {
41    fn drop(&mut self) {
42        shutdown_telemetry();
43    }
44}
45
46pub fn init_telemetry(
47    logs_buffer_size: usize,
48    metrics_buffer_size: usize,
49    threads_buffer_size: usize,
50    sink: Arc<dyn EventSink>,
51    process_properties: HashMap<String, String>,
52    cpu_tracing_enabled: bool,
53) -> Result<()> {
54    init_event_dispatch(
55        logs_buffer_size,
56        metrics_buffer_size,
57        threads_buffer_size,
58        sink,
59        process_properties,
60        cpu_tracing_enabled,
61    )?;
62    init_panic_hook();
63    Ok(())
64}
65
66pub fn shutdown_telemetry() {
67    flush_log_buffer();
68    flush_metrics_buffer();
69    shutdown_dispatch();
70}
71
72pub struct TracingThreadGuard {
73    _dummy_ptr: *mut u8,
74}
75
76impl TracingThreadGuard {
77    pub fn new() -> Self {
78        init_thread_stream();
79        Self {
80            _dummy_ptr: std::ptr::null_mut(),
81        }
82    }
83}
84
85impl Drop for TracingThreadGuard {
86    fn drop(&mut self) {
87        flush_thread_buffer();
88    }
89}
90
91//not used at the time of writing, but clippy wants it
92impl Default for TracingThreadGuard {
93    fn default() -> Self {
94        Self::new()
95    }
96}
97
98// sync scope guard
99pub struct ThreadSpanGuard {
100    thread_span_desc: &'static SpanMetadata,
101    _dummy_ptr: PhantomData<*mut u8>, // to mark the object as !Send
102}
103
104impl ThreadSpanGuard {
105    pub fn new(thread_span_desc: &'static SpanMetadata) -> Self {
106        let guard = Self {
107            thread_span_desc,
108            _dummy_ptr: std::marker::PhantomData {},
109        };
110        on_begin_scope(thread_span_desc);
111        guard
112    }
113}
114
115impl Drop for ThreadSpanGuard {
116    fn drop(&mut self) {
117        on_end_scope(self.thread_span_desc);
118    }
119}
120
121pub struct ThreadNamedSpanGuard {
122    thread_span_location: &'static SpanLocation,
123    name: &'static str,
124    _dummy_ptr: PhantomData<*mut u8>, // to mark the object as !Send
125}
126
127impl ThreadNamedSpanGuard {
128    pub fn new(thread_span_location: &'static SpanLocation, name: &'static str) -> Self {
129        let guard = Self {
130            thread_span_location,
131            name,
132            _dummy_ptr: std::marker::PhantomData {},
133        };
134        on_begin_named_scope(guard.thread_span_location, guard.name);
135        guard
136    }
137}
138
139impl Drop for ThreadNamedSpanGuard {
140    fn drop(&mut self) {
141        on_end_named_scope(self.thread_span_location, self.name);
142    }
143}