Skip to main content

saluki_core/
diagnostic.rs

1//! Diagnostics collection.
2
3use std::sync::Arc;
4
5/// A handle for exposing diagnostics from a component.
6///
7///
8/// Components assert a handle where artifact name is suitable, but not guaranteed, to be
9/// used a filename
10///
11/// # Example:
12///
13/// ```ignore
14/// DataspaceRegistry::try_current()?
15///     .assert(
16///         DiagnosticHandle::new("my_artifact.json", move || {
17///             serde_json::to_vec(&my_state).unwrap_or_default()
18///         }),
19///         "diag-my-component",
20///     );
21/// ```
22///
23
24#[derive(Clone)]
25pub struct DiagnosticHandle {
26    artifact_name: String,
27    collect_fn: Arc<dyn Fn() -> Vec<u8> + Send + Sync>,
28}
29
30impl DiagnosticHandle {
31    /// Creates a new handle with the given artifact name and collection closure.
32    /// `collect_fn` must run synchronously and reasonably fast to produce artifact bytes
33    /// as it can delay entire diagnostic response
34    pub fn new(artifact_name: impl Into<String>, collect_fn: impl Fn() -> Vec<u8> + Send + Sync + 'static) -> Self {
35        Self {
36            artifact_name: artifact_name.into(),
37            collect_fn: Arc::new(collect_fn),
38        }
39    }
40
41    /// Returns the artifact name.
42    pub fn artifact_name(&self) -> &str {
43        &self.artifact_name
44    }
45
46    /// Collects and returns the artifact bytes.
47    pub fn collect(&self) -> Vec<u8> {
48        (self.collect_fn)()
49    }
50}