Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add aws.log.group.names attribute to LambdaResourceDetector #139

Merged
merged 4 commits into from
Dec 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading