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

§Returns

Returns a Resource containing all detected and configured attributes.

§Examples

Basic usage with automatic detection:

use lambda_otel_lite::get_lambda_resource;

let resource = get_lambda_resource();

Using with custom attributes:

use lambda_otel_lite::get_lambda_resource;
use std::env;

// Set custom attributes
env::set_var("OTEL_RESOURCE_ATTRIBUTES", "deployment.stage=prod,team=backend");
env::set_var("OTEL_SERVICE_NAME", "payment-processor");

let resource = get_lambda_resource();

Merging with additional resource attributes:

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

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

// Create additional resource
let extra_resource = Resource::new(vec![
    KeyValue::new("service.version", "1.0.0"),
    KeyValue::new("deployment.environment", "staging"),
]);

// Merge resources (Lambda attributes take precedence)
let final_resource = extra_resource.merge(&lambda_resource);

§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?;