Skip to content

Commit

Permalink
Move mods to crates
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaspustina committed Jan 4, 2019
1 parent bdf96f8 commit 8ac89af
Show file tree
Hide file tree
Showing 20 changed files with 169 additions and 61 deletions.
47 changes: 43 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[workspace]
members = [
"aws-watchtower"
"aws",
"aws-watchtower",
"bosun",
"testing",
]

9 changes: 4 additions & 5 deletions aws-watchtower/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,27 @@ 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"
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"
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"
Expand Down
2 changes: 1 addition & 1 deletion aws-watchtower/src/config.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
8 changes: 5 additions & 3 deletions aws-watchtower/src/events/asg.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -113,7 +114,7 @@ pub fn handle<T: Bosun>(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 {
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 8 additions & 6 deletions aws-watchtower/src/events/ebs.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -112,7 +114,7 @@ pub fn handle<T: Bosun>(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 {
Expand All @@ -133,7 +135,7 @@ pub fn handle<T: Bosun>(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 }
Expand All @@ -148,10 +150,10 @@ pub fn handle<T: Bosun>(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() {
Expand Down
15 changes: 8 additions & 7 deletions aws-watchtower/src/events/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -37,19 +38,19 @@ pub enum HandleResult {

pub fn handle<T: Bosun>(json: Value, ctx: &Context, config: &FunctionConfig, bosun: &T) -> Result<HandleResult, Error> {
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)?;
let res = handle_event(event, ctx, &config, bosun);

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)?
}
}
Expand Down Expand Up @@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions aws-watchtower/src/events/ping.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,9 +25,10 @@ mod tests {

use spectral::prelude::*;
use serde_json::json;
use testing;

fn setup() {
crate::testing::setup();
testing::setup();
}

#[test]
Expand Down
13 changes: 7 additions & 6 deletions aws-watchtower/src/init.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,39 +44,39 @@ pub fn bosun(config: &FunctionConfig, ctx: &Context) -> Result<impl Bosun, Error

pub fn bosun_metrics<T: Bosun>(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]",
);
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]",
);
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]",
);
bosun.emit_metadata(&metadata)?;

let metadata = Metadata::new(
bosun::METRIC_LAMBDA_INVOCATION_COUNT,
metrics::LAMBDA_INVOCATION_COUNT,
"rate",
"Invocations",
"AWS Lambda function invocation counter",
);
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]",
Expand Down
17 changes: 1 addition & 16 deletions aws-watchtower/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,17 +65,3 @@ fn log_result(res: &Result<impl serde::Serialize, Error>, 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();
});
}
}
7 changes: 7 additions & 0 deletions aws-watchtower/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -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";


Loading

0 comments on commit 8ac89af

Please sign in to comment.