Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(influxdb_metrics sink): Fix formatting of timestamps in tests #7036

Merged
merged 2 commits into from Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/sinks/influxdb/metrics.rs
Expand Up @@ -834,7 +834,10 @@ mod integration_tests {
http::HttpClient,
sinks::influxdb::{
metrics::{default_summary_quantiles, InfluxDbConfig, InfluxDbSvc},
test_util::{cleanup_v1, onboarding_v1, onboarding_v2, query_v1, BUCKET, ORG, TOKEN},
test_util::{
cleanup_v1, format_timestamp, onboarding_v1, onboarding_v2, query_v1, BUCKET, ORG,
TOKEN,
},
InfluxDb1Settings, InfluxDb2Settings,
},
tls::{self, TlsOptions},
Expand Down Expand Up @@ -922,11 +925,7 @@ mod integration_tests {
MetricValue::Counter { value } => value,
_ => unreachable!(),
};
let timestamp = metric
.data
.timestamp
.unwrap()
.to_rfc3339_opts(chrono::SecondsFormat::Nanos, true);
let timestamp = format_timestamp(metric.data.timestamp.unwrap());
let res =
query_v1_json(url, &format!("select * from {}..\"{}\"", database, name)).await;

Expand Down
16 changes: 15 additions & 1 deletion src/sinks/influxdb/mod.rs
Expand Up @@ -310,7 +310,7 @@ pub(in crate::sinks) fn encode_uri(
pub mod test_util {
use super::*;
use crate::tls;
use chrono::{offset::TimeZone, Utc};
use chrono::{offset::TimeZone, DateTime, SecondsFormat, Utc};
use std::fs::File;
use std::io::Read;

Expand Down Expand Up @@ -461,6 +461,20 @@ pub mod test_util {
status
);
}

pub(crate) fn format_timestamp(timestamp: DateTime<Utc>) -> String {
strip_timestamp(timestamp.to_rfc3339_opts(SecondsFormat::Nanos, true))
}

// InfluxDB strips off trailing zeros in timestamps in metrics
fn strip_timestamp(timestamp: String) -> String {
let strip_one = || format!("{}Z", &timestamp[..timestamp.len() - 2]);
match timestamp {
_ if timestamp.ends_with("0Z") => strip_timestamp(strip_one()),
_ if timestamp.ends_with(".Z") => strip_one(),
_ => timestamp,
}
}
}

#[cfg(test)]
Expand Down
21 changes: 2 additions & 19 deletions src/sinks/prometheus/remote_write.rs
Expand Up @@ -444,7 +444,7 @@ mod integration_tests {
use crate::{
config::{SinkConfig, SinkContext},
event::metric::MetricValue,
sinks::influxdb::test_util::{cleanup_v1, onboarding_v1, query_v1},
sinks::influxdb::test_util::{cleanup_v1, format_timestamp, onboarding_v1, query_v1},
tls::{self, TlsOptions},
Event,
};
Expand Down Expand Up @@ -511,14 +511,7 @@ mod integration_tests {
for (tag, value) in metric.tags().unwrap() {
assert_eq!(output[&tag[..]], Value::String(value.to_string()));
}
let timestamp = strip_timestamp(
metric
.data
.timestamp
.unwrap()
.format("%Y-%m-%dT%H:%M:%S%.3fZ")
.to_string(),
);
let timestamp = format_timestamp(metric.data.timestamp.unwrap());
assert_eq!(output["time"], Value::String(timestamp));
}

Expand All @@ -531,16 +524,6 @@ mod integration_tests {
serde_json::from_str(&text).expect("error when parsing InfluxDB response JSON")
}

// InfluxDB strips off trailing zeros in
fn strip_timestamp(timestamp: String) -> String {
let strip_one = || format!("{}Z", &timestamp[..timestamp.len() - 2]);
match timestamp {
_ if timestamp.ends_with("0Z") => strip_timestamp(strip_one()),
_ if timestamp.ends_with(".Z") => strip_one(),
_ => timestamp,
}
}

fn decode_metrics(data: &Value) -> Vec<HashMap<String, Value>> {
let data = data.as_object().expect("Data is not an object");
let columns = data["columns"].as_array().expect("Columns is not an array");
Expand Down