Skip to content

Commit

Permalink
adds test for serializing GcpSeries
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonahills committed Feb 13, 2023
1 parent 2b1a1a6 commit 6e5fda8
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/sinks/gcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,45 @@ where
{
serialize_datetime(value.as_ref().expect("always defined"), serializer)
}

#[cfg(test)]
mod tests {
use super::*;

/// Ensures that serialized `GcpSeries` matches the format that GCP expects (https://cloud.google.com/monitoring/api/ref_v3/rest/v3/TimeSeries).
#[test]
fn serialize_gcp_series() {
let end_time = chrono::DateTime::from_utc(chrono::NaiveDate::from_ymd(2023, 2, 14).and_hms(10, 00, 00), chrono::Utc);
let gcp_series = GcpSeries {
time_series: &[GcpSerie {
metric: GcpMetric {
r#type: "custom.googleapis.com/my_namespace/metrics/my_metric".to_string(),
labels: [("my_metric_label".to_string(), "my_metric_label_value".to_string())].into(),
},
resource: GcpResource {
r#type: "my_resource".to_string(),
labels: [("my_resource_label".to_string(), "my_resource_label_value".to_string())].into(),
},
metric_kind: GcpMetricKind::Gauge,
value_type: GcpValueType::Int64,
points: &[GcpPoint {
interval: GcpInterval {
start_time: None,
end_time,
},
value: GcpPointValue {
int64_value: Some(10),
},
}],
}]
};

let serialized = serde_json::to_string(&gcp_series).unwrap();

// Convert to `serde_json::Value` so that field order does not matter.
let value: serde_json::Value = serde_json::from_str(&serialized).unwrap();
let expected: serde_json::Value = serde_json::from_str(r#"{"timeSeries":[{"metric":{"type":"custom.googleapis.com/my_namespace/metrics/my_metric","labels":{"my_metric_label":"my_metric_label_value"}},"resource":{"type":"my_resource","labels":{"my_resource_label":"my_resource_label_value"}},"metricKind":"GAUGE","valueType": "INT64","points":[{"interval":{"endTime":"2023-02-14T10:00:00.000000000Z"},"value":{"int64Value":"10"}}]}]}"#).unwrap();

assert_eq!(value, expected);
}
}

0 comments on commit 6e5fda8

Please sign in to comment.