diff --git a/Cargo.lock b/Cargo.lock index 0412fea..1b0bcd2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,11 +64,29 @@ name = "autocfg" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "aws" +version = "0.0.1" +dependencies = [ + "base64 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rusoto_core 0.35.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rusoto_ec2 0.35.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rusoto_kms 0.35.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", + "spectral 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "testing 0.0.1", +] + [[package]] name = "aws-watchtower" version = "0.2.1" dependencies = [ - "base64 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", + "aws 0.0.1", + "bosun 0.0.1", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "clams 0.0.13 (registry+https://github.com/rust-lang/crates.io-index)", "clams-derive 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -79,14 +97,12 @@ dependencies = [ "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)", - "rusoto_core 0.35.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rusoto_ec2 0.35.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rusoto_kms 0.35.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.34 (registry+https://github.com/rust-lang/crates.io-index)", "spectral 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", + "testing 0.0.1", "toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "vergen 3.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -153,6 +169,22 @@ dependencies = [ "byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "bosun" +version = "0.0.1" +dependencies = [ + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "reqwest 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.34 (registry+https://github.com/rust-lang/crates.io-index)", + "spectral 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "testing 0.0.1", +] + [[package]] name = "byte-tools" version = "0.2.0" @@ -1643,6 +1675,13 @@ dependencies = [ "libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "testing" +version = "0.0.1" +dependencies = [ + "env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "textwrap" version = "0.10.0" diff --git a/Cargo.toml b/Cargo.toml index c62de2f..be63b12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,8 @@ [workspace] members = [ - "aws-watchtower" + "aws", + "aws-watchtower", + "bosun", + "testing", ] diff --git a/aws-watchtower/Cargo.toml b/aws-watchtower/Cargo.toml index 257dd13..e12a6ed 100644 --- a/aws-watchtower/Cargo.toml +++ b/aws-watchtower/Cargo.toml @@ -19,10 +19,10 @@ name = "aws_watchtower" path = "src/lib.rs" [dependencies] +aws = { version = "0.0.1", path = "../aws" } clams = "0.0.13" clams-derive = "^0.0.4" -chrono = "0.4.6" -base64 = "0.10" +bosun = { version = "0.0.1", path = "../bosun" } env_logger = "0.6.0" failure = "0.1" failure_derive = "0.1" @@ -30,9 +30,6 @@ lambda_runtime = "0.1.0" lazy_static = "1.2.0" log = "0.4" reqwest = "0.9" -rusoto_core = "0.35" -rusoto_kms = "0.35" -rusoto_ec2 = "0.35" serde = "1" serde_derive = "1" serde_json = "1" @@ -40,7 +37,9 @@ structopt = "0.2" toml = "0.4" [dev-dependencies] +chrono = "0.4.6" spectral = "^0.6" +testing = { version = "0.0.1", path = "../testing" } [build-dependencies] vergen = "3" diff --git a/aws-watchtower/src/config.rs b/aws-watchtower/src/config.rs index 686b2ab..647ec5d 100644 --- a/aws-watchtower/src/config.rs +++ b/aws-watchtower/src/config.rs @@ -1,7 +1,7 @@ use crate::asg_mapping::Mappings; -use crate::aws::kms; use crate::WatchAutoscalingError; +use aws::kms; use clams::config::*; use clams_derive::Config; use failure::{Error, Fail}; diff --git a/aws-watchtower/src/events/asg.rs b/aws-watchtower/src/events/asg.rs index 9b349d2..b6a8271 100644 --- a/aws-watchtower/src/events/asg.rs +++ b/aws-watchtower/src/events/asg.rs @@ -1,8 +1,9 @@ use crate::asg_mapping::Mapping; -use crate::bosun::{self, Bosun, Datum, Silence, Tags}; use crate::config::FunctionConfig; use crate::events::HandleResult; use crate::error::WatchAutoscalingError; +use crate::metrics; +use bosun::{Bosun, Datum, Silence, Tags}; use failure::Error; use lambda_runtime::Context; use log::{debug, info}; @@ -113,7 +114,7 @@ pub fn handle(asg: AutoScalingEvent, _: &Context, config: &FunctionCon .unwrap_or_else(|| "unmapped".to_string()), ); let value = value.to_string(); - let datum = Datum::now(bosun::METRIC_ASG_UP_DOWN, &value, &tags); + let datum = Datum::now(metrics::ASG_UP_DOWN, &value, &tags); bosun.emit_datum(&datum)?; if let AsgLifeCycleEvent::SuccessfulTermination(ref details) = event { @@ -151,9 +152,10 @@ mod tests { use chrono::offset::Utc; use env_logger; use spectral::prelude::*; + use testing; fn setup() { - crate::testing::setup(); + testing::setup(); } fn asg_success_full_termination_event() -> AutoScalingEvent { diff --git a/aws-watchtower/src/events/ebs.rs b/aws-watchtower/src/events/ebs.rs index 7fc730d..eb30644 100644 --- a/aws-watchtower/src/events/ebs.rs +++ b/aws-watchtower/src/events/ebs.rs @@ -1,7 +1,9 @@ -use crate::aws::{self, AwsError}; -use crate::bosun::{self, Bosun, Datum, Tags}; use crate::config::FunctionConfig; use crate::events::HandleResult; +use crate::metrics; + +use aws::{self, AwsError}; +use bosun::{ Bosun, Datum, Tags}; use failure::Error; use lambda_runtime::Context; use log::{debug, info}; @@ -112,7 +114,7 @@ pub fn handle(event: VolumeEvent, _: &Context, _config: &FunctionConfi tags.insert("event".to_string(), event.detail.event.to_string()); tags.insert("result".to_string(), event.detail.result.to_string()); let value = change_value.to_string(); - let datum = Datum::now(bosun::METRIC_EBS_VOLUME_EVENT, &value, &tags); + let datum = Datum::now(metrics::EBS_VOLUME_EVENT, &value, &tags); bosun.emit_datum(&datum)?; let res = if event.detail.event == VolumeEventType::CreateVolume { @@ -133,7 +135,7 @@ pub fn handle(event: VolumeEvent, _: &Context, _config: &FunctionConfi let mut tags = Tags::new(); tags.insert("encrypted".to_string(), volume_info.encrypted.to_string()); let value = value.to_string(); - let datum = Datum::now(bosun::METRIC_EBS_VOLUME_CREATION_RESULT, &value, &tags); + let datum = Datum::now(metrics::EBS_VOLUME_CREATION_RESULT, &value, &tags); bosun.emit_datum(&datum)?; HandleResult::VolumeInfo { volume_info } @@ -148,10 +150,10 @@ pub fn handle(event: VolumeEvent, _: &Context, _config: &FunctionConfi #[cfg(test)] mod tests { use super::*; - use crate::aws::ec2::ebs::VolumeInfo; - use crate::testing; + use aws::ec2::ebs::VolumeInfo; use spectral::prelude::*; + use testing; #[test] fn test_deserialize_create_volume_event_result_is_available() { diff --git a/aws-watchtower/src/events/mod.rs b/aws-watchtower/src/events/mod.rs index 27d3598..877c0c6 100644 --- a/aws-watchtower/src/events/mod.rs +++ b/aws-watchtower/src/events/mod.rs @@ -1,7 +1,8 @@ -use crate::bosun::{self, Bosun, Datum, Tags}; -use crate::aws::ec2::ebs::VolumeInfo; use crate::config::FunctionConfig; use crate::error::WatchAutoscalingError; +use crate::metrics; +use aws::ec2::ebs::VolumeInfo; +use bosun::{Bosun, Datum, Tags}; use failure::{Error, Fail}; use lambda_runtime::Context; use log::debug; @@ -37,7 +38,7 @@ pub enum HandleResult { pub fn handle(json: Value, ctx: &Context, config: &FunctionConfig, bosun: &T) -> Result { let tags = Tags::new(); - let datum = Datum::now(bosun::METRIC_LAMBDA_INVOCATION_COUNT, "1", &tags); + let datum = Datum::now(metrics::LAMBDA_INVOCATION_COUNT, "1", &tags); bosun.emit_datum(&datum)?; let event = parse_event(json)?; @@ -45,11 +46,11 @@ pub fn handle(json: Value, ctx: &Context, config: &FunctionConfig, bos match res { Ok(_) => { - let datum = Datum::now(bosun::METRIC_LAMBDA_INVOCATION_RESULT, "0", &tags); + let datum = Datum::now(metrics::LAMBDA_INVOCATION_RESULT, "0", &tags); bosun.emit_datum(&datum)? } Err(_) => { - let datum = Datum::now(bosun::METRIC_LAMBDA_INVOCATION_RESULT, "1", &tags); + let datum = Datum::now(metrics::LAMBDA_INVOCATION_RESULT, "1", &tags); bosun.emit_datum(&datum)? } } @@ -77,12 +78,12 @@ mod tests { use super::*; use crate::asg_mapping::{Mapping, Mappings}; - use crate::bosun::testing::{BosunCallStats, BosunMockClient}; - use crate::testing::setup; + use bosun::testing::{BosunCallStats, BosunMockClient}; use env_logger; use serde_json::json; use spectral::prelude::*; + use testing::setup; #[test] fn test_handle_ping() { diff --git a/aws-watchtower/src/events/ping.rs b/aws-watchtower/src/events/ping.rs index 3b5417e..984bc98 100644 --- a/aws-watchtower/src/events/ping.rs +++ b/aws-watchtower/src/events/ping.rs @@ -1,6 +1,6 @@ -use crate::bosun::Bosun; use crate::config::FunctionConfig; use crate::events::HandleResult; +use bosun::Bosun; use failure::Error; use lambda_runtime::Context; use log::info; @@ -25,9 +25,10 @@ mod tests { use spectral::prelude::*; use serde_json::json; + use testing; fn setup() { - crate::testing::setup(); + testing::setup(); } #[test] diff --git a/aws-watchtower/src/init.rs b/aws-watchtower/src/init.rs index 578c699..ad66e6a 100644 --- a/aws-watchtower/src/init.rs +++ b/aws-watchtower/src/init.rs @@ -1,6 +1,7 @@ -use crate::bosun::{self, Bosun, BosunClient, Metadata}; use crate::config::{EncryptedFunctionConfig, EnvConfig, FunctionConfig}; use crate::error::WatchAutoscalingError; +use crate::metrics; +use bosun::{Bosun, BosunClient, Metadata}; use clams::config::Config; use failure::Error; use lambda_runtime::Context; @@ -43,7 +44,7 @@ pub fn bosun(config: &FunctionConfig, ctx: &Context) -> Result(bosun: &T) -> Result<(), Error> { let metadata = Metadata::new( - bosun::METRIC_ASG_UP_DOWN, + metrics::ASG_UP_DOWN, "rate", "Scaling", "ASG up and down scaling event [-1 = down scaling, +1 = up scaling]", @@ -51,7 +52,7 @@ pub fn bosun_metrics(bosun: &T) -> Result<(), Error> { bosun.emit_metadata(&metadata)?; let metadata = Metadata::new( - bosun::METRIC_EBS_VOLUME_EVENT, + metrics::EBS_VOLUME_EVENT, "rate", "Change", "Creation or deletion of EBS volumes [-1 = deletion, +1 = creation]", @@ -59,7 +60,7 @@ pub fn bosun_metrics(bosun: &T) -> Result<(), Error> { bosun.emit_metadata(&metadata)?; let metadata = Metadata::new( - bosun::METRIC_EBS_VOLUME_CREATION_RESULT, + metrics::EBS_VOLUME_CREATION_RESULT, "gauge", "Result", "Creation result of EBS volumes [0 = success, 1 = failure]", @@ -67,7 +68,7 @@ pub fn bosun_metrics(bosun: &T) -> Result<(), Error> { bosun.emit_metadata(&metadata)?; let metadata = Metadata::new( - bosun::METRIC_LAMBDA_INVOCATION_COUNT, + metrics::LAMBDA_INVOCATION_COUNT, "rate", "Invocations", "AWS Lambda function invocation counter", @@ -75,7 +76,7 @@ pub fn bosun_metrics(bosun: &T) -> Result<(), Error> { bosun.emit_metadata(&metadata)?; let metadata = Metadata::new( - bosun::METRIC_LAMBDA_INVOCATION_RESULT, + metrics::LAMBDA_INVOCATION_RESULT, "gauge", "Result", "AWS Lambda function invocation result code [0 = success, >0 = failure]", diff --git a/aws-watchtower/src/lib.rs b/aws-watchtower/src/lib.rs index 0e34bd8..5ff7286 100644 --- a/aws-watchtower/src/lib.rs +++ b/aws-watchtower/src/lib.rs @@ -10,12 +10,11 @@ use serde_json::Value; use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT}; mod asg_mapping; -mod bosun; +mod metrics; pub mod config; pub mod error; mod events; mod init; -mod aws; mod lambda; // Use a counter, in case we want to track how often the function gets called before getting cold @@ -66,17 +65,3 @@ fn log_result(res: &Result, ctx: &Context) { lambda_result.log_human(); lambda_result.log_json(); } - -#[cfg(test)] -mod testing { - use std::sync::{Once, ONCE_INIT}; - - pub static INIT: Once = ONCE_INIT; - - /// Setup function that is only run once, even if called multiple times. - pub fn setup() { - INIT.call_once(|| { - env_logger::init(); - }); - } -} diff --git a/aws-watchtower/src/metrics.rs b/aws-watchtower/src/metrics.rs new file mode 100644 index 0000000..a0f62f5 --- /dev/null +++ b/aws-watchtower/src/metrics.rs @@ -0,0 +1,7 @@ +pub static ASG_UP_DOWN: &str = "aws.ec2.asg.scaling.event"; +pub static EBS_VOLUME_EVENT: &str = "aws.ec2.ebs.volume.change.event"; +pub static EBS_VOLUME_CREATION_RESULT: &str = "aws.ec2.ebs.volume.creation.result"; +pub static LAMBDA_INVOCATION_COUNT: &str = "aws.lambda.function.invocation.count"; +pub static LAMBDA_INVOCATION_RESULT: &str = "aws.lambda.function.invocation.result"; + + diff --git a/aws/Cargo.toml b/aws/Cargo.toml new file mode 100644 index 0000000..6462ed7 --- /dev/null +++ b/aws/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "aws" +version = "0.0.1" +authors = ["Lukas Pustina "] + +edition = "2018" + +[lib] +name = "aws" +path = "src/lib.rs" + +[dependencies] +base64 = "0.10" +failure = "0.1" +failure_derive = "0.1" +log = "0.4" +rusoto_core = "0.35" +rusoto_kms = "0.35" +rusoto_ec2 = "0.35" +serde = "1" +serde_derive = "1" + +[dev-dependencies] +spectral = "^0.6" +testing = { version = "0.0.1", path = "../testing" } + diff --git a/aws-watchtower/src/aws/auth.rs b/aws/src/auth.rs similarity index 100% rename from aws-watchtower/src/aws/auth.rs rename to aws/src/auth.rs diff --git a/aws-watchtower/src/aws/ec2.rs b/aws/src/ec2.rs similarity index 98% rename from aws-watchtower/src/aws/ec2.rs rename to aws/src/ec2.rs index dcbeda1..bbbe1c7 100644 --- a/aws-watchtower/src/aws/ec2.rs +++ b/aws/src/ec2.rs @@ -1,5 +1,5 @@ pub mod ebs { - use crate::aws::{auth, AwsError}; + use crate::{auth, AwsError}; use failure::Error; use log::debug; use rusoto_core::{HttpClient, Region}; @@ -78,8 +78,8 @@ pub mod ebs { mod tests { use super::*; - use crate::testing; use spectral::prelude::*; + use testing; #[test] fn test_id_from_arn() { diff --git a/aws-watchtower/src/aws/kms.rs b/aws/src/kms.rs similarity index 98% rename from aws-watchtower/src/aws/kms.rs rename to aws/src/kms.rs index 79341b6..ab75eea 100644 --- a/aws-watchtower/src/aws/kms.rs +++ b/aws/src/kms.rs @@ -1,4 +1,4 @@ -use crate::aws::auth; +use crate::auth; use failure::{Error, Fail}; use log::{debug, warn}; use rusoto_core::{HttpClient, Region}; diff --git a/aws-watchtower/src/aws/mod.rs b/aws/src/lib.rs similarity index 100% rename from aws-watchtower/src/aws/mod.rs rename to aws/src/lib.rs diff --git a/bosun/Cargo.toml b/bosun/Cargo.toml new file mode 100644 index 0000000..b7c135b --- /dev/null +++ b/bosun/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "bosun" +version = "0.0.1" +authors = ["Lukas Pustina "] + +edition = "2018" + +[lib] +name = "bosun" +path = "src/lib.rs" + +[dependencies] +chrono = "0.4.6" +failure = "0.1" +failure_derive = "0.1" +log = "0.4" +reqwest = "0.9" +serde = "1" +serde_derive = "1" +serde_json = "1" + +[dev-dependencies] +spectral = "^0.6" +testing = { version = "0.0.1", path = "../testing" } + diff --git a/aws-watchtower/src/bosun.rs b/bosun/src/lib.rs similarity index 96% rename from aws-watchtower/src/bosun.rs rename to bosun/src/lib.rs index f15c40e..7c3e3d1 100644 --- a/aws-watchtower/src/bosun.rs +++ b/bosun/src/lib.rs @@ -7,12 +7,6 @@ use serde_json; use std::collections::HashMap; use std::time::Duration; -pub static METRIC_ASG_UP_DOWN: &str = "aws.ec2.asg.scaling.event"; -pub static METRIC_EBS_VOLUME_EVENT: &str = "aws.ec2.ebs.volume.change.event"; -pub static METRIC_EBS_VOLUME_CREATION_RESULT: &str = "aws.ec2.ebs.volume.creation.result"; -pub static METRIC_LAMBDA_INVOCATION_COUNT: &str = "aws.lambda.function.invocation.count"; -pub static METRIC_LAMBDA_INVOCATION_RESULT: &str = "aws.lambda.function.invocation.result"; - /// Result of an attempt to send meta data or a metric datum pub type BosunResult = Result<(), BosunError>; @@ -347,7 +341,6 @@ impl Silence { } } -#[cfg(test)] pub mod testing { use super::*; diff --git a/testing/Cargo.toml b/testing/Cargo.toml new file mode 100644 index 0000000..84bac2d --- /dev/null +++ b/testing/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "testing" +version = "0.0.1" +authors = ["Lukas Pustina "] + +edition = "2018" + +[lib] +name = "testing" +path = "src/lib.rs" + +[dependencies] +env_logger = "0.6.0" + diff --git a/testing/src/lib.rs b/testing/src/lib.rs new file mode 100644 index 0000000..93e5937 --- /dev/null +++ b/testing/src/lib.rs @@ -0,0 +1,10 @@ +use std::sync::{Once, ONCE_INIT}; + +pub static INIT: Once = ONCE_INIT; + +/// Setup function that is only run once, even if called multiple times. +pub fn setup() { + INIT.call_once(|| { + env_logger::init(); + }); +}