Module telemetry

Source
Expand description

Core functionality for OpenTelemetry initialization and configuration in Lambda functions.

This module provides the initialization and configuration components for OpenTelemetry in Lambda:

  • init_telemetry: Main entry point for telemetry setup
  • TelemetryConfig: Configuration builder with environment-based defaults
  • TelemetryCompletionHandler: Controls span export timing based on processing mode

§Architecture

The initialization flow:

  1. Configuration is built from environment and/or builder options
  2. Span processor is created based on processing mode
  3. Resource attributes are detected from Lambda environment
  4. Tracer provider is initialized with the configuration
  5. Completion handler is returned for managing span export

§Environment Configuration

Core environment variables:

  • LAMBDA_EXTENSION_SPAN_PROCESSOR_MODE: “sync” (default), “async”, or “finalize”
  • LAMBDA_SPAN_PROCESSOR_QUEUE_SIZE: Maximum spans in buffer (default: 2048)
  • OTEL_SERVICE_NAME: Override auto-detected service name

§Basic Usage

use lambda_otel_lite::telemetry::{init_telemetry, TelemetryConfig};
use lambda_runtime::Error;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let (_, completion_handler) = init_telemetry(TelemetryConfig::default()).await?;
    Ok(())
}

Custom configuration with custom resource attributes:

use lambda_otel_lite::telemetry::{init_telemetry, TelemetryConfig};
use opentelemetry::KeyValue;
use opentelemetry_sdk::Resource;
use lambda_runtime::Error;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let resource = Resource::builder()
        .with_attributes(vec![
            KeyValue::new("service.version", "1.0.0"),
            KeyValue::new("deployment.environment", "production"),
        ])
        .build();

    let config = TelemetryConfig::builder()
        .resource(resource)
        .build();

    let (_, completion_handler) = init_telemetry(config).await?;
    Ok(())
}

Custom configuration with custom span processor:

use lambda_otel_lite::{init_telemetry, TelemetryConfig};
use opentelemetry_sdk::trace::SimpleSpanProcessor;
use otlp_stdout_span_exporter::OtlpStdoutSpanExporter;
use lambda_runtime::Error;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let config = TelemetryConfig::builder()
        .with_span_processor(SimpleSpanProcessor::new(
            Box::new(OtlpStdoutSpanExporter::default())
        ))
        .enable_fmt_layer(true)
        .build();

    let (_, completion_handler) = init_telemetry(config).await?;
    Ok(())
}

§Environment Variables

The following environment variables affect the configuration:

  • OTEL_SERVICE_NAME: Service name for spans
  • OTEL_RESOURCE_ATTRIBUTES: Additional resource attributes
  • LAMBDA_SPAN_PROCESSOR_QUEUE_SIZE: Span buffer size (default: 2048)
  • OTLP_STDOUT_SPAN_EXPORTER_COMPRESSION_LEVEL: Export compression (default: 6)
  • LAMBDA_TRACING_ENABLE_FMT_LAYER: Enable formatting layer (default: false)
  • LAMBDA_EXTENSION_SPAN_PROCESSOR_MODE: Processing mode (sync/async/finalize)
  • RUST_LOG or AWS_LAMBDA_LOG_LEVEL: Log level configuration

Structs§

TelemetryCompletionHandler
Manages the lifecycle of span export based on the processing mode.
TelemetryConfig
Configuration for OpenTelemetry initialization.
TelemetryConfigBuilder
Use builder syntax to set the inputs and finish with build().

Functions§

init_telemetry
Initialize OpenTelemetry for AWS Lambda with the provided configuration.