vector/top/
mod.rs

1//! Top subcommand
2mod cmd;
3
4use clap::Parser;
5use glob::Pattern;
6
7pub use cmd::{cmd, top};
8use url::Url;
9use vector_lib::top::state::{FilterColumn, SortColumn};
10
11use crate::config::api::default_graphql_url;
12
13/// Top options
14#[derive(Parser, Debug, Clone)]
15#[command(rename_all = "kebab-case")]
16pub struct Opts {
17    /// Interval to sample metrics at, in milliseconds
18    #[arg(default_value = "1000", short = 'i', long)]
19    interval: u32,
20
21    /// GraphQL API server endpoint
22    #[arg(short, long)]
23    url: Option<Url>,
24
25    /// Humanize metrics, using numeric suffixes - e.g. 1,100 = 1.10 k, 1,000,000 = 1.00 M
26    #[arg(short = 'H', long, default_value_t = true)]
27    human_metrics: bool,
28
29    /// Whether to reconnect if the underlying API connection drops.
30    ///
31    /// By default, top will attempt to reconnect if the connection drops.
32    #[arg(short, long)]
33    no_reconnect: bool,
34
35    /// Components IDs to observe (comma-separated; accepts glob patterns)
36    #[arg(default_value = "*", value_delimiter(','), short = 'c', long)]
37    components: Vec<Pattern>,
38
39    /// Field to sort values to by default (can be changed while running).
40    #[arg(short = 's', long)]
41    sort_field: Option<SortColumn>,
42
43    /// Sort descending instead of ascending.
44    #[arg(long, default_value_t = false)]
45    sort_desc: bool,
46
47    /// Field to filter values by default (can be changed while running).
48    #[arg(default_value = "id", long)]
49    filter_field: FilterColumn,
50
51    /// Filter to apply to the chosen field (ID by default).
52    ///
53    /// This accepts Regex patterns.
54    #[arg(short = 'f', long)]
55    filter_value: Option<String>,
56}
57
58impl Opts {
59    /// Use the provided URL as the Vector GraphQL API server, or default to the local port
60    /// provided by the API config.
61    pub fn url(&self) -> Url {
62        self.url.clone().unwrap_or_else(default_graphql_url)
63    }
64
65    /// URL with scheme set to WebSockets
66    pub fn web_socket_url(&self) -> Url {
67        let mut url = self.url();
68        url.set_scheme(match url.scheme() {
69            "https" => "wss",
70            _ => "ws",
71        })
72        .expect("Couldn't build WebSocket URL. Please report.");
73
74        url
75    }
76}