Skip to content

Commit

Permalink
Add tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
maahl committed Nov 5, 2023
1 parent f7150a8 commit e0bcfaa
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 1 deletion.
124 changes: 124 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ lambda_runtime = "0.8.1"
serde = "1.0.164"
serde_json = "1.0.97"
tokio = "1.28.2"
tracing = { version = "0.1.37", features = ["log"] }
tracing-subscriber = { version = "0.3.17", features = ["fmt", "env-filter"] }
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ Tested with Rust 1.73.0, Terraform 1.6.3.

* [The Rust Lambda runtime](https://github.com/awslabs/aws-lambda-rust-runtime)
* [Cargo Lambda](https://www.cargo-lambda.info/)
* [tracing documentation](https://docs.rs/tracing/latest/tracing/)
* [Lambda logging in Rust](https://docs.aws.amazon.com/lambda/latest/dg/rust-logging.html)
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ fn say_hello(name: Option<&str>) -> HelloResponse {
// Wrapper for our core function
// Its role is to extract the relevant info from the incoming event, and convert the
// response to json.
#[tracing::instrument()]
async fn run_lambda(event: LambdaEvent<Value>) -> Result<Value, Error> {
let (event, _context) = event.into_parts();
let (event, context) = event.into_parts();
tracing::info!(event = ?event, context = ?context);

let name = event["name"].as_str();
let result = say_hello(name);
Expand All @@ -30,6 +32,11 @@ async fn run_lambda(event: LambdaEvent<Value>) -> Result<Value, Error> {

#[tokio::main]
async fn main() -> Result<(), Error> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.with_ansi(false) // no colors as they look messed up in Cloudwatch
.init();

lambda_runtime::run(service_fn(run_lambda)).await
}

Expand Down

0 comments on commit e0bcfaa

Please sign in to comment.