veecle-telemetry 0.1.0

Veecle OS telemetry
Documentation
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#![expect(missing_docs, reason = "tests")]
#![cfg(not(miri))]

// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
// Copyright 2025 Veecle GmbH.
//
// This file has been modified from the original TiKV implementation.

use std::time::Duration;

use indoc::indoc;
use serial_test::serial;
use tokio::runtime::Builder;
use veecle_telemetry::future::FutureExt;
use veecle_telemetry::protocol::Severity;
use veecle_telemetry::test_helpers::format_telemetry_tree;
use veecle_telemetry::{CurrentSpan, KeyValue, Span, SpanContext, instrument, span};

mod exporter {
    use std::sync::{Arc, LazyLock, Mutex};

    use veecle_telemetry::collector::TestExporter;
    use veecle_telemetry::protocol::{ExecutionId, InstanceMessage};

    /// Initializes the lazy lock which sets the exporter.
    pub fn set_exporter() -> ExporterHandle {
        static EXPORTER: LazyLock<Arc<Mutex<Vec<InstanceMessage<'static>>>>> =
            LazyLock::new(|| {
                let (reporter, collected_spans) = TestExporter::new();

                let execution_id = ExecutionId::random(&mut rand::rng());
                veecle_telemetry::collector::set_exporter(
                    execution_id,
                    Box::leak(Box::new(reporter)),
                )
                .expect("exporter was not set yet");

                collected_spans
            });

        ExporterHandle {
            message_buffer: EXPORTER.clone(),
        }
    }

    pub struct ExporterHandle {
        message_buffer: Arc<Mutex<Vec<InstanceMessage<'static>>>>,
    }

    impl ExporterHandle {
        pub fn take_messages(&self) -> Vec<InstanceMessage<'static>> {
            self.message_buffer.lock().unwrap().drain(..).collect()
        }
    }
}

use exporter::set_exporter;

