From 71d6a02ca19857a06bc789f09c885e5cd77f7212 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 18 Feb 2022 12:49:59 +0300 Subject: refactor(journaldriver): Use time crate directly instead of chrono With this change, we still depend on chrono (through medallion), but but I'm going to try and fix that upstream as well. Change-Id: Iefd3d8578ea8870961107f3222dea7f936c2dd9a Reviewed-on: https://cl.tvl.fyi/c/depot/+/5311 Tested-by: BuildkiteCI Autosubmit: tazjin Reviewed-by: sterni --- ops/journaldriver/src/main.rs | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) (limited to 'ops/journaldriver/src/main.rs') diff --git a/ops/journaldriver/src/main.rs b/ops/journaldriver/src/main.rs index fb195aab89..104df84b6e 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 { fn sign_service_account_token(credentials: &Credentials) -> Result { 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 { 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) -> 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> { +fn parse_microseconds(input: String) -> Option { 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>, + #[serde(with = "time::serde::rfc3339::option")] + timestamp: Option, #[serde(flatten)] payload: Payload, -- cgit 1.4.1