1use 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_scope, on_end_named_scope_tagged, on_end_scope,
8 on_end_scope_tagged, shutdown_dispatch,
9 },
10 errors::Result,
11 event::EventSink,
12 panic_hook::init_panic_hook,
13 spans::{SpanLocation, SpanMetadata, SpanName, SpanProperties, StaticSpanName},
14};
15
16pub struct TracingSystemGuard {}
17
18impl TracingSystemGuard {
19 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
91impl Default for TracingThreadGuard {
93 fn default() -> Self {
94 Self::new()
95 }
96}
97
98mod sealed {
99 pub trait Sealed {}
100 impl Sealed for () {}
101 impl Sealed for super::SpanProperties {}
102}
103
104pub trait SpanEndProperties: sealed::Sealed {
107 #[doc(hidden)]
108 fn end_thread(&self, desc: &'static SpanMetadata);
109 #[doc(hidden)]
110 fn end_thread_named<N: SpanName>(&self, location: &'static SpanLocation, name: &N);
111}
112
113impl SpanEndProperties for () {
114 #[inline(always)]
115 fn end_thread(&self, desc: &'static SpanMetadata) {
116 on_end_scope(desc);
117 }
118
119 #[inline(always)]
120 fn end_thread_named<N: SpanName>(&self, location: &'static SpanLocation, name: &N) {
121 name.end_thread(location);
122 }
123}
124
125impl SpanEndProperties for SpanProperties {
126 fn end_thread(&self, desc: &'static SpanMetadata) {
127 if self.is_empty() {
128 on_end_scope(desc);
129 } else {
130 self.with_dyn(|set, dyn_properties| on_end_scope_tagged(desc, set, dyn_properties));
131 }
132 }
133
134 fn end_thread_named<N: SpanName>(&self, location: &'static SpanLocation, name: &N) {
135 if self.is_empty() {
136 name.end_thread(location);
137 } else {
138 self.with_dyn(|set, dyn_properties| {
139 on_end_named_scope_tagged(location, name.as_str(), set, dyn_properties);
140 });
141 }
142 }
143}
144
145#[must_use = "the span ends when the guard is dropped"]
148pub struct ThreadSpanGuard<P: SpanEndProperties = ()> {
149 thread_span_desc: &'static SpanMetadata,
150 properties: P,
151 _dummy_ptr: PhantomData<*mut u8>, }
153
154impl ThreadSpanGuard {
155 pub fn new(thread_span_desc: &'static SpanMetadata) -> Self {
156 let guard = Self {
157 thread_span_desc,
158 properties: (),
159 _dummy_ptr: std::marker::PhantomData {},
160 };
161 on_begin_scope(thread_span_desc);
162 guard
163 }
164}
165
166impl ThreadSpanGuard<SpanProperties> {
167 pub fn with_properties(
169 thread_span_desc: &'static SpanMetadata,
170 properties: SpanProperties,
171 ) -> Self {
172 let guard = Self {
173 thread_span_desc,
174 properties,
175 _dummy_ptr: std::marker::PhantomData {},
176 };
177 on_begin_scope(thread_span_desc);
178 guard
179 }
180
181 pub fn set_property(&mut self, name: &'static str, value: impl std::fmt::Display) {
183 self.properties.set_property(name, value);
184 }
185}
186
187impl<P: SpanEndProperties> Drop for ThreadSpanGuard<P> {
188 fn drop(&mut self) {
189 self.properties.end_thread(self.thread_span_desc);
190 }
191}
192
193#[must_use = "the span ends when the guard is dropped"]
195pub struct ThreadNamedSpanGuard<N: SpanName = StaticSpanName, P: SpanEndProperties = ()> {
196 thread_span_location: &'static SpanLocation,
197 name: N,
198 properties: P,
199 _dummy_ptr: PhantomData<*mut u8>, }
201
202impl ThreadNamedSpanGuard {
203 pub fn new(thread_span_location: &'static SpanLocation, name: &'static str) -> Self {
204 Self::with_name(thread_span_location, StaticSpanName(name))
205 }
206}
207
208impl<N: SpanName> ThreadNamedSpanGuard<N> {
209 pub fn with_name(thread_span_location: &'static SpanLocation, name: N) -> Self {
210 let guard = Self {
211 thread_span_location,
212 name,
213 properties: (),
214 _dummy_ptr: std::marker::PhantomData {},
215 };
216 guard.name.begin_thread(guard.thread_span_location);
217 guard
218 }
219}
220
221impl<N: SpanName> ThreadNamedSpanGuard<N, SpanProperties> {
222 pub fn with_properties(
224 thread_span_location: &'static SpanLocation,
225 name: N,
226 properties: SpanProperties,
227 ) -> Self {
228 let guard = Self {
229 thread_span_location,
230 name,
231 properties,
232 _dummy_ptr: std::marker::PhantomData {},
233 };
234 guard.name.begin_thread(guard.thread_span_location);
235 guard
236 }
237
238 pub fn set_property(&mut self, name: &'static str, value: impl std::fmt::Display) {
240 self.properties.set_property(name, value);
241 }
242}
243
244impl<N: SpanName, P: SpanEndProperties> Drop for ThreadNamedSpanGuard<N, P> {
245 fn drop(&mut self) {
246 self.properties
247 .end_thread_named(self.thread_span_location, &self.name);
248 }
249}