检查快速入门

本快速入门将引导您了解如何使用组件检查的基础知识。您将学习如何使用特定于语言的库将 Inspect 集成到组件中,以及如何使用 ffx inspect 查看数据。

如需详细了解 Inspect 概念,请参阅 Inspect Codelab

项目设置

如需查看您所选语言的快速入门指南,请参阅下文:

C++

本部分假设您正在编写异步组件,并且组件的某些部分(通常是 main.cc)如下所示:

async::Loop loop(&kAsyncLoopConfigAttachToCurrentThread);
auto context_ = sys::ComponentContext::CreateAndServeOutgoingDirectory();
// ...
loop.Run();

这会设置一个异步循环,创建运行时提供的 ComponentContext 封装句柄,然后在完成一些其他初始化工作后运行该循环。

将 Inspect 库依赖项添加到您的 BUILD.gn 文件中

"//sdk/lib/inspect/component/cpp",
"//sdk/lib/sys/cpp",

添加以下 include

#include <lib/inspect/component/cpp/component.h>

添加以下代码以初始化 Inspect

inspector_ = std::make_unique<inspect::ComponentInspector>(async_get_default_dispatcher(),
                                                           inspect::PublishOptions{});

您现在使用的是“检查”功能!通过将属性附加到根节点,在检查树中创建属性:

// Attach properties to the root node of the tree
inspect::Node& root_node = inspector_->root();
// Important: Hold references to properties and don't let them go out of scope.
auto total_requests = root_node.CreateUint("total_requests", 0);
auto bytes_processed = root_node.CreateUint("bytes_processed", 0);

如需查看您可以尝试的数据类型的完整列表,请参阅支持的数据类型

健康检查

健康检查子系统提供用于检查组件健康状况的标准化指标。您可以使用健康状况节点来报告组件的总体状态:

inspector_->Health().StartingUp();

// ...

inspector_->Health().Ok();

测试

如需测试检查代码,您可以使用 //sdklib/inspect/testing/cpp/inspect.h

#include <fidl/fidl.examples.routing.echo/cpp/fidl.h>
#include <lib/async-loop/cpp/loop.h>
#include <lib/async-loop/default.h>
#include <lib/inspect/cpp/inspect.h>
#include <lib/inspect/testing/cpp/inspect.h>

#include <gtest/gtest.h>
#include <src/lib/testing/loop_fixture/real_loop_fixture.h>

using namespace inspect::testing;

此库包含一组完整的匹配器,用于验证检查树的内容。

// Validate the contents of the tree match
auto hierarchy_result = inspect::ReadFromVmo(inspector_.DuplicateVmo());
ASSERT_TRUE(hierarchy_result.is_ok());
EXPECT_THAT(hierarchy_result.take_value(),
            NodeMatches(AllOf(PropertyList(::testing::UnorderedElementsAre(
                UintIs("bytes_processed", 24), UintIs("total_requests", 2))))));

Rust

本部分假设您正在编写异步组件,并且您的组件(通常是 main.rs)的某些部分类似于以下内容:

async fn main() -> Result<(), Error> {
  // ...
  let mut service_fs = ServiceFs::new();
  // ...
  service_fs.take_and_serve_directory_handle().unwrap();
  service_fs.collect::<()>().await;
  Ok(())
}

将 Inspect 库依赖项添加到您的 BUILD.gn 文件中

"//src/lib/diagnostics/inspect/runtime/rust",
"//src/lib/diagnostics/inspect/rust",

添加以下代码以初始化 Inspect

// This creates the root of an Inspect tree
// The Inspector is a singleton that you can access from any scope
let inspector = fuchsia_inspect::component::inspector();
// This serves the Inspect tree, converting failures into fatal errors
let _inspect_server_task =
    inspect_runtime::publish(inspector, inspect_runtime::PublishOptions::default());

您现在使用的是“检查”功能!通过将属性附加到根节点,在检查树中创建属性:

// Attach properties to the root node of the tree
let root_node = inspector.root();
let total_requests = root_node.create_uint("total_requests", 0);
let bytes_processed = root_node.create_uint("bytes_processed", 0);

