Function create_traced_handler

Source
pub fn create_traced_handler<T, R, F, Fut>(
    name: &'static str,
    completion_handler: TelemetryCompletionHandler,
    handler_fn: F,
) -> TracedHandler<T, R>
where T: SpanAttributesExtractor + DeserializeOwned + Serialize + Send + 'static, R: Serialize + Send + 'static, F: Fn(LambdaEvent<T>) -> Fut + Send + Sync + Clone + 'static, Fut: Future<Output = Result<R, Error>> + Send + 'static,
Expand description

Creates a traced handler function that can be used directly with service_fn.

This is a convenience wrapper around traced_handler that returns a function suitable for direct use with the Lambda runtime. It provides a more ergonomic interface by allowing handler creation to be separated from usage.

§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, must be Clone to allow reuse across invocations
  • Fut - The future returned by the handler function

§Handler Requirements

The handler function must implement Clone. This is automatically satisfied by:

  • Regular functions (e.g., fn(LambdaEvent<T>) -> Future<...>)
  • Closures that capture only Clone types

For example:

// Regular function - automatically implements Clone
async fn my_handler(event: LambdaEvent<Value>) -> Result<Value, Error> {
    Ok(serde_json::json!({}))
}

// Closure capturing Clone types - implements Clone
let prefix = "my-prefix".to_string();
let handler = |event: LambdaEvent<Value>| async move {
    let prefix = prefix.clone();
    Ok::<Value, Error>(serde_json::json!({ "prefix": prefix }))
};

§Arguments

  • name - Name of the handler/span
  • completion_handler - Handler for managing span export
  • handler_fn - The actual Lambda handler function to wrap

§Returns

Returns a boxed function that can be used directly with service_fn

§Examples

use lambda_runtime::{Error, LambdaEvent};
use serde_json::Value;
use lambda_otel_lite::{init_telemetry, create_traced_handler, TelemetryConfig};

async fn my_handler(event: LambdaEvent<Value>) -> Result<Value, Error> {
    let prefix = event.payload.get("prefix").and_then(|p| p.as_str()).unwrap_or("default");
    Ok::<Value, Error>(serde_json::json!({ "prefix": prefix }))
}

    let (_, completion_handler) = init_telemetry(TelemetryConfig::default()).await?;
    let handler = create_traced_handler(
        "my-handler",
        completion_handler,
        my_handler
    );
    // ... use handler with Runtime ...