選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

記錄和監控 Rust Lambda 函數

焦點模式
記錄和監控 Rust Lambda 函數 - AWS Lambda

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

注意

Rust 執行期用戶端是實驗性套件。它可能會發生變更,僅用於評估目的。

AWS Lambda 會自動代表您監控 Lambda 函數,並將日誌傳送至 Amazon CloudWatch。您的 Lambda 函數隨附有 CloudWatch Logs 日誌群組,且函數的每一執行個體各有一個日誌串流。Lambda 執行期環境會將每次調用的詳細資訊傳送至日誌串流,並且轉傳來自函數程式碼的日誌及其他輸出。如需詳細資訊,請參閱將 CloudWatch Logs 與 Lambda 搭配使用。此頁面說明如何從 Lambda 函數的程式碼中產生日誌輸出。

建立編寫日誌的函數

若要從您的函式程式碼輸出日誌,可使用寫入到 stdoutstderr 的任何日誌記錄函數,例如 println! 巨集。下列範例使用 println! 在函數處理常式啟動時和完成之前列印訊息。

use lambda_runtime::{service_fn, LambdaEvent, Error}; use serde_json::{json, Value}; async fn handler(event: LambdaEvent<Value>) -> Result<Value, Error> { println!("Rust function invoked"); let payload = event.payload; let first_name = payload["firstName"].as_str().unwrap_or("world"); println!("Rust function responds to {}", &first_name); Ok(json!({ "message": format!("Hello, {first_name}!") })) } #[tokio::main] async fn main() -> Result<(), Error> { lambda_runtime::run(service_fn(handler)).await }

實作帶有追蹤套件的進階日誌記錄

追蹤是檢測 Rust 程式以收集結構化、以事件為基礎的診斷資訊的架構。此架構提供公用程式來自訂日誌記錄輸出層級和格式,例如建立結構化的 JSON 日誌訊息。若要使用此架構,必須在實作函數處理常式之前初始化 subscriber。然後,您可以使用追蹤巨集 (例如 debuginfoerror) 來指定每個案例所需的日誌記錄層級。

範例 – 使用追蹤套件

注意下列事項:

  • tracing_subscriber::fmt().json():包含此選項時,日誌會格式化為 JSON。若要使用此選項,必須將 json 功能包含在 tracing-subscriber 相依項中 (例如,tracing-subscriber = { version = "0.3.11", features = ["json"] })。

  • #[tracing::instrument(skip(event), fields(req_id = %event.context.request_id))]:每次叫用處理常式時,此註釋都會產生一個範圍。此範圍會將請求 ID 新增至每個日誌行。

  • { %first_name }:此建構模組將 first_name 欄位新增到使用該欄位的日誌行。此欄位的值對應具有相同名稱的變數。

use lambda_runtime::{service_fn, Error, LambdaEvent}; use serde_json::{json, Value}; #[tracing::instrument(skip(event), fields(req_id = %event.context.request_id))] async fn handler(event: LambdaEvent<Value>) -> Result<Value, Error> { tracing::info!("Rust function invoked"); let payload = event.payload; let first_name = payload["firstName"].as_str().unwrap_or("world"); tracing::info!({ %first_name }, "Rust function responds to event"); Ok(json!({ "message": format!("Hello, {first_name}!") })) } #[tokio::main] async fn main() -> Result<(), Error> { tracing_subscriber::fmt().json() .with_max_level(tracing::Level::INFO) // this needs to be set to remove duplicated information in the log. .with_current_span(false) // 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() // remove the name of the function from every log entry .with_target(false) .init(); lambda_runtime::run(service_fn(handler)).await }

叫用此 Rust 函數時,其會列印類似於以下內容的兩條日誌行:

{"level":"INFO","fields":{"message":"Rust function invoked"},"spans":[{"req_id":"45daaaa7-1a72-470c-9a62-e79860044bb5","name":"handler"}]} {"level":"INFO","fields":{"message":"Rust function responds to event","first_name":"David"},"spans":[{"req_id":"45daaaa7-1a72-470c-9a62-e79860044bb5","name":"handler"}]}
隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。