fast-observe 0.1.0

Error faults with causal trees + fastrace-first profiling/log orchestration
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! New-behavior tests: multi-sink fan-out, hook panic containment, and the
//! per-type per-second hook throttle.

use fast_observe::exn::{Attachment, Fault};
use fast_observe::hook::{
    add_capture_hook, clear_error_hooks, hooks_len, set_default_hook_enabled,
};
use fast_observe::profiling::enter_function_scope;
use fast_observe::{add_error_hook, config};
use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use std::sync::Mutex;
use std::sync::atomic::{AtomicUsize, Ordering};

// Serializes tests that mutate the global throttle config or assert on
// hook-fire counts: cargo test runs them on parallel threads, so without
// this they can interleave and flake.
static TEST_LOCK: Mutex<()> = Mutex::new(());

macro_rules! unique_error {
    ($name:ident, $msg:literal) => {
        #[derive(Debug)]
        struct $name;
        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str($msg)
            }
        }
        impl Error for $name {}
    };
}

// ── Multi-sink fan-out ──────────────────────────────────────────────────

unique_error!(FanoutError, "fanout");

#[test]
#[allow(clippy::items_after_statements, clippy::unwrap_used, reason = "test")]
fn multiple_hooks_all_fire() {
    let _serial = TEST_LOCK.lock().unwrap();
    static A: AtomicUsize = AtomicUsize::new(0);
    static B: AtomicUsize = AtomicUsize::new(0);

    add_error_hook(|frame| {
        if frame.type_name().ends_with("FanoutError") {
            A.fetch_add(1, Ordering::Relaxed);
        }
    });
    add_error_hook(|frame| {
        if frame.type_name().ends_with("FanoutError") {
            B.fetch_add(1, Ordering::Relaxed);
        }
    });

    let before_a = A.load(Ordering::Relaxed);
    let before_b = B.load(Ordering::Relaxed);

    let f = Fault::new(FanoutError);
    assert!(f.to_string().contains("fanout"));

    assert_eq!(
        A.load(Ordering::Relaxed) - before_a,
        1,
        "first added hook must fire"
    );
    assert_eq!(
        B.load(Ordering::Relaxed) - before_b,
        1,
        "second added hook must fire"
    );
}

// ── Panic containment ───────────────────────────────────────────────────

unique_error!(PanicError, "panic path");

#[test]
#[allow(
    clippy::panic,
    clippy::manual_assert,
    clippy::items_after_statements,
    clippy::unwrap_used,
    reason = "the test deliberately installs a panicking hook to verify containment; unwrap is test idiom"
)]
fn panicking_hook_is_contained_and_later_hooks_still_run() {
    let _serial = TEST_LOCK.lock().unwrap();
    static AFTER: AtomicUsize = AtomicUsize::new(0);

    add_error_hook(|frame| {
        if frame.type_name().ends_with("PanicError") {
            panic!("deliberate hook panic");
        }
    });
    add_error_hook(|frame| {
        if frame.type_name().ends_with("PanicError") {
            AFTER.fetch_add(1, Ordering::Relaxed);
        }
    });

    let before = AFTER.load(Ordering::Relaxed);
    // Must not propagate the hook's panic.
    let f = Fault::new(PanicError);
    assert!(f.to_string().contains("panic path"));
    assert_eq!(
        AFTER.load(Ordering::Relaxed) - before,
        1,
        "hook registered after the panicking one must still run"
    );
}

// ── Throttle ────────────────────────────────────────────────────────────

unique_error!(ThrottleError, "throttle me");

#[test]
#[allow(clippy::items_after_statements, clippy::unwrap_used, reason = "test")]
fn throttle_caps_identical_type_hooks_per_second() {
    let _serial = TEST_LOCK.lock().unwrap();
    static COUNT: AtomicUsize = AtomicUsize::new(0);

    add_error_hook(|frame| {
        if frame.type_name().ends_with("ThrottleError") {
            COUNT.fetch_add(1, Ordering::Relaxed);
        }
    });

    let cfg = config::config();
    let original = cfg.error_hook_throttle();
    cfg.set_error_hook_throttle(2);

    let before = COUNT.load(Ordering::Relaxed);
    for _ in 0..5 {
        let _f = Fault::new(ThrottleError);
    }
    let fired = COUNT.load(Ordering::Relaxed) - before;

    cfg.set_error_hook_throttle(original);

    assert_eq!(
        fired, 2,
        "throttle limit 2/sec: 5 same-type errors must invoke hooks twice, got {fired}"
    );
}