#[test]
#[serial]
fn trace_macro() {
    trait Foo {
        async fn run(&self, millis: &u64);
    }

    struct Bar;

    impl Foo for Bar {
        #[instrument(name = "run")]
        async fn run(&self, millis: &u64) {
            work_inner().await;
            work(millis).await;
            let _g = span!("local-span").entered();
        }
    }

    #[instrument(short_name = true)]
    async fn work(millis: &u64) {
        work_inner().await;
        tokio::time::sleep(Duration::from_millis(*millis))
            .with_span(span!("sleep"))
            .await;
    }

    #[instrument(short_name = true)]
    async fn work_inner() {
        tokio::time::sleep(Duration::from_millis(1)).await;
    }

    impl Bar {
        #[instrument(short_name = true)]
        async fn work2(&self, millis: &u64) {
            work_inner().await;
            tokio::time::sleep(Duration::from_millis(*millis))
                .with_span(span!("sleep"))
                .await;
        }
    }

    #[instrument(short_name = true)]
    async fn work3(millis1: &u64, millis2: &u64) {
        work_inner().await;
        tokio::time::sleep(Duration::from_millis(*millis1))
            .with_span(span!("sleep"))
            .await;
        tokio::time::sleep(Duration::from_millis(*millis2))
            .with_span(span!("sleep"))
            .await;
    }

    let exporter = set_exporter();

    {
        let context = SpanContext::generate();
        let root = Span::root("root", context, &[]);

        let runtime = Builder::new_multi_thread()
            .worker_threads(4)
            .enable_all()
            .build()
            .unwrap();

        runtime.block_on(
            async {
                Bar.run(&100).await;
                Bar.work2(&100).await;
                work3(&100, &100).await;
            }
            .with_span(root),
        );
    }

    let graph = format_telemetry_tree(exporter.take_messages());
    assert_eq!(
        graph,
        indoc! {"
            root []
                run []
                    work_inner []
                    work []
                        work_inner []
                        sleep []
                    local-span []
                work2 []
                    work_inner []
                    sleep []
                work3 []
                    work_inner []
                    sleep []
                    sleep []
        "}
    );
}

#[test]
#[serial]
fn trace_macro_example() {
    #[instrument(short_name = true)]
    fn do_something_short_name(i: u64) {
        std::thread::sleep(Duration::from_millis(i));
    }

    #[instrument(short_name = true)]
    async fn do_something_async_short_name(i: u64) {
        tokio::time::sleep(Duration::from_millis(i)).await;
    }

    #[instrument]
    fn do_something(i: u64) {
        std::thread::sleep(Duration::from_millis(i));
    }

    #[instrument]
    async fn do_something_async(i: u64) {
        tokio::time::sleep(Duration::from_millis(i)).await;
    }

    let exporter = set_exporter();

    {
        let context = SpanContext::generate();
        let _root_guard = Span::root("root", context, &[]).entered();

        let runtime = Builder::new_current_thread().enable_all().build().unwrap();

        do_something(100);
        runtime.block_on(do_something_async(100));
        do_something_short_name(100);
        runtime.block_on(do_something_async_short_name(100));
    }

    let graph = format_telemetry_tree(exporter.take_messages());
    assert_eq!(
        graph,
        indoc! {"
            root []
                lib::trace_macro_example::{{closure}}::do_something []
                lib::trace_macro_example::{{closure}}::do_something_async []
                do_something_short_name []
                do_something_async_short_name []
        "}
    );
}

#[test]
#[serial]
fn span_property() {
    let exporter = set_exporter();

    {
        let context = SpanContext::generate();
        let root = Span::root(
            "root",
            context,
            &[KeyValue::new("k1", "v1"), KeyValue::new("k2", 2)],
        );
        root.set_attribute(KeyValue::new("k3", "v3"));

        let _g = root.enter();

        root.set_attribute(KeyValue::new("k4", "v4"));
        root.set_attribute(KeyValue::new("k5", 5));

        let _span = span!("span").entered();
        CurrentSpan::set_attribute(KeyValue::new("k6", "v6"));
        CurrentSpan::set_attribute(KeyValue::new("k7", 7));
    }

    let graph = format_telemetry_tree(exporter.take_messages());
    assert_eq!(
        graph,
        indoc! {r#"
            root [k1="v1", k2=2]
                + attr: k3="v3"
                + attr: k4="v4"
                + attr: k5=5
                span []
                    + attr: k6="v6"
                    + attr: k7=7
        "#}
    );
}

#[test]
#[serial]
fn current_span_integration() {
    use veecle_telemetry::{CurrentSpan, SpanId, TraceId};

    let exporter = set_exporter();

    {
        let context = SpanContext::generate();
        let root = Span::root("root", context, &[]);

        let _guard = root.entered();

        // Test CurrentSpan::event
        CurrentSpan::add_event(
            "test_event",
            &[
                KeyValue::new("event_key", "event_value"),
                KeyValue::new("event_num", 42),
            ],
        );

        // Test CurrentSpan::link
        let external_context =
            SpanContext::new(TraceId(0x123456789ABCDEF0), SpanId(0xFEDCBA9876543210));
        CurrentSpan::add_link(external_context);

        // Test CurrentSpan::attribute
        CurrentSpan::set_attribute(KeyValue::new("runtime_attr", "added_later"));

        // Create a child span to test nested behavior
        let child_span = span!("child", { child_attr = true });
        let _child_guard = child_span.entered();

        // Test CurrentSpan methods on child span
        CurrentSpan::add_event(
            "child_event",
            &[KeyValue::new("child_event_data", "nested")],
        );
        CurrentSpan::set_attribute(KeyValue::new("child_runtime_attr", 100));

        let another_external =
            SpanContext::new(TraceId(0x1111111111111111), SpanId(0x2222222222222222));
        CurrentSpan::add_link(another_external);
    }

    let graph = format_telemetry_tree(exporter.take_messages());
    assert_eq!(
        graph,
        indoc! {r#"
            root []
                + attr: runtime_attr="added_later"
                + link: trace=123456789abcdef0, span=fedcba9876543210
                + event: test_event [event_key="event_value", event_num=42]
                child [child_attr=true]
                    + attr: child_runtime_attr=100
                    + link: trace=1111111111111111, span=2222222222222222
                    + event: child_event [child_event_data="nested"]
        "#}
    );
}

#[test]
#[serial]
fn log_without_span_context() {
    let exporter = set_exporter();

    {
        veecle_telemetry::info!("test message");
    }

    let graph = format_telemetry_tree(exporter.take_messages());
    assert_eq!(
        graph,
        indoc! {"
            + log: [Info] test message []
        "}
    );
}

#[test]
#[serial]
fn log_with_span_context() {
    use veecle_telemetry::{SpanId, TraceId};

    let exporter = set_exporter();

    {
        let trace_id = TraceId(0x123);
        let span_id = SpanId(0);
        let span_context = SpanContext::new(trace_id, span_id);
        let span = Span::root("test_span", span_context, &[]);

        let _guard = span.entered();
        veecle_telemetry::log::log(Severity::Error, "error message", &[]);
    }

    let graph = format_telemetry_tree(exporter.take_messages());
    assert_eq!(
        graph,
        indoc! {"
            test_span []
                + log: [Error] error message []
        "}
    );
}

#[test]
#[serial]
fn log_with_attributes() {
    let exporter = set_exporter();

    {
        veecle_telemetry::debug!("debug message", key1 = "value1", key2 = 42);
        veecle_telemetry::info!("info message");
        veecle_telemetry::warn!("warning message", error_code = 404);
    }

    let graph = format_telemetry_tree(exporter.take_messages());
    assert_eq!(
        graph,
        indoc! {r#"
            + log: [Debug] debug message [key1="value1", key2=42]
            + log: [Info] info message []
            + log: [Warn] warning message [error_code=404]
        "#}
    );
}

#[test]
#[serial]
fn log_attribute_syntax_variations() {
    let exporter = set_exporter();

    {
        let my_var = "test_value";

        // Test identifier only syntax
        veecle_telemetry::log!(Severity::Info, "test identifier", my_var);
    }

    let graph = format_telemetry_tree(exporter.take_messages());
    assert_eq!(
        graph,
        indoc! {r#"
            + log: [Info] test identifier [my_var="test_value"]
        "#}
    );

    // Test literal key syntax
    let exporter = set_exporter();
    {
        veecle_telemetry::log!(Severity::Warn, "test literal key", "literal_key" = 123);
    }

    let graph = format_telemetry_tree(exporter.take_messages());
    assert_eq!(
        graph,
        indoc! {"
            + log: [Warn] test literal key [literal_key=123]
        "}
    );
}