ハローラムダハローワールドログ


前の記事では、Ruustでラムダを作成するチュートリアルを始めました.あなたがそれを逃したならば、あなたはここでその記事に追いつくことができますRust Lambda Hello World

ログの追加


私たちの錆コードでさらに多くの前に、いくつかのログを追加しましょう.我々は、利用しているtracing library これは既にラムダ依存関係の多くに統合されています.

Details about the tracing library are out of scope for this article, but you can learn more from the tracing crate docs


インロガー


ロガーを設定するには、追跡対象のライブラリを初期化する必要がありますAWS Lambda Rust Runtime Examples いくつかの詳細については、起動する方法についての例があります.
以下にコードを追加します.
fn init_lambda_tracing() {
    tracing_subscriber::fmt()        
        .with_max_level(tracing::Level::INFO)
        // this needs to be set to false, otherwise ANSI color codes will
        // show up in a confusing manner in CloudWatch logs.
        .with_ansi(false)
        // disabling time is handy because CloudWatch will add the ingestion time.
        .without_time()
        .init();
}
私は小さな関数を作成しましたinit_lambda_tracing() ログを設定するすべての詳細を.この関数はtracing_subscriber これはstdoutにログを出力する.Builderで呼び出された追加のメソッドは、デフォルトのログレベルを“info”に設定し、いくつかのデフォルトを無効にしてラムダ環境でより良いものにします.最後に、追加コードを追加しましたmain() ラムダランタイム設定の前にこの関数をコールするには.

コンパイルの問題を修正


たとえ我々が追加を加えなかったとしてもimport ... 行は、新しいトレースライブラリの依存関係をCargo.toml , 我々のコードが構築する前に.

The reason we did not add import.. statements is that we fully qualified the crate directly in the code with the prefix tracing_subscriber::....


次の2つの依存関係を追加します.tracing , and tracing-subscriber
cargo add tracing

cargo add tracing-subscriber

テストしましょう!


今ログ設定をしているので、ログを追加してみましょう.トレースライブラリは、ログのようなマクロを提供します.debug! , info! , warn! , error!私たちは新しい“info”ログをfunc メソッド.
info!("Going to say hello to {}!", first_name);
また、インポートする必要があります
use tracing::info;
今、我々は機能とテストを更新することができます.

コンパイルと更新


前の記事のように、我々はラムダENVのためにコンパイルして、機能を更新する必要があります.

コンパイル


cargo zigbuild --release --target x86_64-unknown-linux-gnu

既存の関数を更新する


aws lambda update-function-code --function-name rustTest \
  --zip-file fileb://./lambda.zip

テストを実行し、ログをチェックする


ラムダが新しいコードで更新される今、我々は進んで、テストして、我々の新しいログをチェックします.

呼び出す


aws lambda invoke \
  --cli-binary-format raw-in-base64-out \
  --function-name rustTest \
  --payload '{"firstName": "James"}' \
  output.json

CloudWatchログを再度チェックする


START RequestId: 93bcbe8c-4b2e-4af3-b00d-3b4b27083ebe Version: $LATEST
INFO lambda_test: going to say hello to James
END RequestId: 93bcbe8c-4b2e-4af3-b00d-3b4b27083ebe
REPORT RequestId: 93bcbe8c-4b2e-4af3-b00d-3b4b27083ebe  Duration: 1.16 ms   Billed Duration: 33 ms  Memory Size: 128 MB Max Memory Used: 17 MB  Init Duration: 31.20 ms 
XRAY TraceId: 1-622553a3-5644934610a4952626f10403   SegmentId: 3b7b79993cba7894 Sampled: true   
成功!我々は今ログを持っている.
INFO lambda_test: going to say hello to James

ロガーの再構成


動的ログレベルのコードを更新する