#[test]
#[allow(clippy::items_after_statements, clippy::unwrap_used, reason = "test")]
fn throttle_zero_is_unlimited() {
    let _serial = TEST_LOCK.lock().unwrap();
    static COUNT: AtomicUsize = AtomicUsize::new(0);

    add_error_hook(|frame| {
        if frame.type_name().ends_with("FanoutError") {
            COUNT.fetch_add(1, Ordering::Relaxed);
        }
    });

    // Default config is unlimited (0). Other tests never raise it above 0
    // without restoring, but set explicitly for determinism.
    let cfg = config::config();
    let original = cfg.error_hook_throttle();
    cfg.set_error_hook_throttle(0);

    let before = COUNT.load(Ordering::Relaxed);
    for _ in 0..4 {
        let _f = Fault::new(FanoutError);
    }
    let fired = COUNT.load(Ordering::Relaxed) - before;

    cfg.set_error_hook_throttle(original);

    assert_eq!(fired, 4, "throttle 0 = unlimited, got {fired}");
}

// ── Hook management API ─────────────────────────────────────────────────

#[test]
#[allow(clippy::items_after_statements, clippy::unwrap_used, reason = "test")]
fn clear_error_hooks_resets_to_default_only() {
    let _serial = TEST_LOCK.lock().unwrap();
    let before = hooks_len();
    add_error_hook(|_| {});
    assert_eq!(
        hooks_len(),
        before + 1,
        "adding a hook grows the count by 1"
    );
    clear_error_hooks();
    assert_eq!(hooks_len(), 1, "clear resets to only the default hook");
}

unique_error!(DefaultToggleError, "default toggle");

#[test]
#[allow(clippy::items_after_statements, clippy::unwrap_used, reason = "test")]
fn default_hook_can_be_disabled_and_reenabled() {
    let _serial = TEST_LOCK.lock().unwrap();
    static COUNT: AtomicUsize = AtomicUsize::new(0);

    add_error_hook(|frame| {
        if frame.type_name().ends_with("DefaultToggleError") {
            COUNT.fetch_add(1, Ordering::Relaxed);
        }
    });

    set_default_hook_enabled(false);
    let before = COUNT.load(Ordering::Relaxed);
    let _f = Fault::new(DefaultToggleError);
    let fired = COUNT.load(Ordering::Relaxed) - before;
    set_default_hook_enabled(true);

    assert_eq!(
        fired, 1,
        "custom hook must still fire while default hook disabled, got {fired}"
    );
}

// ── Capture hooks — run during frame construction, may attach data ────────

unique_error!(CaptureMarkerError, "capture marker");

// Unique newtype for find_attachment — a bare `String` value would be
// ambiguous against the built-in hooks' `String` attachments.
struct Marker;
impl fmt::Display for Marker {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("yes")
    }
}

#[test]
#[allow(clippy::items_after_statements, clippy::unwrap_used, reason = "test")]
fn capture_hooks_attach_data() {
    let _serial = TEST_LOCK.lock().unwrap();

    add_capture_hook(|frame| {
        if frame.type_name().ends_with("CaptureMarkerError") {
            frame.push_attachment(Attachment::with_key("marker", Marker));
        }
    });

    let f = Fault::new(CaptureMarkerError);
    let marker = f.frame().find_attachment::<Marker>();
    assert!(
        marker.is_some(),
        "capture hook attachment must be present on the root frame"
    );
    assert_eq!(
        marker.map(ToString::to_string).as_deref(),
        Some("yes"),
        "attached Marker must display as yes"
    );
}

unique_error!(CapturePanicError, "capture panic");

#[test]
#[allow(
    clippy::panic,
    clippy::manual_assert,
    clippy::items_after_statements,
    clippy::unwrap_used,
    reason = "the test deliberately installs a panicking capture hook to verify containment; unwrap is test idiom"
)]
fn capture_hook_panic_is_contained() {
    let _serial = TEST_LOCK.lock().unwrap();
    static AFTER: AtomicUsize = AtomicUsize::new(0);

    add_capture_hook(|frame| {
        if frame.type_name().ends_with("CapturePanicError") {
            panic!("deliberate capture hook panic");
        }
    });
    add_capture_hook(|frame| {
        if frame.type_name().ends_with("CapturePanicError") {
            AFTER.fetch_add(1, Ordering::Relaxed);
        }
    });

    let before = AFTER.load(Ordering::Relaxed);
    // Construction must succeed despite the panicking capture hook.
    let f = Fault::new(CapturePanicError);
    assert!(f.to_string().contains("capture panic"));
    assert_eq!(
        AFTER.load(Ordering::Relaxed) - before,
        1,
        "capture hook registered after the panicking one must still run"
    );
}

unique_error!(ScopedCaptureError, "scoped capture");

// ── Span-trail breadcrumbs (feature `instant`) ────────────────────────────

#[cfg(feature = "instant")]
unique_error!(BreadcrumbError, "breadcrumb trail");

