diff --git a/autopush-common/Cargo.toml b/autopush-common/Cargo.toml index f65764ff..4ad80eec 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 ef306ede..4e1634bc 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))