From 04df79e5df9899fc0563d116187fbd59e0a53eee Mon Sep 17 00:00:00 2001 From: JR Conlin Date: Wed, 7 Feb 2024 19:05:04 -0800 Subject: [PATCH] feat: put old AWS crap behind a feature flag (#606) Co-authored-by: Philip Jenvey --- autopush-common/Cargo.toml | 1 + autopush-common/src/logging.rs | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/autopush-common/Cargo.toml b/autopush-common/Cargo.toml index f65764ff3..4ad80eeca 100644 --- a/autopush-common/Cargo.toml +++ b/autopush-common/Cargo.toml @@ -80,6 +80,7 @@ actix-rt = "2.8" bigtable = ["dep:google-cloud-rust-raw", "dep:grpcio", "dep:protobuf"] dynamodb = ["dep:rusoto_core", "dep:rusoto_credential", "dep:rusoto_dynamodb"] dual = ["dynamodb", "bigtable"] +aws = [] emulator = [ "bigtable", ] # used for testing big table, requires an external bigtable emulator running. diff --git a/autopush-common/src/logging.rs b/autopush-common/src/logging.rs index ef306edeb..4e1634bcd 100644 --- a/autopush-common/src/logging.rs +++ b/autopush-common/src/logging.rs @@ -1,4 +1,6 @@ -use std::{io, sync::OnceLock, time::Duration}; +use std::io; +#[cfg(feature = "aws")] +use std::{sync::OnceLock, time::Duration}; use gethostname::gethostname; use slog::{self, Drain}; @@ -6,14 +8,18 @@ use slog_mozlog_json::MozLogJson; use crate::errors::Result; +#[cfg(feature = "aws")] static EC2_INSTANCE_ID: OnceLock> = OnceLock::new(); pub fn init_logging(json: bool, name: &str, version: &str) -> Result<()> { let logger = if json { - let ec2_instance_id = EC2_INSTANCE_ID.get_or_init(|| get_ec2_instance_id().ok()); - let hostname = ec2_instance_id - .clone() - .unwrap_or_else(|| gethostname().to_string_lossy().to_string()); + let hostname = { + #[cfg(feature = "aws")] + let result = ec2_instance_id(); + #[cfg(not(feature = "aws"))] + let result = gethostname().to_string_lossy().to_string(); + result + }; let drain = MozLogJson::new(io::stdout()) .logger_name(format!("{}-{}", name, version)) .msg_type(format!("{}:log", name)) @@ -56,9 +62,17 @@ pub fn init_test_logging() { slog_stdlog::init().ok(); } +#[cfg(feature = "aws")] +fn ec2_instance_id() -> String { + let ec2_instance_id = EC2_INSTANCE_ID.get_or_init(|| get_ec2_instance_id().ok()); + ec2_instance_id + .clone() + .unwrap_or_else(|| gethostname().to_string_lossy().to_string()) +} /// Fetch the EC2 instance-id /// /// NOTE: This issues a blocking web request +#[cfg(feature = "aws")] fn get_ec2_instance_id() -> reqwest::Result { let client = reqwest::blocking::Client::builder() .timeout(Duration::from_secs(1))