use ddog::prelude::*;
extern crate dotenv;
use dotenv::dotenv;
#[test]
fn post_count_series_explicitly() {
dotenv().ok();
let api_key = dotenv::var("DD_API_KEY").unwrap();
let mut builder = builder::Builder::new();
let mut builder_ref = builder.v2();
if dotenv::var("TRACING_SUBSCRIBER")
.map(|s| s == "true")
.unwrap_or(false)
{
builder_ref = builder_ref.with_subscriber();
}
tokio_test::block_on(async {
let (status, res) = builder_ref
.post_series()
.headers(vec![
("Accept", "application/json"),
("Content-Type", "application/json"),
("DD-API-KEY", &api_key),
])
.body(
r#"{
"series": [{
"metric": "rpc_latency",
"type": 1,
"interval": 100000,
"unit": "count",
"tags": [ "gridfin:" ],
"source_type_name": "gridfin_worker",
"resources": [{
"name": "latency",
"type": "time"
}],
"points": [
{ "timestamp": 1660157680, "value": 10.0 },
{ "timestamp": 1660157680, "value": 5.0 },
{ "timestamp": 1660157680, "value": 15.0 }
],
"metadata": {
"origin": {
"metric_type": 1,
"product": 1,
"service": 1
}
}
}]
}"#,
)
.execute()
.await;
tracing::info!(target: "v2/series", "Response: {:?}", res);
assert_eq!(status, 202);
assert_eq!(res.unwrap(), series::SeriesResponse { errors: vec![] });
});
}
#[test]
fn post_series_invalid_json() {
dotenv().ok();
let api_key = dotenv::var("DD_API_KEY").unwrap();
let mut builder = builder::Builder::new();
let mut builder_ref = builder.v2();
if dotenv::var("TRACING_SUBSCRIBER")
.map(|s| s == "true")
.unwrap_or(false)
{
builder_ref = builder_ref.with_subscriber();
}
tokio_test::block_on(async {
let (status, res) = builder_ref
.post_series()
.headers(vec![
("Accept", "application/json"),
("Content-Type", "application/json"),
("DD-API-KEY", &api_key),
])
.body(
r#"{
"series": [{
"metric": "rpc_latency",
"type": 1,
"source_type_name": "gridfin_worker",
"resources": [{
"name": "latency",
"type": "time"
}],
"points": [
{ "timestamp": 1660157680, "value": 10.0 },
{ "timestamp": 1660157680, "value": 5.0 },
{ "timestamp": 1660157680, "value": 15.0 },
]
}]
}"#,
)
.execute()
.await;
tracing::info!(target: "v2/series", "Request Error: {:?}", res);
assert_eq!(status, reqwest::StatusCode::from_u16(400).unwrap());
});
}
#[test]
fn series_post_fails_invalid_api_key() {
let mut builder = builder::Builder::new();
let mut builder_ref = builder.v2();
if dotenv::var("TRACING_SUBSCRIBER")
.map(|s| s == "true")
.unwrap_or(false)
{
builder_ref = builder_ref.with_subscriber();
}
tokio_test::block_on(async {
let (status, res) = builder_ref
.post_series()
.headers(vec![
("Accept", "application/json"),
("Content-Type", "application/json"),
("DD-API-KEY", "THIS_IS_NOT_A_VALID_API_KEY.JPEG"),
])
.body(
r#"{
"series": [{
"metric": "rpc_latency",
"type": 1,
"source_type_name": "gridfin_worker",
"resources": [{
"name": "latency",
"type": "time"
}],
"points": [
{ "timestamp": 1660157680, "value": 10.0 },
{ "timestamp": 1660157680, "value": 5.0 },
{ "timestamp": 1660157680, "value": 15.0 }
]
}]
}"#,
)
.execute()
.await;
tracing::info!(target: "v2/series", "Unauthorized Error: {:?}", res);
assert_eq!(status, reqwest::StatusCode::from_u16(403).unwrap());
});
}