如需查看您可以尝试的数据类型的完整列表,请参阅支持的数据类型

健康检查

健康检查子系统提供用于检查组件健康状况的标准化指标。您可以使用健康状况节点来报告组件的总体状态:

fuchsia_inspect::component::health().set_starting_up();

// ...

fuchsia_inspect::component::health().set_ok();

测试

如需测试您的检查代码,您可以使用 assert_data_tree 验证检查树的内容:

// Get a reference to the root node of the Inspect tree
let inspector = fuchsia_inspect::component::inspector();

// ...

// Validate the contents of the tree match
diagnostics_assertions::assert_data_tree!(inspector, root: {
    total_requests: 2u64,
    bytes_processed: 24u64,
});

检查库

现在,您已经拥有 root_node,可以开始构建层次结构了。本部分介绍了一些重要的概念和模式,可帮助您入门。

  • 一个节点可以有任意数量的键值对,称为属性
  • Value 的键始终是 UTF-8 字符串,值可以是以下支持的类型之一。
  • 一个节点可以有任意数量的子节点,这些子节点也是节点。

C++

上述代码可让您访问名为“root”的单个节点。hello_world_property 是包含字符串值(恰当地称为 StringProperty)的属性。

  • 值和节点是在父节点下创建的。

Node 具有每种受支持的值类型的创建者方法。hello_world_property 是使用 CreateStringProperty 创建的。您可以通过调用 root_node.CreateChild("child name") 在根节点下创建子节点。请注意,名称必须始终是 UTF-8 字符串。

  • 值和节点具有严格的所有权语义。

hello_world_property 拥有相应属性。当它被销毁(超出范围)时,底层属性会被删除,并且不再显示在组件的检查输出中。对于子节点也是如此。

如果您要创建的值不需要修改,请使用 ValueList 来保持这些值处于活动状态,直到不再需要它们为止。

  • 检查是尽力而为的。

由于空间限制,Inspect 库可能无法满足 Create 请求。此错误不会显示在您的代码中:您将收到一个方法为免运维的 Node/Property 对象。

  • 模式:将子节点传递给子对象。

为自己的类添加 inspect::Node 实参非常有用。父对象(应拥有自己的 inspect::Node)随后可以在构建子对象时将 CreateChild(...) 的结果传递给子对象:

class Child {
  public:
    Child(inspect::Node my_node) : my_node_(std::move(my_node)) {
      // Create a string that doesn't change, and emplace it in the ValueList
      my_node_.CreateString("version", "1.0", &values_);
      // Create metrics and properties on my_node_.
    }

  private:
    inspect::Node my_node_;
    inspect::StringProperty some_property_;
    inspect::ValueList values_;
    // ... more properties and metrics
};

class Parent {
  public:
    // ...

    void AddChild() {
      // Note: inspect::UniqueName returns a globally unique name with the specified prefix.
      children_.emplace_back(my_node_.CreateChild(inspect::UniqueName("child-")));
    }

  private:
    std::vector<Child> children_;
    inspect::Node my_node_;
};

Rust

Rust 库提供了两种管理节点和属性的方式:创建和记录。

使用 create_* 方法时,属性或节点对象的所有权归调用方所有。当返回的对象被丢弃时,该属性会被移除。 例如:

{
    let property = root.create_int("name", 1);
}

在此示例中,property 超出了作用域,因此系统会调用属性的 drop。读者不会看到此属性。

使用 record_* 方法时,属性的生命周期与父节点相关联。删除节点时,系统会删除记录的属性。

{
    let node = root.create_child("name");
    {
      node.record_uint(2); // no return
    }
    // The uint property will still be visible to readers.
}

在此示例中,与 name 关联的 uint 属性对读者可见,直到父级 node 超出范围。

动态值

本部分介绍了 Inspect 库对在读取时延迟扩充的节点的支持。这些方法接受回调函数,而不是值。读取属性值时,系统会调用回调函数。

C++

C++ 库有两个用于创建动态值的属性创建器:CreateLazyNodeCreateLazyValues

这两种方法都接受一个返回 inspect::Inspector 的 Promise 的回调,唯一的区别在于动态值在树中的存储方式。

