Function get_lambda_resource

Source
pub fn get_lambda_resource() -> Resource
Expand description

Get default Lambda resource attributes.

This function automatically detects and sets standard Lambda attributes from environment variables and allows for custom attribute configuration through OTEL_RESOURCE_ATTRIBUTES.

§Environment Variables

  • AWS_REGION: Sets cloud.region
  • AWS_LAMBDA_FUNCTION_NAME: Sets faas.name and default service.name
  • AWS_LAMBDA_FUNCTION_VERSION: Sets faas.version
  • AWS_LAMBDA_FUNCTION_MEMORY_SIZE: Sets faas.max_memory
  • AWS_LAMBDA_LOG_STREAM_NAME: Sets faas.instance
  • OTEL_SERVICE_NAME: Overrides default service name
  • OTEL_RESOURCE_ATTRIBUTES: Additional attributes in key=value format

§Configuration Attributes

The following configuration attributes are set in the resource only when the corresponding environment variables are explicitly set:

  • LAMBDA_EXTENSION_SPAN_PROCESSOR_MODE: Sets lambda_otel_lite.extension.span_processor_mode
  • LAMBDA_SPAN_PROCESSOR_QUEUE_SIZE: Sets lambda_otel_lite.lambda_span_processor.queue_size
  • LAMBDA_SPAN_PROCESSOR_BATCH_SIZE: Sets lambda_otel_lite.lambda_span_processor.batch_size
  • OTLP_STDOUT_SPAN_EXPORTER_COMPRESSION_LEVEL: Sets lambda_otel_lite.otlp_stdout_span_exporter.compression_level

§Returns

Returns a Resource containing all detected and configured attributes.

§Examples

Basic usage with environment variables:

use lambda_otel_lite::resource::get_lambda_resource;
use opentelemetry::KeyValue;

// Get resource with Lambda environment attributes
let resource = get_lambda_resource();

Adding custom attributes:

use lambda_otel_lite::resource::get_lambda_resource;
use opentelemetry::KeyValue;
use opentelemetry_sdk::Resource;

// Get Lambda resource
let lambda_resource = get_lambda_resource();

// Create custom resource
let extra_resource = Resource::builder()
    .with_attributes(vec![
        KeyValue::new("deployment.stage", "prod"),
        KeyValue::new("team", "backend"),
    ])
    .build();

// Combine resources (custom attributes take precedence)
// Create a new resource with all attributes
let mut all_attributes = vec![
    KeyValue::new("deployment.stage", "prod"),
    KeyValue::new("team", "backend"),
];

// Add lambda attributes (could be done more programmatically in real code)
all_attributes.push(KeyValue::new("cloud.provider", "aws"));
all_attributes.push(KeyValue::new("faas.name", "my-function"));

let final_resource = Resource::builder()
    .with_attributes(all_attributes)
    .build();

§Integration with Telemetry Config

This function is automatically called by init_telemetry when no custom resource is provided. To override or extend these attributes, use the TelemetryConfig builder:

use lambda_otel_lite::{TelemetryConfig, init_telemetry};
use opentelemetry_sdk::Resource;

// Get base Lambda resource
let base_resource = lambda_otel_lite::get_lambda_resource();

// Configure telemetry with the resource
let config = TelemetryConfig::builder()
    .resource(base_resource)
    .build();

let _completion_handler = init_telemetry(config).await?;