Skip to main content

dd_api/
monitors.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::client::Client;
5use crate::error::Result;
6
7pub const MONITOR_PATH: &str = "api/v1/monitor";
8
9/// A monitor as returned by `GET /api/v1/monitor` and `/monitor/{id}`.
10///
11/// Only the fields we render are typed; the rest of the (large) payload is kept
12/// in `extra` so `-o json` stays lossless.
13#[derive(Debug, Clone, Deserialize, Serialize)]
14pub struct Monitor {
15    pub id: i64,
16    #[serde(default)]
17    pub name: Option<String>,
18    /// `OK` | `Alert` | `Warn` | `No Data` | `Skipped` | `Ignored` | …
19    #[serde(default)]
20    pub overall_state: Option<String>,
21    #[serde(default, rename = "type")]
22    pub kind: Option<String>,
23    #[serde(default)]
24    pub tags: Vec<String>,
25    #[serde(default)]
26    pub query: Option<String>,
27    #[serde(default)]
28    pub message: Option<String>,
29    #[serde(flatten)]
30    pub extra: Value,
31}
32
33/// Filters for `monitors_list`, each mapping to a `GET /api/v1/monitor` query
34/// param.
35#[derive(Debug, Clone, Default)]
36pub struct MonitorListFilter {
37    /// Substring match on the monitor name (`name`).
38    pub name: Option<String>,
39    /// Scope tags, e.g. `service:nodejs-worker` (`tags`).
40    pub tags: Option<String>,
41    /// Tags set on the monitor object itself (`monitor_tags`).
42    pub monitor_tags: Option<String>,
43}
44
45impl Client {
46    pub async fn monitors_list(&self, filter: &MonitorListFilter) -> Result<Vec<Monitor>> {
47        let mut query: Vec<(&str, String)> = Vec::new();
48        if let Some(name) = &filter.name {
49            query.push(("name", name.clone()));
50        }
51        if let Some(tags) = &filter.tags {
52            query.push(("tags", tags.clone()));
53        }
54        if let Some(monitor_tags) = &filter.monitor_tags {
55            query.push(("monitor_tags", monitor_tags.clone()));
56        }
57        self.get_json(MONITOR_PATH, &query).await
58    }
59
60    pub async fn monitor_get(&self, id: i64) -> Result<Monitor> {
61        self.get_json(&format!("{MONITOR_PATH}/{id}"), &[]).await
62    }
63}