root->CreateLazyNode(name, callback) 使用给定的 name 创建 root 的子节点。callback 会返回一个 inspect::Inspector 的 promise,该 promise 的根节点在读取时会拼接到父层次结构中。以下示例显示,存在一个名为“lazy”的子级,该子级具有字符串属性“version”,并且还有一个名为“lazy.”的子级。

root->CreateLazyValues(name, callback) 的工作方式与 root->CreateLazyNode(name, callback) 类似,只不过承诺的根节点上的所有属性和子节点都直接作为值添加到原始 root 中。在此示例的第二个输出中,内部延迟节点不会显示,其值会扁平化为 root 上的属性。

root->CreateLazy{Node,Values}("lazy", [] {
  Inspector a;
  a.GetRoot().CreateString("version", "1.0", &a);
  a.GetRoot().CreateLazy{Node,Values}("lazy", [] {
    Inspector b;
    b.GetRoot().RecordInt("value", 10);
    return fpromise::make_ok_promise(std::move(b));
  }, &a);

  return fpromise::make_ok_promise(std::move(a));
});

输出(CreateLazyNode):

root:
  lazy:
    version = "1.0"
    lazy:
      value = 10

输出(CreateLazyValues):

root:
  value = 10
  version = "1.0"

CreateLazy{Node,Values} 的返回值是拥有所传递回调的 LazyNode。一旦 LazyNode 被销毁,系统就不会再调用该回调。如果您在执行回调的同时销毁 LazyNode,则销毁操作会被阻塞,直到回调返回其 promise。

如果您想动态公开 this 上的属性,只需编写以下代码:

class Employee {
  public:
    Employee(inspect::Node node) : node_(std::move(node)) {
      calls_ = node_.CreateInt("calls", 0);

      // Create a lazy node that populates values on its parent
      // dynamically.
      // Note: The callback will never be called after the LazyNode is
      // destroyed, so it is safe to capture "this."
      lazy_ = node_.CreateLazyValues("lazy", [this] {
        // Create a new Inspector and put any data in it you want.
        inspect::Inspector inspector;

        // Keep track of the number of times this callback is executed.
        // This is safe because the callback is executed without locking
        // any state in the parent node.
        calls_.Add(1);

        // ERROR: You cannot modify the LazyNode from the callback. Doing
        // so may deadlock!
        // lazy_ = ...

        // The value is set to the result of calling a method on "this".
        inspector.GetRoot().RecordInt("performance_score",
                                      this->CalculatePerformance());

        // Callbacks return a fpromise::promise<Inspector>, so return a result
        // promise containing the value we created.
        // You can alternatively return a promise that is completed by
        // some asynchronous task.
        return fpromise::make_ok_promise(std::move(inspector));
      });
    }

  private:
    inspect::Node node_;
    inspect::IntProperty calls_;
    inspect::LazyNode lazy_;
};

Rust

请参阅 C++ 动态值支持,因为类似的概念也适用于 Rust。

示例:

root.create_lazy_{child,values}("lazy", [] {
    async move {
        let inspector = Inspector::default();
        inspector.root().record_string("version", "1.0");
        inspector.root().record_lazy_{node,values}("lazy", || {
            let inspector = Inspector::default();
            inspector.root().record_int("value", 10);
            // `_value`'s drop is called when the function returns, so it will be removed.
            // For these situations `record_` is provided.
            let _value = inspector.root().create_int("gone", 2);
            Ok(inspector)
        });
        Ok(inspector)
    }
    .boxed()
});

Output (create_lazy_node):
root:
  lazy:
    version = "1.0"
    lazy:
      value = 10

Output (create_lazy_values):
root:
  value = 10
  version = "1.0"

字符串引用

C++

节点和属性的名称会自动使用字符串留存。

using inspect::Inspector;

Inspector inspector;

for (int i = 0; i < 100; i++) {
  inspector.GetRoot().CreateChild("child", &inspector);
}

将仅生成一个被引用 100 次的 "child" 副本。

Rust

在 Rust Inspect 中,字符串名称会自动去重。例如,

use fuchsia_inspect::Inspector;

let inspector = Inspector::default();
for _ in 0..100 {
  inspector.root().record_child("child");
}

