Skip to content

Commit 57233e4

Browse files
api-clients-generation-pipeline[bot]ci.datadog-api-spec
andauthored
Add a cost monitor example (#469)
Co-authored-by: ci.datadog-api-spec <[email protected]>
1 parent 21d6090 commit 57233e4

11 files changed

+487
-4
lines changed

.apigentools-info

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
"spec_versions": {
55
"v1": {
66
"apigentools_version": "1.6.6",
7-
"regenerated": "2025-01-17 21:03:19.197216",
8-
"spec_repo_commit": "617655da"
7+
"regenerated": "2025-01-17 21:41:10.807788",
8+
"spec_repo_commit": "27e609f7"
99
},
1010
"v2": {
1111
"apigentools_version": "1.6.6",
12-
"regenerated": "2025-01-17 21:03:19.212316",
13-
"spec_repo_commit": "617655da"
12+
"regenerated": "2025-01-17 21:41:10.823634",
13+
"spec_repo_commit": "27e609f7"
1414
}
1515
}
1616
}

.generator/schemas/v1/openapi.yaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6784,6 +6784,62 @@ components:
67846784
- FIREFOX_LAPTOP_LARGE
67856785
- FIREFOX_TABLET
67866786
- FIREFOX_MOBILE_SMALL
6787+
MonitorFormulaAndFunctionCostAggregator:
6788+
description: Aggregation methods for metric queries.
6789+
enum:
6790+
- avg
6791+
- sum
6792+
- max
6793+
- min
6794+
- last
6795+
- area
6796+
- l2norm
6797+
- percentile
6798+
- stddev
6799+
example: avg
6800+
type: string
6801+
x-enum-varnames:
6802+
- AVG
6803+
- SUM
6804+
- MAX
6805+
- MIN
6806+
- LAST
6807+
- AREA
6808+
- L2NORM
6809+
- PERCENTILE
6810+
- STDDEV
6811+
MonitorFormulaAndFunctionCostDataSource:
6812+
description: Data source for cost queries.
6813+
enum:
6814+
- metrics
6815+
- cloud_cost
6816+
- datadog_usage
6817+
example: cloud_cost
6818+
type: string
6819+
x-enum-varnames:
6820+
- METRICS
6821+
- CLOUD_COST
6822+
- DATADOG_USAGE
6823+
MonitorFormulaAndFunctionCostQueryDefinition:
6824+
description: A formula and functions cost query.
6825+
properties:
6826+
aggregator:
6827+
$ref: '#/components/schemas/MonitorFormulaAndFunctionCostAggregator'
6828+
data_source:
6829+
$ref: '#/components/schemas/MonitorFormulaAndFunctionCostDataSource'
6830+
name:
6831+
description: Name of the query for use in formulas.
6832+
example: query1
6833+
type: string
6834+
query:
6835+
description: The monitor query.
6836+
example: sum:all.cost{*}.rollup(sum, 86400)
6837+
type: string
6838+
required:
6839+
- name
6840+
- data_source
6841+
- query
6842+
type: object
67876843
MonitorFormulaAndFunctionEventAggregation:
67886844
description: Aggregation methods for event platform queries.
67896845
enum:
@@ -6934,6 +6990,7 @@ components:
69346990
description: A formula and function query.
69356991
oneOf:
69366992
- $ref: '#/components/schemas/MonitorFormulaAndFunctionEventQueryDefinition'
6993+
- $ref: '#/components/schemas/MonitorFormulaAndFunctionCostQueryDefinition'
69376994
MonitorGroupSearchResponse:
69386995
description: The response of a monitor group search.
69396996
example:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Create a Cost Monitor returns "OK" response
2+
use datadog_api_client::datadog;
3+
use datadog_api_client::datadogV1::api_monitors::MonitorsAPI;
4+
use datadog_api_client::datadogV1::model::Monitor;
5+
use datadog_api_client::datadogV1::model::MonitorFormulaAndFunctionCostAggregator;
6+
use datadog_api_client::datadogV1::model::MonitorFormulaAndFunctionCostDataSource;
7+
use datadog_api_client::datadogV1::model::MonitorFormulaAndFunctionCostQueryDefinition;
8+
use datadog_api_client::datadogV1::model::MonitorFormulaAndFunctionQueryDefinition;
9+
use datadog_api_client::datadogV1::model::MonitorOptions;
10+
use datadog_api_client::datadogV1::model::MonitorThresholds;
11+
use datadog_api_client::datadogV1::model::MonitorType;
12+
13+
#[tokio::main]
14+
async fn main() {
15+
let body =
16+
Monitor::new(
17+
r#"formula("exclude_null(query1)").last("7d").anomaly(direction="above", threshold=10) >= 5"#.to_string(),
18+
MonitorType::COST_ALERT,
19+
)
20+
.message("some message Notify: @hipchat-channel".to_string())
21+
.name("Example Monitor".to_string())
22+
.options(
23+
MonitorOptions::new()
24+
.include_tags(true)
25+
.thresholds(MonitorThresholds::new().critical(5.0 as f64).warning(Some(3.0 as f64)))
26+
.variables(
27+
vec![
28+
MonitorFormulaAndFunctionQueryDefinition::MonitorFormulaAndFunctionCostQueryDefinition(
29+
Box::new(
30+
MonitorFormulaAndFunctionCostQueryDefinition::new(
31+
MonitorFormulaAndFunctionCostDataSource::CLOUD_COST,
32+
"query1".to_string(),
33+
"sum:aws.cost.net.amortized.shared.resources.allocated{aws_product IN (amplify ,athena, backup, bedrock ) } by {aws_product}.rollup(sum, 86400)".to_string(),
34+
).aggregator(MonitorFormulaAndFunctionCostAggregator::SUM),
35+
),
36+
)
37+
],
38+
),
39+
)
40+
.priority(Some(3))
41+
.tags(vec!["test:examplemonitor".to_string(), "env:ci".to_string()]);
42+
let configuration = datadog::Configuration::new();
43+
let api = MonitorsAPI::with_config(configuration);
44+
let resp = api.create_monitor(body).await;
45+
if let Ok(value) = resp {
46+
println!("{:#?}", value);
47+
} else {
48+
println!("{:#?}", resp.unwrap_err());
49+
}
50+
}

