sl4f_lib/diagnostics/
facade.rs

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
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::diagnostics::types::SnapshotInspectArgs;
use anyhow::Error;
use diagnostics_reader::{ArchiveReader, Inspect, RetryConfig};
use fidl_fuchsia_diagnostics::ArchiveAccessorMarker;
use fuchsia_component::client;
use serde_json::Value;

// Give components 5 minutes to respond with their Inspect data in case the system is under heavy
// load. This can happen especially when running unoptimized on emulators.
const BATCH_RETRIEVAL_TIMEOUT_SECONDS: i64 = 300;

/// Facade providing access to diagnostics interface.
#[derive(Debug)]
pub struct DiagnosticsFacade {}

impl DiagnosticsFacade {
    pub fn new() -> DiagnosticsFacade {
        DiagnosticsFacade {}
    }

    pub async fn snapshot_inspect(&self, args: SnapshotInspectArgs) -> Result<Value, Error> {
        let service_path = format!("/svc/{}", args.service_name);
        let proxy =
            client::connect_to_protocol_at_path::<ArchiveAccessorMarker>(&service_path).unwrap();
        let value = ArchiveReader::new()
            .retry(RetryConfig::never())
            .with_archive(proxy)
            .add_selectors(args.selectors.into_iter())
            .with_batch_retrieval_timeout_seconds(BATCH_RETRIEVAL_TIMEOUT_SECONDS)
            .snapshot_raw::<Inspect, serde_json::Value>()
            .await?;
        Ok(value)
    }
}