about summary refs log tree commit diff
path: root/ops/journaldriver
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-02-18T09·49+0300
committerclbot <clbot@tvl.fyi>2022-02-18T11·52+0000
commit71d6a02ca19857a06bc789f09c885e5cd77f7212 (patch)
treea64cf3e1e38da78b8c6ce0dda2483958545af016 /ops/journaldriver
parentaf512558e695605fdfc1b3ede407a6067e355e30 (diff)
refactor(journaldriver): Use time crate directly instead of chrono r/3856
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 <tazjin@tvl.su>
Reviewed-by: sterni <sternenseemann@systemli.org>
Diffstat (limited to 'ops/journaldriver')
-rw-r--r--ops/journaldriver/Cargo.lock35
-rw-r--r--ops/journaldriver/Cargo.toml2
-rw-r--r--ops/journaldriver/src/main.rs29
-rw-r--r--ops/journaldriver/src/tests.rs6
4 files changed, 47 insertions, 25 deletions
diff --git a/ops/journaldriver/Cargo.lock b/ops/journaldriver/Cargo.lock
index 7e4c7923ef..a758178433 100644
--- a/ops/journaldriver/Cargo.lock
+++ b/ops/journaldriver/Cargo.lock
@@ -94,8 +94,7 @@ dependencies = [
  "libc",
  "num-integer",
  "num-traits",
- "serde",
- "time",
+ "time 0.1.44",
  "winapi",
 ]
 
@@ -112,7 +111,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "80f6044740a4a516b8aac14c140cdf35c1a640b1bd6b98b6224e49143b2f1566"
 dependencies = [
  "percent-encoding 2.1.0",
- "time",
+ "time 0.1.44",
 ]
 
 [[package]]
@@ -227,7 +226,6 @@ name = "journaldriver"
 version = "1.1.0"
 dependencies = [
  "anyhow",
- "chrono",
  "env_logger",
  "lazy_static",
  "log",
@@ -237,6 +235,7 @@ dependencies = [
  "serde_derive",
  "serde_json",
  "systemd",
+ "time 0.3.7",
  "ureq",
 ]
 
@@ -344,6 +343,15 @@ dependencies = [
 ]
 
 [[package]]
+name = "num_threads"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "97ba99ba6393e2c3734791401b66902d981cb03bf190af674ca69949b6d5fb15"
+dependencies = [
+ "libc",
+]
+
+[[package]]
 name = "once_cell"
 version = "1.9.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -597,6 +605,25 @@ dependencies = [
 ]
 
 [[package]]
+name = "time"
+version = "0.3.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "004cbc98f30fa233c61a38bc77e96a9106e65c88f2d3bef182ae952027e5753d"
+dependencies = [
+ "itoa",
+ "libc",
+ "num_threads",
+ "serde",
+ "time-macros",
+]
+
+[[package]]
+name = "time-macros"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "25eb0ca3468fc0acc11828786797f6ef9aa1555e4a211a60d64cc8e4d1be47d6"
+
+[[package]]
 name = "tinyvec"
 version = "1.5.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/ops/journaldriver/Cargo.toml b/ops/journaldriver/Cargo.toml
index 8cd2117726..51efc62bd3 100644
--- a/ops/journaldriver/Cargo.toml
+++ b/ops/journaldriver/Cargo.toml
@@ -7,7 +7,6 @@ edition = "2021"
 
 [dependencies]
 anyhow = "1.0"
-chrono = { version = "0.4", features = [ "serde" ]}
 env_logger = "0.5"
 lazy_static = "1.0"
 log = "0.4"
@@ -16,6 +15,7 @@ serde = "1.0"
 serde_derive = "1.0"
 serde_json = "1.0"
 systemd = "0.3"
+time = { version = "0.3", features = [ "serde-well-known", "macros" ]}
 ureq = { version = "0.6.2", features = [ "json" ]}
 
 [build-dependencies]
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<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 391d399471..6f5045d6a5 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));
 }