lambda_otel_utils/
resource.rs1use opentelemetry::KeyValue;
2use opentelemetry_sdk::Resource;
3use std::env;
4
5pub fn get_lambda_resource() -> Resource {
18 let mut attributes = Vec::new();
19
20 if let Ok(region) = env::var("AWS_REGION") {
22 attributes.push(KeyValue::new("cloud.provider", "aws"));
23 attributes.push(KeyValue::new("cloud.region", region));
24 }
25
26 if let Ok(function_name) = env::var("AWS_LAMBDA_FUNCTION_NAME") {
27 attributes.push(KeyValue::new("faas.name", function_name.clone()));
28 if env::var("OTEL_SERVICE_NAME").is_err() {
30 attributes.push(KeyValue::new("service.name", function_name));
31 }
32 }
33
34 if let Ok(version) = env::var("AWS_LAMBDA_FUNCTION_VERSION") {
35 attributes.push(KeyValue::new("faas.version", version));
36 }
37
38 if let Ok(memory) = env::var("AWS_LAMBDA_FUNCTION_MEMORY_SIZE") {
39 if let Ok(memory_mb) = memory.parse::<i64>() {
40 let memory_bytes = memory_mb * 1024 * 1024;
41 attributes.push(KeyValue::new("faas.max_memory", memory_bytes));
42 }
43 }
44
45 if let Ok(log_stream) = env::var("AWS_LAMBDA_LOG_STREAM_NAME") {
46 attributes.push(KeyValue::new("faas.instance", log_stream));
47 }
48
49 Resource::builder().with_attributes(attributes).build()
51}