Other Crates

Additional experimental crates for AWS Lambda and OpenTelemetry integration.

These crates are experimental and under active development. APIs and features may change.

Overview

Due to the early stage of Rust support for OpenTelemetry in AWS Lambda, we’ve developed several utility crates to fill gaps and provide better integration. These crates are part of the Serverless OTLP Forwarder project but can be used independently.

Available Crates

lambda-lw-http-router

Crates.io docs.rs

A lightweight, type-safe HTTP router for AWS Lambda functions with built-in OpenTelemetry support.

Key features:

  • Zero runtime overhead with compile-time route registration
  • Type-safe route handlers and application state
  • Path parameter extraction
  • Support for API Gateway (v1/v2) and ALB events
  • Automatic OpenTelemetry span creation and attribute injection

GitHub | Documentation | Crates.io

otlp-sigv4-client

Crates.io docs.rs

An HTTP client for OpenTelemetry that adds AWS SigV4 authentication support.

Key features:

  • AWS SigV4 authentication for OTLP endpoints
  • Compatible with AWS X-Ray and Application Signals
  • Support for both reqwest and hyper HTTP clients
  • Automatic credential resolution from environment

GitHub | Documentation | Crates.io

lambda-otel-utils

Crates.io docs.rs

Utilities for integrating OpenTelemetry with AWS Lambda functions.

Key features:

  • Easy setup of TracerProvider and MeterProvider
  • AWS Lambda resource detection
  • Environment variable configuration
  • Flexible subscriber configuration
  • JSON formatting support

GitHub | Documentation | Crates.io

Usage Example

Here’s a simple example using these crates together:

use lambda_otel_utils::{HttpTracerProviderBuilder, OpenTelemetrySubscriberBuilder};
use lambda_lw_http_router::{define_router, route};
use aws_lambda_events::apigw::ApiGatewayV2httpRequest;
use serde_json::{json, Value};
use lambda_runtime::{service_fn, Error, LambdaEvent};

// Define application state
#[derive(Clone)]
struct AppState {}

// Set up the router
define_router!(event = ApiGatewayV2httpRequest, state = AppState);

// Define a route handler
#[route(path = "/hello/{name}")]
async fn handle_hello(ctx: RouteContext) -> Result<Value, Error> {
    // The span is automatically created with the route path
    ctx.set_otel_attribute("user.name", ctx.params.get("name").unwrap());
    Ok(json!({ "message": format!("Hello, {}!", ctx.params.get("name").unwrap()) }))
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    // Initialize OpenTelemetry with AWS Lambda detection
    let tracer_provider = HttpTracerProviderBuilder::default()
        .with_stdout_client()
        .build()?;

    OpenTelemetrySubscriberBuilder::new()
        .with_tracer_provider(tracer_provider)
        .with_env_filter(true)
        .with_json_format(true)
        .init()?;

    // Set up the router
    let state = Arc::new(AppState {});
    let router = Arc::new(RouterBuilder::from_registry().build());
    
    lambda_runtime::run(service_fn(|event: LambdaEvent<ApiGatewayV2httpRequest>| {
        let router = Arc::clone(&router);
        let state = Arc::clone(&state);
        async move { router.handle_request(event, state).await }
    })).await
}

Contributing

These crates are part of the Serverless OTLP Forwarder project. Contributions are welcome! Please feel free to submit issues or pull requests to the main repository.