fxfs/
metrics.rs

1// Copyright 2022 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use anyhow::Error;
6use fuchsia_inspect::{Inspector, LazyNode, Node};
7use fuchsia_sync::Mutex;
8use futures::future::BoxFuture;
9use once_cell::sync::Lazy;
10
11/// Root node to which the filesystem Inspect tree will be attached.
12fn root() -> Node {
13    #[cfg(target_os = "fuchsia")]
14    static FXFS_ROOT_NODE: Lazy<Mutex<fuchsia_inspect::Node>> =
15        Lazy::new(|| Mutex::new(fuchsia_inspect::component::inspector().root().clone_weak()));
16    #[cfg(not(target_os = "fuchsia"))]
17    static FXFS_ROOT_NODE: Lazy<Mutex<Node>> = Lazy::new(|| Mutex::new(Node::default()));
18
19    FXFS_ROOT_NODE.lock().clone_weak()
20}
21
22/// `fs.detail` node for holding fxfs-specific metrics.
23pub fn detail() -> Node {
24    static DETAIL_NODE: Lazy<Mutex<Node>> =
25        Lazy::new(|| Mutex::new(root().create_child("fs.detail")));
26
27    DETAIL_NODE.lock().clone_weak()
28}
29
30pub fn register_fs(
31    populate_stores_fn: impl Fn() -> BoxFuture<'static, Result<Inspector, Error>>
32    + Sync
33    + Send
34    + 'static,
35) -> LazyNode {
36    root().create_lazy_child("stores", populate_stores_fn)
37}