#[cfg(feature = "instant")]
#[test]
#[allow(
    clippy::items_after_statements,
    clippy::unwrap_used,
    clippy::expect_used,
    reason = "test"
)]
fn breadcrumb_trail_attached_on_fault_with_instant_backend() {
    let _serial = TEST_LOCK.lock().unwrap();
    let cfg = config::config();
    let original = cfg.backends();
    cfg.set_backends(config::Backends::INSTANT);
    fast_observe::profiling::instant::clear();
    // The default hook wraps its log in `scope!("error")`, which would record
    // into FINISHED and skew the post-fault drain count — disable it for the
    // duration of this test (restored below).
    set_default_hook_enabled(false);

    {
        let _outer = fast_observe::scope!("crumb_outer");
        let _inner = fast_observe::scope!("crumb_inner");
    }
    let f = Fault::new(BreadcrumbError);

    set_default_hook_enabled(true);
    cfg.set_backends(original);

    let trail = f
        .frame()
        .attachments()
        .iter()
        .find(|a| a.key() == Some("span_trail"))
        .expect("built-in breadcrumb hook must attach span_trail");
    assert_eq!(
        trail.placement(),
        fast_observe::Placement::Appendix,
        "span_trail must be Appendix (counted, not inlined)"
    );
    let rendered = trail.display();
    assert!(
        rendered.contains("crumb_outer"),
        "trail must name the outer scope: {rendered}"
    );
    assert!(
        rendered.contains("crumb_inner"),
        "trail must name the inner scope: {rendered}"
    );
    // Non-destructive: the breadcrumb peek must not have drained FINISHED.
    assert_eq!(
        fast_observe::profiling::instant::drain().len(),
        2,
        "span_trail capture must leave the accumulator undisturbed"
    );
}

#[cfg(feature = "instant")]
unique_error!(NoBreadcrumbError, "no breadcrumb");

#[cfg(feature = "instant")]
#[test]
#[allow(clippy::items_after_statements, clippy::unwrap_used, reason = "test")]
fn no_breadcrumbs_when_backend_off() {
    let _serial = TEST_LOCK.lock().unwrap();
    let cfg = config::config();
    let original = cfg.backends();
    cfg.set_backends(config::Backends::OFF);
    fast_observe::profiling::instant::clear();

    let f = Fault::new(NoBreadcrumbError);
    cfg.set_backends(original);

    assert!(
        f.frame()
            .attachments()
            .iter()
            .all(|a| a.key() != Some("span_trail")),
        "no span_trail attachment when the instant backend is off"
    );
}

#[cfg(feature = "instant")]
#[test]
#[allow(clippy::items_after_statements, clippy::unwrap_used, reason = "test")]
fn peek_recent_is_non_destructive_and_bounded() {
    let _serial = TEST_LOCK.lock().unwrap();
    use fast_observe::profiling::instant::{clear, drain, enter, peek_recent};
    clear();
    // Static names — `enter` needs &'static str.
    const NAMES: [&str; 10] = ["p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9"];
    for name in NAMES {
        drop(enter(name, None));
    }

    let peeked = peek_recent(3);
    let names: Vec<_> = peeked.iter().map(|s| s.name).collect();
    assert_eq!(
        names,
        vec!["p7", "p8", "p9"],
        "peek_recent(3) must return the 3 newest spans, oldest→newest"
    );
    assert_eq!(
        drain().len(),
        10,
        "peek must be non-destructive — drain still yields all 10 spans"
    );
}

#[test]
#[allow(clippy::items_after_statements, clippy::unwrap_used, reason = "test")]
fn scope_path_attached_when_scoped() {
    let _serial = TEST_LOCK.lock().unwrap();

    let _scope = enter_function_scope(Cow::Borrowed("test_scope"));
    let f = Fault::new(ScopedCaptureError);

    let has_scope_path = f
        .frame()
        .attachments()
        .iter()
        .any(|a| a.key() == Some("scope_path"));
    assert!(
        has_scope_path,
        "built-in scope hook must attach scope_path when a scope is active"
    );
}

// ── Backtrace capture hook (feature `backtrace`) ──────────────────────────

#[cfg(feature = "backtrace")]
unique_error!(BacktraceMarkerError, "backtrace marker");

#[cfg(feature = "backtrace")]
#[test]
#[allow(clippy::items_after_statements, clippy::unwrap_used, reason = "test")]
fn backtrace_attachment_when_env_set() {
    let _serial = TEST_LOCK.lock().unwrap();

    // The env decision (RUST_BACKTRACE / OBSERVE_BACKTRACE) is resolved once
    // in a LazyLock at first use — process-global and possibly already
    // initialized by an earlier test thread, so env manipulation here would
    // be racy (and `set_var` is unsafe on edition 2024). The full decision
    // matrix is unit-tested against the pure `backtrace_enabled` fn in
    // src/hook.rs. This integration test asserts only deterministic
    // properties: construction must not panic, and any `backtrace`
    // attachment must be Appendix-placed (counted, not inlined by the
    // report).
    let f = Fault::new(BacktraceMarkerError);
    assert!(f.to_string().contains("backtrace marker"));
    for attachment in f.frame().attachments() {
        if attachment.key() == Some("backtrace") {
            assert_eq!(
                attachment.placement(),
                fast_observe::Placement::Appendix,
                "backtrace attachment must be Appendix (counted, not inlined)"
            );
        }
    }
}