Skip to content

Commit 5092db1

Browse files
authored
Embedded Metrics Logging
1 parent b536a52 commit 5092db1

File tree

4 files changed

+67
-15
lines changed

4 files changed

+67
-15
lines changed

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
extern crate libc;
2+
mod log;
23
use lazy_static::lazy_static;
34
use libc::c_char;
45
use std::collections::HashMap;
@@ -27,7 +28,7 @@ redhook::hook! {
2728
}
2829
}
2930
if DEBUG {
30-
println!("[crypteia] libcrypteia: initialized using LD_PRELOAD");
31+
log::cloudwatch_metric("lib", "initialized", false, None);
3132
}
3233
});
3334
let env_value = redhook::real!(getenv)(name);
@@ -73,7 +74,7 @@ fn is_env_file() -> bool {
7374
let boo_value = metadata(ENV_FILE).is_ok();
7475
unsafe {
7576
if DEBUG {
76-
println!("[crypteia] libcrypteia: is_env_file() -> {}", boo_value);
77+
log::cloudwatch_metric("lib", "is_env_file", false, None);
7778
}
7879
}
7980
boo_value
@@ -96,7 +97,7 @@ fn read_env_file() {
9697
});
9798
unsafe {
9899
if DEBUG {
99-
println!("[crypteia] libcrypteia: read_env_file()");
100+
log::cloudwatch_metric("lib", "read_env_file", false, None);
100101
}
101102
}
102103
delete_file();
@@ -107,7 +108,7 @@ fn delete_file() {
107108
remove_file(ENV_FILE).unwrap();
108109
unsafe {
109110
if DEBUG {
110-
println!("[crypteia] libcrypteia: delete_file()");
111+
log::cloudwatch_metric("lib", "delete_file", false, None);
111112
}
112113
}
113114
}

src/log.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use serde_json::json;
2+
use std::time::SystemTime;
3+
use std::time::UNIX_EPOCH;
4+
5+
pub fn cloudwatch_metric(dimension: &str, name: &str, error: bool, error_message: Option<String>) {
6+
let timestamp = SystemTime::now()
7+
.duration_since(UNIX_EPOCH)
8+
.unwrap()
9+
.as_millis()
10+
.to_string()
11+
.parse::<u64>()
12+
.unwrap();
13+
let metric = serde_json::to_string(&json!(
14+
{
15+
"_aws": {
16+
"Timestamp": timestamp,
17+
"CloudWatchMetrics": [
18+
{
19+
"Namespace": "Crypteia",
20+
"Dimensions": [["All", dimension]],
21+
"Metrics": [
22+
{ "Name": name, "Unit": "Count"}
23+
]
24+
}
25+
]
26+
},
27+
"All": "all",
28+
dimension: dimension,
29+
name: 1,
30+
"ErrorMessage": error_message.unwrap_or("".to_string()),
31+
}
32+
))
33+
.unwrap();
34+
if error {
35+
eprintln!("{}", metric);
36+
} else {
37+
println!("{}", metric);
38+
}
39+
}

src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod log;
12
mod ssm;
23
use lambda_extension::{service_fn, Error, LambdaEvent, NextEvent};
34
use std::collections::HashMap;
@@ -8,10 +9,10 @@ const ENV_FILE: &str = "/tmp/crypteia.json";
89

910
#[tokio::main]
1011
async fn main() -> Result<(), Error> {
11-
println!("[crypteia] main: Init");
12+
log::cloudwatch_metric("main", "initialized", false, None);
1213
let env_vars: HashMap<String, String> = std::env::vars().collect();
1314
let env_map = ssm::get_envs(env_vars).await.unwrap();
14-
println!("[crypteia] main: Fetched environment variables");
15+
log::cloudwatch_metric("main", "fetched", false, None);
1516
write_envs_to_tmp_json(env_map);
1617
let func = service_fn(parameters_extension);
1718
lambda_extension::run(func).await
@@ -20,7 +21,7 @@ async fn main() -> Result<(), Error> {
2021
async fn parameters_extension(event: LambdaEvent) -> Result<(), Error> {
2122
match event.next {
2223
NextEvent::Shutdown(_e) => {
23-
println!("[crypteia] main: Shutdown");
24+
log::cloudwatch_metric("main", "shutdown", false, None);
2425
}
2526
NextEvent::Invoke(_e) => {}
2627
}

src/ssm.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::log;
12
use anyhow::Result;
23
use futures::future::join_all;
34
use std::collections::HashMap;
@@ -30,9 +31,9 @@ pub async fn get_envs(env_vars: HashMap<String, String>) -> Result<HashMap<Strin
3031
results.insert(key, value);
3132
});
3233
}
33-
Err(error) => eprintln!("[crypteia] ssm: Parameter error {err}", err = error),
34+
Err(error) => log::cloudwatch_metric("ssm", "error", true, Some(error.to_string())),
3435
},
35-
Err(error) => eprintln!("[crypteia] ssm: JoinError {err}", err = error),
36+
Err(error) => log::cloudwatch_metric("ssm", "error", true, Some(error.to_string())),
3637
}
3738
}
3839
Ok(results)
@@ -57,9 +58,14 @@ async fn ssm_get_parameter(
5758
}
5859
}
5960
Err(error) => {
60-
eprintln!(
61-
"[crypteia] ssm: Error calling ssm:GetParameter. Environment variable: {name} Path: {path} Error: {err}",
62-
err = error
61+
log::cloudwatch_metric(
62+
"ssm",
63+
"error",
64+
true,
65+
Some(format!(
66+
"Error calling ssm:GetParameter. Environment variable: {} Path: {} Error: {}",
67+
name, path, error
68+
)),
6369
);
6470
}
6571
}
@@ -100,9 +106,14 @@ async fn ssm_get_parameters_by_path(
100106
token = response.next_token;
101107
}
102108
Err(error) => {
103-
eprintln!(
104-
"[crypteia] ssm: Error calling ssm:GetParametersByPath. Environment variable: {name} Path: {path} Error: {err}",
105-
err = error
109+
log::cloudwatch_metric(
110+
"ssm",
111+
"error",
112+
true,
113+
Some(format!(
114+
"Error calling ssm:GetParametersByPath. Environment variable: {} Path: {} Error: {}",
115+
name, path, error
116+
)),
106117
);
107118
break;
108119
}

0 commit comments

Comments
 (0)