ログ処理を行ったのでログレベルを読み込むように再設定しましょう
環境変数から.これは、より柔軟になり、コードを再コンパイルすることなく、開発中とデバッグ中にログレベルを上下に変える機能を提供します.
我々は更新することができますinit_lambda_tracing このように:
fn init_lambda_tracing() {
    tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        // this needs to be set to false, otherwise ANSI color codes will
        // show up in a confusing manner in CloudWatch logs.
        .with_ansi(false)
        // disabling time is handy because CloudWatch will add the ingestion time.
        .without_time()
        .init();
}
ハードコードを置き換えましたset_max_level... with
.with_env_filter(EnvFilter::from_default_env())
これは、
環境変数RUST_LOG . これで、実行中のコードを再コンパイルして更新することなく、ログレベルを動的に変更できます.
また、更新する必要がありますCargo.toml “EnvFilter”機能を利用できるようにするにはtracing_subscriber 図書館.
tracing-subscriber = {version = "0.3.9", features = ["env-filter"]}

機能の設定と更新


コードが更新されたので、再構築し、関数を更新する必要があります
新しい"RUST_LOG" 環境変数.テストのためにログレベルを「デバッグ」に設定しましょう.

コンパイル


cargo zigbuild --release --target x86_64-unknown-linux-gnu

既存の関数を更新する


aws lambda update-function-code --function-name rustTest \
  --zip-file fileb://./lambda.zip

既存の関数の設定を更新する


aws lambda update-function-configuration \
    --function-name  rustTest \
    --environment Variables="{RUST_BACKTRACE=1,RUST_LOG=debug}"

再度起動する


aws lambda invoke \
  --cli-binary-format raw-in-base64-out \
  --function-name rustTest \
  --payload '{"firstName": "James"}' \
  output.json

CloudWatchログを再確認


ログははるかに冗長です、我々は含まれている依存関係から新しいログの多くを見ることができます.
START RequestId: 9ebad5dc-3cee-4302-a0f8-0e592e432f41 Version: $LATEST
DEBUG hyper::client::connect::http: connecting to 127.0.0.1:9001
DEBUG hyper::client::connect::http: connected to 127.0.0.1:9001
DEBUG hyper::proto::h1::io: flushed 109 bytes
DEBUG hyper::proto::h1::io: parsed 7 headers
DEBUG hyper::proto::h1::conn: incoming body is content-length (21 bytes)
DEBUG hyper::proto::h1::conn: incoming body completed
DEBUG hyper::client::pool: pooling idle connection for ("http", 127.0.0.1:9001)
INFO lambda_test: going to say hello to James
DEBUG hyper::client::pool: reuse idle connection for ("http", 127.0.0.1:9001)
DEBUG hyper::proto::h1::io: flushed 198 bytes
DEBUG hyper::proto::h1::io: parsed 3 headers
DEBUG hyper::proto::h1::conn: incoming body is content-length (16 bytes)
DEBUG hyper::client::connect::http: connecting to 127.0.0.1:9001
DEBUG hyper::proto::h1::conn: incoming body completed
DEBUG hyper::client::pool: reuse idle connection for ("http", 127.0.0.1:9001)
DEBUG hyper::proto::h1::io: flushed 109 bytes
END RequestId: 9ebad5dc-3cee-4302-a0f8-0e592e432f41
REPORT RequestId: 9ebad5dc-3cee-4302-a0f8-0e592e432f41  Duration: 1.46 ms   Billed Duration: 38 ms  Memory Size: 128 MB Max Memory Used: 17 MB  Init Duration: 36.11 ms 
XRAY TraceId: 1-622551be-6d58480b496655162937c59c   SegmentId: 03a6c5576dc28250 Sampled: true   
DEBUG hyper::client::connect::http: connected to 127.0.0.1:9001
DEBUG hyper::client::pool: pooling idle connection for ("http", 127.0.0.1:9001)

結論


私たちは、私たちの“Hello World”ラムダにログを追加し、環境変数に基づいてログレベルを動的に変更することができました.トレースライブラリを使用して、この簡単に、コードをまっすぐに前進した.

最後にソースコード


完全なプロジェクトは、私のgithubレポでここにありますhttps://github.com/millerjam/rust_lambda_hello_world

次回


次回は、ユニットテストの追加を見て、最終的に私たちのラムダ実行についてさらに節約するために、X 86をARMに構築することに切り替えます.