将仅生成 1 个 "child" 的副本,该副本被引用 100 次。

这样可以为每个子节点节省 16 字节,但共享数据的成本为 32 字节。最终节省了 1568 字节。

事件日志记录和时间戳

虽然 Inspect 属性通常表示瞬时组件状态,但组件通常需要记录历史事件(例如连接尝试、状态转换或错误)。

如需有效地记录滚动事件日志和时间戳,请执行以下操作:

  • 遵循 @time 命名惯例,以便开发者清楚地识别时间戳,并使 Fuchsia 快照查看器 (FSV) 可以解析时间戳。

  • 使用有界列表节点来维护固定容量的 FIFO 事件缓冲区,而不会出现无限制的内存增长。

时间戳属性规范

Inspect 没有专用的时间戳基元类型。相反,时间戳会记录为 64 位整数属性(IntPropertyUintProperty),通常表示纳秒或秒。Fuchsia 快照查看器 (FSV) 不会对时间轴做出任何假设;用户必须根据上下文了解如何解读时间戳。

FSV 会解析名为 @time 或以 @time (<prefix>@time) 结尾的属性键,以呈现简明易懂的日期并计算经过的时长:

  • 事件时间戳 (@time):使用确切的属性键 @time 作为事件或状态变化的主要时间戳。

  • 间隔时间戳(start@timeend@time:在跟踪时间范围内的各个时间戳和时长时,请在属性键(例如 start@timeend@timecreated@timelast_seen@time)上使用 @time 后缀。

有界列表节点

有界列表节点在父节点下维护子节点的循环 FIFO 缓冲区。每个新事件都会添加为子节点,并以自动递增的索引("0""1""2"…)命名。当列表达到最大容量时,创建新条目会自动逐出最旧的条目。

Rust

在 Rust 中,使用 fuchsia-inspect-contrib crate 中的 BoundedListNode。将其与 inspect_log! 宏结合使用,以记录带时间戳的事件并自动注入 @time 属性:

use fuchsia_inspect_contrib::inspect_log;
use fuchsia_inspect_contrib::nodes::BoundedListNode;

// Create a bounded list with a capacity of 10 entries under "events".
let mut events = BoundedListNode::new(root.create_child("events"), 10);

// Log an event using key-value syntax. An "@time" property is recorded
// automatically.
inspect_log!(events, state: "connected", address: 42u64);

// Log an event using block syntax with multiple fields.
inspect_log!(events, {
    state: "disconnected",
    reason: "timeout",
    retry_count: 3u32,
});

// Log optional fields and nested structures.
let peer_id: Option<u64> = Some(1234);
inspect_log!(events, {
    event: "peer_discovered",
    peer_id?: peer_id,
    details: {
        rssi: -45i16,
        channel: 6u8,
    },
});

如需在任何节点(例如开始时间和结束时间)上手动记录时间戳,请使用 NodeTimeExt 扩展特征:

use fuchsia_inspect_contrib::nodes::{BootTimeline, NodeTimeExt};

// Record an "@time" property with the current boot timestamp.
NodeTimeExt::<BootTimeline>::record_time(&node, "@time");

// Record interval timestamps on a node.
node.record_int("start@time", start_instant.into_nanos());
node.record_int("end@time", end_instant.into_nanos());

C++

在 C++ 中,通过包含 <lib/inspect/cpp/bounded_list_node.h> 来使用 inspect::BoundedListNode

#include <lib/inspect/cpp/bounded_list_node.h>
#include <lib/zx/clock.h>

// Create a bounded list with a capacity of 10 entries under "events".
inspect::BoundedListNode events(root.CreateChild("events"), 10);

// Record a timestamped entry using CreateEntry.
events.CreateEntry([](inspect::Node& entry) {
  entry.RecordInt("@time", zx::clock::get_boot().get());
  entry.RecordString("state", "connected");
  entry.RecordUint("address", 42);
});

// Record interval timestamps when measuring operations.
events.CreateEntry([&](inspect::Node& entry) {
  entry.RecordInt("start@time", start_time.get());
  entry.RecordInt("end@time", zx::clock::get_boot().get());
  entry.RecordString("status", "success");
});

输出层次结构

使用 ffx inspect 进行检查时,生成的有界事件日志在层次结构中显示为带索引的子节点:

root:
  events:
    "0":
      "@time" = 123456789012
      address = 42
      state = "connected"
    "1":
      "@time" = 123457890123
      reason = "timeout"
      retry_count = 3
      state = "disconnected"

查看检查数据

您可以使用 ffx inspect 命令查看从组件导出的检查数据。

本部分假定您已通过 SSH 访问正在运行的 Fuchsia 系统,并且已开始运行组件。我们将使用名称 my_component.cm 作为组件清单名称的占位符。

读取检查数据

以下命令会输出系统中运行的所有组件的检查层次结构:

ffx inspect show

使用 ffx inspect list 的输出,您可以指定单个组件(例如 core/network/netstack)作为 ffx inspect show 的输入:

ffx inspect show core/network/netstack

您可以指定多个组件(例如 core/font_providercore/my_component):

ffx inspect show core/font_provider core/my_component

您还可以指定节点和属性值。如需查看所有可能的选择器的列表,请使用 ffx inspect selectors

ffx inspect selectors core/my_component

然后,您可以指定指向节点的选择器作为 ffx inspect show 的输入:

ffx inspect show core/my_component:root/my_node

这会生成一个包含相应节点及其所有子级和嵌套属性的输出:

core/my_component:
  metadata:
    name = root
    component_url = fuchsia-pkg://fuchsia.com/my_package#meta/my_component.cm
    timestamp = 1234567890
  payload:
    root:
      my_node:
        hello = "goodbye"
        world = 2
        a_child:
          test = 4.2

您还可以指定一个指向属性的选择器作为 ffx inspect show 的输入:

ffx inspect show core/my_component:root/my_node:hello

这会生成如下所示的输出:

core/my_component:
  metadata:
    name = root
    component_url = fuchsia-pkg://fuchsia.com/my_package#meta/my_component.cm
    timestamp = 1234567890
  payload:
    root:
      my_node:
        hello = "goodbye"

如果您不知道组件的 moniker,可以传递您认为与组件清单、网址、moniker 等相关的字符串。然后,该工具会对所有组件进行模糊匹配。如果找到多个匹配项,系统会要求您消除歧义;否则,系统会返回您预期的输出。

例如,以下内容可能会返回多个匹配项:

ffx inspect show network

此操作会返回:

Fuzzy matching failed due to too many matches, please re-try with one of these:
bootstrap/boot-drivers:PCI0.bus.00_04_0.00_04_0.virtio-net
core/network
core/network-tun
core/network/dhcpd
core/network/dhcpv6-client
core/network/dns-resolver
core/network/http-client
core/network/netcfg
core/network/netcfg/netcfg-config
core/network/netstack
core/network/netstack/dhcp-client
core/network/reachability

此示例展示了导致单个匹配项的调用:

ffx inspect show feedback

此示例会输出 core/feedback 的检查数据:

使用 JSON 美化打印工具获取完整列表。例如:

use diagnostics_assertions::JsonGetter;
...
    #[fuchsia::test]
    fn my_test() {
        let inspect = fuchsia_inspect::component::inspector();
        ...
        print!("{}", inspect.get_pretty_json());
    }

支持的数据类型

类型 说明 备注
IntProperty 包含有符号 64 位整数的指标。 所有语言
UIntProperty 包含无符号 64 位整数的指标。 在 Dart 中不受支持
DoubleProperty 包含双精度浮点数的指标。 所有语言
BoolProperty 包含双精度浮点数的指标。 所有语言
{Int,Double,Uint}Array 一个指标类型数组,包含各种直方图的类型化封装容器。 与基础指标类型支持的语言相同
StringArray 字符串数组。以 StringReference 表示。 在 Dart 中不受支持。
StringProperty 具有 UTF-8 字符串值的属性。 所有语言
ByteVectorProperty 具有任意字节值的属性。 所有语言
节点 一种节点,指标、属性和更多节点可以嵌套在其下。 所有语言
LazyNode 动态实例化完整的节点树。 C++、Rust