Skip to content

Commit

Permalink
Made tracing subscriber init in lambdas a shared Fn, better log format.
Browse files Browse the repository at this point in the history
  • Loading branch information
rimutaka committed Dec 23, 2024
1 parent 37acdbc commit 2fcb294
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 53 deletions.
23 changes: 7 additions & 16 deletions rust/lambdas/client-sync/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,21 @@ use aws_lambda_events::{
lambda_function_urls::{LambdaFunctionUrlRequest, LambdaFunctionUrlResponse},
};
use aws_sdk_dynamodb::Client;
use bookworm_types::{jwt, lambda::USER_BOOKS_TABLE_NAME, Book, AUTH_HEADER, ISBN_URL_PARAM_NAME};
use bookworm_types::{
jwt,
lambda::{init_tracing_subscriber, USER_BOOKS_TABLE_NAME},
Book, AUTH_HEADER, ISBN_URL_PARAM_NAME,
};
use lambda_runtime::{service_fn, Error, LambdaEvent, Runtime};
use tracing::info;
use tracing_subscriber::filter::LevelFilter;

mod book;
mod photo;

#[tokio::main]
async fn main() -> Result<(), Error> {
// required to enable CloudWatch error logging by the runtime
#[cfg(debug_assertions)]
tracing_subscriber::fmt()
.without_time()
.with_max_level(LevelFilter::INFO)
.with_ansi(true)
.init();

#[cfg(not(debug_assertions))]
tracing_subscriber::fmt()
.with_max_level(LevelFilter::INFO)
.with_ansi(false)
.compact()
.init();
// this init is required to enable CloudWatch error logging by the runtime
init_tracing_subscriber();

let func = service_fn(my_handler);
let runtime = Runtime::new(func);
Expand Down
17 changes: 5 additions & 12 deletions rust/lambdas/index-html-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ use aws_lambda_events::{
use lambda_runtime::{service_fn, Error, LambdaEvent, Runtime};
// use serde::{Deserialize, Serialize};
// use serde_json::from_str;
use bookworm_types::google;
use bookworm_types::lambda::init_tracing_subscriber;
use index::get_index_from_s3;
use tracing::{error, info};
use tracing_subscriber::filter::LevelFilter;
use bookworm_types::google;

mod index;

#[tokio::main]
async fn main() -> Result<(), Error> {
// required to enable CloudWatch error logging by the runtime
tracing_subscriber::fmt()
.with_ansi(true)
.without_time()
.with_max_level(LevelFilter::INFO)
.init();
// this init is required to enable CloudWatch error logging by the runtime
init_tracing_subscriber();

let func = service_fn(my_handler);
let runtime = Runtime::new(func);
Expand Down Expand Up @@ -154,10 +150,7 @@ fn replace_with_regex(source: &str, title: &str, description: &str, path: &str)
replaced
} else {
match regex::Regex::new(r#"("og:url"[^>]+content=")([^"]+)"#) {
Ok(v) => v.replace(
&replaced,
["${1}", &["https://bookworm.im", path].concat()].concat(),
),
Ok(v) => v.replace(&replaced, ["${1}", &["https://bookworm.im", path].concat()].concat()),
Err(e) => {
error!("Invalid og:url regex. It's a bug. {:?}", e);
return Err(Error::from("Invalid og:url replacement regex"));
Expand Down
10 changes: 3 additions & 7 deletions rust/lambdas/photo-tracker/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
use aws_lambda_events::s3::{S3Event, S3EventRecord};

use bookworm_types::lambda::init_tracing_subscriber;
use bookworm_types::{USER_PHOTOS_S3_PREFIX, USER_PHOTOS_S3_SUFFIX};
use lambda_runtime::{service_fn, Error, LambdaEvent, Runtime};
use tracing::info;
use tracing_subscriber::filter::LevelFilter;

mod photo;

#[tokio::main]
async fn main() -> Result<(), Error> {
// required to enable CloudWatch error logging by the runtime
tracing_subscriber::fmt()
.without_time()
.with_max_level(LevelFilter::INFO)
.with_ansi(false)
.init();
// this init is required to enable CloudWatch error logging by the runtime
init_tracing_subscriber();

let func = service_fn(my_handler);
let runtime = Runtime::new(func);
Expand Down
10 changes: 3 additions & 7 deletions rust/lambdas/share-handler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@ use aws_lambda_events::{
http::{method::Method, HeaderMap, HeaderValue},
lambda_function_urls::{LambdaFunctionUrlRequest, LambdaFunctionUrlResponse},
};
use bookworm_types::lambda::init_tracing_subscriber;
use bookworm_types::{ISBN_URL_PARAM_NAME, SHARE_ID_URL_PARAM_NAME};
use lambda_runtime::{service_fn, Error, LambdaEvent, Runtime};
use tracing::info;
use tracing_subscriber::filter::LevelFilter;

mod share;

#[tokio::main]
async fn main() -> Result<(), Error> {
// required to enable CloudWatch error logging by the runtime
tracing_subscriber::fmt()
.without_time()
.with_max_level(LevelFilter::INFO)
.with_ansi(false)
.init();
// this init is required to enable CloudWatch error logging by the runtime
init_tracing_subscriber();

let func = service_fn(my_handler);
let runtime = Runtime::new(func);
Expand Down
1 change: 1 addition & 0 deletions rust/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ hex = { workspace = true }
jsonwebtoken = "9.3.0"

tracing = { workspace = true }
tracing-subscriber = { workspace = true }

[dependencies.web-sys]
version = "0.3"
Expand Down
24 changes: 24 additions & 0 deletions rust/types/src/lambda.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(not(target_arch = "wasm32"))]
use tracing_subscriber::filter::LevelFilter;

pub const USER_BOOKS_TABLE_NAME: &str = "user_books";

/// An index for finding the user ID for a particular share ID.
Expand All @@ -24,3 +27,24 @@ pub mod user_books_table_fields {
/// `share` is a reserved keyword in DDB and must be escaped.
pub const SHARE_ID: &str = "share";
}

/// Initializes the tracing subscriber for CloudWatch or local logging.
/// - CloudWatch: compact format with x-ray data at the end
/// - Local: no time, ANSI color
#[cfg(not(target_arch = "wasm32"))]
pub fn init_tracing_subscriber() {
// this init is required to enable CloudWatch error logging by the runtime
#[cfg(debug_assertions)] // a more compact format for local debugging
tracing_subscriber::fmt()
.without_time()
.with_max_level(LevelFilter::INFO)
.with_ansi(true)
.init();

#[cfg(not(debug_assertions))]
tracing_subscriber::fmt() // CloudWatch-friendly format
.with_max_level(LevelFilter::INFO)
.with_ansi(false)
.compact() // puts x-ray data at the end
.init();
}
22 changes: 11 additions & 11 deletions src/wasm-rust/isbn_mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,17 +395,6 @@ async function __wbg_load(module, imports) {
function __wbg_get_imports() {
const imports = {};
imports.wbg = {};
imports.wbg.__wbg_reportprogress_631745a2bb962786 = function() { return logError(function (arg0, arg1) {
let deferred0_0;
let deferred0_1;
try {
deferred0_0 = arg0;
deferred0_1 = arg1;
report_progress(getStringFromWasm0(arg0, arg1));
} finally {
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
}
}, arguments) };
imports.wbg.__wbindgen_error_new = function(arg0, arg1) {
const ret = new Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
Expand Down Expand Up @@ -474,6 +463,17 @@ function __wbg_get_imports() {
const ret = upload_file_return_http_status(getStringFromWasm0(arg0, arg1), takeObject(arg2));
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbg_reportprogress_631745a2bb962786 = function() { return logError(function (arg0, arg1) {
let deferred0_0;
let deferred0_1;
try {
deferred0_0 = arg0;
deferred0_1 = arg1;
report_progress(getStringFromWasm0(arg0, arg1));
} finally {
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
}
}, arguments) };
imports.wbg.__wbindgen_as_number = function(arg0) {
const ret = +getObject(arg0);
return ret;
Expand Down
Binary file modified src/wasm-rust/isbn_mod_bg.wasm
Binary file not shown.

0 comments on commit 2fcb294

Please sign in to comment.