src/datadogV1/model/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,12 @@ pub mod model_monitor_formula_and_function_event_query_group_by_sort;
930930
pub use self::model_monitor_formula_and_function_event_query_group_by_sort::MonitorFormulaAndFunctionEventQueryGroupBySort;
931931
pub mod model_monitor_formula_and_function_event_query_definition_search;
932932
pub use self::model_monitor_formula_and_function_event_query_definition_search::MonitorFormulaAndFunctionEventQueryDefinitionSearch;
933+
pub mod model_monitor_formula_and_function_cost_query_definition;
934+
pub use self::model_monitor_formula_and_function_cost_query_definition::MonitorFormulaAndFunctionCostQueryDefinition;
935+
pub mod model_monitor_formula_and_function_cost_aggregator;
936+
pub use self::model_monitor_formula_and_function_cost_aggregator::MonitorFormulaAndFunctionCostAggregator;
937+
pub mod model_monitor_formula_and_function_cost_data_source;
938+
pub use self::model_monitor_formula_and_function_cost_data_source::MonitorFormulaAndFunctionCostDataSource;
933939
pub mod model_monitor_formula_and_function_query_definition;
934940
pub use self::model_monitor_formula_and_function_query_definition::MonitorFormulaAndFunctionQueryDefinition;
935941
pub mod model_monitor_overall_states;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
2+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
3+
// Copyright 2019-Present Datadog, Inc.
4+
5+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
6+
7+
#[non_exhaustive]
8+
#[derive(Clone, Debug, Eq, PartialEq)]
9+
pub enum MonitorFormulaAndFunctionCostAggregator {
10+
AVG,
11+
SUM,
12+
MAX,
13+
MIN,
14+
LAST,
15+
AREA,
16+
L2NORM,
17+
PERCENTILE,
18+
STDDEV,
19+
UnparsedObject(crate::datadog::UnparsedObject),
20+
}
21+
22+
impl ToString for MonitorFormulaAndFunctionCostAggregator {
23+
fn to_string(&self) -> String {
24+
match self {
25+
Self::AVG => String::from("avg"),
26+
Self::SUM => String::from("sum"),
27+
Self::MAX => String::from("max"),
28+
Self::MIN => String::from("min"),
29+
Self::LAST => String::from("last"),
30+
Self::AREA => String::from("area"),
31+
Self::L2NORM => String::from("l2norm"),
32+
Self::PERCENTILE => String::from("percentile"),
33+
Self::STDDEV => String::from("stddev"),
34+
Self::UnparsedObject(v) => v.value.to_string(),
35+
}
36+
}
37+
}
38+
39+
impl Serialize for MonitorFormulaAndFunctionCostAggregator {
40+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
41+
where
42+
S: Serializer,
43+
{
44+
match self {
45+
Self::UnparsedObject(v) => v.serialize(serializer),
46+
_ => serializer.serialize_str(self.to_string().as_str()),
47+
}
48+
}
49+
}
50+
51+
impl<'de> Deserialize<'de> for MonitorFormulaAndFunctionCostAggregator {
52+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
53+
where
54+
D: Deserializer<'de>,
55+
{
56+
let s: String = String::deserialize(deserializer)?;
57+
Ok(match s.as_str() {
58+
"avg" => Self::AVG,
59+
"sum" => Self::SUM,
60+
"max" => Self::MAX,
61+
"min" => Self::MIN,
62+
"last" => Self::LAST,
63+
"area" => Self::AREA,
64+
"l2norm" => Self::L2NORM,
65+
"percentile" => Self::PERCENTILE,
66+
"stddev" => Self::STDDEV,
67+
_ => Self::UnparsedObject(crate::datadog::UnparsedObject {
68+
value: serde_json::Value::String(s.into()),
69+
}),
70+
})
71+
}
72+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
2+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
3+
// Copyright 2019-Present Datadog, Inc.
4+
5+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
6+
7+
#[non_exhaustive]
8+
#[derive(Clone, Debug, Eq, PartialEq)]
9+
pub enum MonitorFormulaAndFunctionCostDataSource {
10+
METRICS,
11+
CLOUD_COST,
12+
DATADOG_USAGE,
13+
UnparsedObject(crate::datadog::UnparsedObject),
14+
}
15+
16+
impl ToString for MonitorFormulaAndFunctionCostDataSource {
17+
fn to_string(&self) -> String {
18+
match self {
19+
Self::METRICS => String::from("metrics"),
20+
Self::CLOUD_COST => String::from("cloud_cost"),
21+
Self::DATADOG_USAGE => String::from("datadog_usage"),
22+
Self::UnparsedObject(v) => v.value.to_string(),
23+
}
24+
}
25+
}
26+
27+
impl Serialize for MonitorFormulaAndFunctionCostDataSource {
28+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
29+
where
30+
S: Serializer,
31+
{
32+
match self {
33+
Self::UnparsedObject(v) => v.serialize(serializer),
34+
_ => serializer.serialize_str(self.to_string().as_str()),
35+
}
36+
}
37+
}
38+
39+
impl<'de> Deserialize<'de> for MonitorFormulaAndFunctionCostDataSource {
40+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
41+
where
42+
D: Deserializer<'de>,
43+
{
44+
let s: String = String::deserialize(deserializer)?;
45+
Ok(match s.as_str() {
46+
"metrics" => Self::METRICS,
47+
"cloud_cost" => Self::CLOUD_COST,
48+
"datadog_usage" => Self::DATADOG_USAGE,
49+
_ => Self::UnparsedObject(crate::datadog::UnparsedObject {
50+
value: serde_json::Value::String(s.into()),
51+
}),
52+
})
53+
}
54+
}

0 commit comments

Comments
 (0)