lambda_otel_lite

Function traced_handler

Source
pub async fn traced_handler<T, R, F, Fut>(
    options: TracedHandlerOptions<T>,
    completion_handler: TelemetryCompletionHandler,
    handler_fn: F,
) -> Result<R, Error>
where T: DeserializeOwned + Serialize + Send + 'static, R: Serialize + Send + 'static, F: FnOnce(LambdaEvent<T>) -> Fut, Fut: Future<Output = Result<R, Error>> + Send,
Expand description

Wraps a Lambda handler function with OpenTelemetry tracing.

This function wraps a Lambda handler function to automatically:

  • Create spans for each invocation
  • Extract and set span attributes from the event
  • Propagate context from incoming requests
  • Track response status codes
  • Signal completion for span export

The wrapper supports both synchronous and asynchronous span processing modes through the completion_handler.

§Type Parameters

  • T - The event payload type that must be deserializable and serializable
  • R - The response type that must be serializable
  • F - The handler function type
  • Fut - The future returned by the handler function

§Arguments

  • options - Configuration options for the traced handler
  • completion_handler - Handler for managing span export
  • handler_fn - The actual Lambda handler function to wrap

§Returns

Returns the result from the handler function

§Example

use lambda_otel_lite::{init_telemetry, traced_handler, TracedHandlerOptions, TelemetryConfig};
use lambda_runtime::{service_fn, Error, LambdaEvent};
use serde_json::Value;

async fn function_handler(event: LambdaEvent<Value>) -> Result<Value, Error> {
    Ok(serde_json::json!({ "statusCode": 200 }))
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let completion_handler = init_telemetry(TelemetryConfig::default()).await?;
     
    let func = service_fn(|event| {
        traced_handler(
            TracedHandlerOptions::default()
                .with_name("my-handler")
                .with_event(event),
            completion_handler.clone(),
            function_handler,
        )
    });

    lambda_runtime::run(func).await
}