about summary refs log tree commit diff
path: root/ops/journaldriver/src
diff options
context:
space:
mode:
Diffstat (limited to 'ops/journaldriver/src')
-rw-r--r--ops/journaldriver/src/main.rs29
-rw-r--r--ops/journaldriver/src/tests.rs6
2 files changed, 15 insertions, 20 deletions
diff --git a/ops/journaldriver/src/main.rs b/ops/journaldriver/src/main.rs
index fb195aab892f..104df84b6e64 100644
--- a/ops/journaldriver/src/main.rs
+++ b/ops/journaldriver/src/main.rs
@@ -31,13 +31,12 @@
 //! `GOOGLE_APPLICATION_CREDENTIALS`, `GOOGLE_CLOUD_PROJECT` and
 //! `LOG_NAME` environment variables.
 
-use anyhow::{bail, format_err, Context, Result};
-use chrono::offset::LocalResult;
-use chrono::prelude::{DateTime, TimeZone, Utc};
+use anyhow::{bail, Context, Result};
 use lazy_static::lazy_static;
 use log::{debug, error, info, trace};
 use serde::{Deserialize, Serialize};
 use serde_json::{from_str, json, Value};
+use std::convert::TryInto;
 use std::fs::{self, rename, File};
 use std::io::{self, ErrorKind, Read, Write};
 use std::path::PathBuf;
@@ -239,10 +238,8 @@ fn get_metadata_token() -> Result<Token> {
 fn sign_service_account_token(credentials: &Credentials) -> Result<Token> {
     use medallion::{Algorithm, Header, Payload};
 
-    let iat = Utc::now();
-    let exp = iat
-        .checked_add_signed(chrono::Duration::seconds(3600))
-        .ok_or_else(|| format_err!("Failed to calculate token expiry"))?;
+    let iat = time::OffsetDateTime::now_utc();
+    let exp = iat + time::Duration::seconds(3600);
 
     let header = Header {
         alg: Algorithm::RS256,
@@ -255,8 +252,8 @@ fn sign_service_account_token(credentials: &Credentials) -> Result<Token> {
         iss: Some(credentials.client_email.clone()),
         sub: Some(credentials.client_email.clone()),
         aud: Some(LOGGING_SERVICE.to_string()),
-        iat: Some(iat.timestamp() as u64),
-        exp: Some(exp.timestamp() as u64),
+        iat: Some(iat.unix_timestamp().try_into().unwrap()),
+        exp: Some(exp.unix_timestamp().try_into().unwrap()),
         ..Default::default()
     };
 
@@ -338,18 +335,15 @@ fn message_to_payload(message: Option<String>) -> Payload {
 /// Parse errors are dismissed and returned as empty options: There
 /// simply aren't any useful fallback mechanisms other than defaulting
 /// to ingestion time for journaldriver's use-case.
-fn parse_microseconds(input: String) -> Option<DateTime<Utc>> {
+fn parse_microseconds(input: String) -> Option<time::OffsetDateTime> {
     if input.len() != 16 {
         return None;
     }
 
-    let seconds: i64 = (&input[..10]).parse().ok()?;
-    let micros: u32 = (&input[10..]).parse().ok()?;
+    let micros: i128 = input.parse().ok()?;
+    let nanos: i128 = micros * 1000;
 
-    match Utc.timestamp_opt(seconds, micros * 1000) {
-        LocalResult::Single(time) => Some(time),
-        _ => None,
-    }
+    time::OffsetDateTime::from_unix_timestamp_nanos(nanos).ok()
 }
 
 /// Converts a journald log message priority to a
@@ -398,7 +392,8 @@ struct LogEntry {
     labels: Value,
 
     #[serde(skip_serializing_if = "Option::is_none")]
-    timestamp: Option<DateTime<Utc>>,
+    #[serde(with = "time::serde::rfc3339::option")]
+    timestamp: Option<time::OffsetDateTime>,
 
     #[serde(flatten)]
     payload: Payload,
diff --git a/ops/journaldriver/src/tests.rs b/ops/journaldriver/src/tests.rs
index 391d39947101..6f5045d6a5cd 100644
--- a/ops/journaldriver/src/tests.rs
+++ b/ops/journaldriver/src/tests.rs
@@ -1,5 +1,6 @@
 use super::*;
 use serde_json::to_string;
+use time::macros::datetime;
 
 #[test]
 fn test_text_entry_serialization() {
@@ -23,10 +24,9 @@ fn test_text_entry_serialization() {
 
 #[test]
 fn test_timestamped_entry_serialization() {
-    let timestamp: DateTime<Utc> = "1952-10-07T12:00:00Z".to_string().parse().unwrap();
     let entry = LogEntry {
         labels: Value::Null,
-        timestamp: Some(timestamp),
+        timestamp: Some(datetime!(1952-10-07 12:00:00 UTC)),
         payload: Payload::TextPayload {
             text_payload: "test entry".into(),
         },
@@ -125,7 +125,7 @@ fn test_json_no_object() {
 #[test]
 fn test_parse_microseconds() {
     let input: String = "1529175149291187".into();
-    let expected: DateTime<Utc> = "2018-06-16T18:52:29.291187Z".to_string().parse().unwrap();
+    let expected: time::OffsetDateTime = datetime!(2018-06-16 18:52:29.291187 UTC);
 
     assert_eq!(Some(expected), parse_microseconds(input));
 }