datadog_api/apis/
metrics.rs

1use crate::{
2    client::DatadogClient,
3    models::{MetricMetadata, MetricsListResponse, MetricsQueryResponse},
4    Result,
5};
6use serde::Serialize;
7
8/// API client for Datadog metrics endpoints.
9pub struct MetricsApi {
10    client: DatadogClient,
11}
12
13impl MetricsApi {
14    /// Creates a new metrics API client.
15    #[must_use]
16    pub const fn new(client: DatadogClient) -> Self {
17        Self { client }
18    }
19
20    /// Query time series data for metrics.
21    ///
22    /// # Errors
23    ///
24    /// Returns an error if the API request fails.
25    pub async fn query_metrics(
26        &self,
27        from: i64,
28        to: i64,
29        query: &str,
30    ) -> Result<MetricsQueryResponse> {
31        #[derive(Serialize)]
32        struct QueryParams<'a> {
33            from: i64,
34            to: i64,
35            query: &'a str,
36        }
37
38        let params = QueryParams { from, to, query };
39
40        self.client.get_with_query("/api/v1/query", &params).await
41    }
42
43    /// List active metrics matching a query.
44    ///
45    /// # Errors
46    ///
47    /// Returns an error if the API request fails.
48    pub async fn list_metrics(&self, query: &str) -> Result<MetricsListResponse> {
49        #[derive(Serialize)]
50        struct QueryParams<'a> {
51            q: &'a str,
52        }
53
54        let params = QueryParams { q: query };
55
56        self.client.get_with_query("/api/v1/metrics", &params).await
57    }
58
59    /// Get metadata for a specific metric.
60    ///
61    /// # Errors
62    ///
63    /// Returns an error if the API request fails.
64    pub async fn get_metric_metadata(&self, metric_name: &str) -> Result<MetricMetadata> {
65        let endpoint = format!("/api/v1/metrics/{metric_name}");
66        self.client.get(&endpoint).await
67    }
68}