Skip to content

Commit

Permalink
feat: add aws.log.group.names attribute to LambdaResourceDetector (
Browse files Browse the repository at this point in the history
  • Loading branch information
garysassano authored Dec 14, 2024
1 parent c6974fb commit c27ebcc
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion opentelemetry-aws/src/detector/lambda.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use opentelemetry::KeyValue;
use opentelemetry::{Array, KeyValue, StringValue, Value};
use opentelemetry_sdk::resource::ResourceDetector;
use opentelemetry_sdk::Resource;
use opentelemetry_semantic_conventions as semconv;
Expand All @@ -12,6 +12,7 @@ const AWS_REGION_ENV_VAR: &str = "AWS_REGION";
const AWS_LAMBDA_FUNCTION_VERSION_ENV_VAR: &str = "AWS_LAMBDA_FUNCTION_VERSION";
const AWS_LAMBDA_LOG_STREAM_NAME_ENV_VAR: &str = "AWS_LAMBDA_LOG_STREAM_NAME";
const AWS_LAMBDA_MEMORY_LIMIT_ENV_VAR: &str = "AWS_LAMBDA_FUNCTION_MEMORY_SIZE";
const AWS_LAMBDA_LOG_GROUP_NAME_ENV_VAR: &str = "AWS_LAMBDA_LOG_GROUP_NAME";

/// Resource detector that collects resource information from AWS Lambda environment.
pub struct LambdaResourceDetector;
Expand All @@ -34,6 +35,7 @@ impl ResourceDetector for LambdaResourceDetector {
// Instance attributes corresponds to the log stream name for AWS Lambda;
// See the FaaS resource specification for more details.
let instance = env::var(AWS_LAMBDA_LOG_STREAM_NAME_ENV_VAR).unwrap_or_default();
let log_group_name = env::var(AWS_LAMBDA_LOG_GROUP_NAME_ENV_VAR).unwrap_or_default();

let attributes = [
KeyValue::new(semconv::resource::CLOUD_PROVIDER, "aws"),
Expand All @@ -42,6 +44,10 @@ impl ResourceDetector for LambdaResourceDetector {
KeyValue::new(semconv::resource::FAAS_NAME, lambda_name),
KeyValue::new(semconv::resource::FAAS_VERSION, function_version),
KeyValue::new(semconv::resource::FAAS_MAX_MEMORY, function_memory_limit),
KeyValue::new(
semconv::resource::AWS_LOG_GROUP_NAMES,
Value::Array(Array::from(vec![StringValue::from(log_group_name)])),
),
];

Resource::new(attributes)
Expand All @@ -64,6 +70,10 @@ mod tests {
"2023/01/01/[$LATEST]5d1edb9e525d486696cf01a3503487bc",
);
set_var(AWS_LAMBDA_MEMORY_LIMIT_ENV_VAR, "128");
set_var(
AWS_LAMBDA_LOG_GROUP_NAME_ENV_VAR,
"/aws/lambda/my-lambda-function",
);

let expected = Resource::new([
KeyValue::new(semconv::resource::CLOUD_PROVIDER, "aws"),
Expand All @@ -75,6 +85,12 @@ mod tests {
KeyValue::new(semconv::resource::FAAS_NAME, "my-lambda-function"),
KeyValue::new(semconv::resource::FAAS_VERSION, "$LATEST"),
KeyValue::new(semconv::resource::FAAS_MAX_MEMORY, 128 * 1024 * 1024),
KeyValue::new(
semconv::resource::AWS_LOG_GROUP_NAMES,
Value::Array(Array::from(vec![StringValue::from(
"/aws/lambda/my-lambda-function".to_string(),
)])),
),
]);

let detector = LambdaResourceDetector {};
Expand All @@ -87,6 +103,7 @@ mod tests {
remove_var(AWS_LAMBDA_FUNCTION_VERSION_ENV_VAR);
remove_var(AWS_LAMBDA_LOG_STREAM_NAME_ENV_VAR);
remove_var(AWS_LAMBDA_MEMORY_LIMIT_ENV_VAR);
remove_var(AWS_LAMBDA_LOG_GROUP_NAME_ENV_VAR);
}

#[sealed_test]
Expand Down

0 comments on commit c27ebcc

Please sign in to comment.