Skip to content

Commit acc57d7

Browse files
fix: update time_unix_nano to utc::now() when incoming ts is 0 or null (#1756)
1 parent 1811926 commit acc57d7

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

‎src/otel/otel_utils.rs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,37 @@ pub fn insert_attributes(map: &mut Map<String, Value>, attributes: &[KeyValue])
204204
}
205205

206206
pub fn convert_epoch_nano_to_timestamp(epoch_ns: i64) -> String {
207+
if epoch_ns == 0 {
208+
return chrono::Utc::now()
209+
.format("%Y-%m-%dT%H:%M:%S%.9fZ")
210+
.to_string();
211+
}
207212
let dt = DateTime::from_timestamp_nanos(epoch_ns).naive_utc();
208213
dt.format("%Y-%m-%dT%H:%M:%S%.9fZ").to_string()
209214
}
215+
216+
#[cfg(test)]
217+
mod tests {
218+
use super::*;
219+
220+
#[test]
221+
fn zero_epoch_uses_current_utc_time() {
222+
let before = chrono::Utc::now();
223+
let timestamp = convert_epoch_nano_to_timestamp(0);
224+
let after = chrono::Utc::now();
225+
let parsed = chrono::DateTime::parse_from_rfc3339(&timestamp)
226+
.expect("generated timestamp must be RFC 3339")
227+
.with_timezone(&chrono::Utc);
228+
229+
assert!(parsed >= before);
230+
assert!(parsed <= after);
231+
}
232+
233+
#[test]
234+
fn nonzero_epoch_is_preserved() {
235+
assert_eq!(
236+
convert_epoch_nano_to_timestamp(1_000_000_000),
237+
"1970-01-01T00:00:01.000000000Z"
238+
);
239+
}
240+
}

0 commit comments

Comments
 (0)