pub async fn traced_handler<T, R, F, Fut>(
options: TracedHandlerOptions<T>,
completion_handler: TelemetryCompletionHandler,
handler_fn: F,
) -> Result<R, Error>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 serializableR- The response type that must be serializableF- The handler function typeFut- The future returned by the handler function
§Arguments
options- Configuration options for the traced handlercompletion_handler- Handler for managing span exporthandler_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
}