saluki_core/diagnostic/collector.rs
1//! On-demand diagnostic artifact collection.
2
3use std::sync::Arc;
4
5/// A named, on-demand producer of diagnostic artifact bytes.
6///
7/// A collector pairs an artifact name with a synchronous closure that produces the artifact's bytes when
8/// invoked. Collectors are registered through a [`DiagnosticsEmitter`][super::DiagnosticsEmitter] and gathered on
9/// demand by whichever subsystem is responsible for assembling diagnostic artifacts.
10///
11/// The artifact name is suitable, but not guaranteed, to be used as a filename.
12#[derive(Clone)]
13pub struct DiagnosticCollector {
14 artifact_name: String,
15 collect_fn: Arc<dyn Fn() -> Vec<u8> + Send + Sync>,
16}
17
18impl DiagnosticCollector {
19 /// Creates a new collector with the given artifact name and collection closure.
20 ///
21 /// `collect_fn` runs synchronously and must return promptly, as it can delay the collection of artifacts for the
22 /// whole system.
23 pub fn new<T>(artifact_name: impl Into<String>, collect_fn: impl Fn() -> T + Send + Sync + 'static) -> Self
24 where
25 T: Into<Vec<u8>>,
26 {
27 Self {
28 artifact_name: artifact_name.into(),
29 collect_fn: Arc::new(move || collect_fn().into()),
30 }
31 }
32
33 /// Returns the artifact name.
34 pub fn artifact_name(&self) -> &str {
35 &self.artifact_name
36 }
37
38 /// Collects and returns the artifact bytes.
39 pub fn collect(&self) -> Vec<u8> {
40 (self.collect_fn)()
41 }
42}