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

fix(detector-aws): update aws lambda detector #2589

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ import {
ResourceAttributes,
ResourceDetectionConfig,
} from '@opentelemetry/resources';
import {
SEMRESATTRS_CLOUD_PROVIDER,
SEMRESATTRS_CLOUD_PLATFORM,
SEMRESATTRS_CLOUD_REGION,
SEMRESATTRS_FAAS_VERSION,
SEMRESATTRS_FAAS_NAME,
CLOUDPROVIDERVALUES_AWS,
CLOUDPLATFORMVALUES_AWS_LAMBDA,
} from '@opentelemetry/semantic-conventions';

/**
* The AwsLambdaDetector can be used to detect if a process is running in AWS Lambda
Expand All @@ -43,23 +34,22 @@ export class AwsLambdaDetectorSync implements DetectorSync {
return Resource.empty();
}

const awsRegion = process.env.AWS_REGION;
const functionVersion = process.env.AWS_LAMBDA_FUNCTION_VERSION;
const region = process.env.AWS_REGION;
const logGroupName = process.env.AWS_LAMBDA_LOG_GROUP_NAME;
const logStreamName = process.env.AWS_LAMBDA_LOG_STREAM_NAME;
const memorySize = process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE as string;

const attributes: ResourceAttributes = {
garysassano marked this conversation as resolved.
Show resolved Hide resolved
[SEMRESATTRS_CLOUD_PROVIDER]: String(CLOUDPROVIDERVALUES_AWS),
[SEMRESATTRS_CLOUD_PLATFORM]: String(CLOUDPLATFORMVALUES_AWS_LAMBDA),
'aws.log.group.names': [logGroupName],
'cloud.provider': 'aws',
'cloud.platform': 'aws_lambda',
'cloud.region': awsRegion,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For attribute values that can possibly be undefined (even if not expected to be so), I think it would be safer to keep the if checks to only add such attributes if defined.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this context, it's unnecessary to check for undefined runtime environment variables. Since you're already ensuring the code runs within a Lambda environment, these variables are guaranteed to be present. Adding extra checks for them would undermine the purpose of AWS's standardization across Lambda runtimes, as detailed in the docs.

'faas.name': functionName,
'faas.version': functionVersion,
'faas.instance': logStreamName,
'faas.max_memory': parseInt(memorySize) * 1024 * 1024,
};
if (region) {
attributes[SEMRESATTRS_CLOUD_REGION] = region;
}

if (functionName) {
attributes[SEMRESATTRS_FAAS_NAME] = functionName;
}
if (functionVersion) {
attributes[SEMRESATTRS_FAAS_VERSION] = functionVersion;
}

return new Resource(attributes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ describe('awsLambdaDetectorSync', () => {

describe('on lambda', () => {
it('fills resource', async () => {
process.env.AWS_REGION = 'us-east-1';
process.env.AWS_LAMBDA_FUNCTION_NAME = 'name';
process.env.AWS_LAMBDA_FUNCTION_VERSION = 'v1';
process.env.AWS_REGION = 'us-east-1';
process.env.AWS_LAMBDA_LOG_GROUP_NAME = '/aws/lambda/name';
process.env.AWS_LAMBDA_LOG_STREAM_NAME = '2024/03/14/[$LATEST]123456';
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '128';

const resource = awsLambdaDetectorSync.detect();

Expand All @@ -48,6 +51,17 @@ describe('awsLambdaDetectorSync', () => {

assert.strictEqual(resource.attributes['faas.name'], 'name');
assert.strictEqual(resource.attributes['faas.version'], 'v1');
assert.strictEqual(
resource.attributes['faas.instance'],
'2024/03/14/[$LATEST]123456'
);
assert.strictEqual(
resource.attributes['faas.max_memory'],
128 * 1024 * 1024
);
assert.deepStrictEqual(resource.attributes['aws.log.group.names'], [
'/aws/lambda/name',
]);
});
});

Expand Down
Loading