Skip to content

Commit

Permalink
update aws lambda detector
Browse files Browse the repository at this point in the history
  • Loading branch information
garysassano committed Dec 6, 2024
1 parent baccd98 commit 36007c8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,32 @@ import {
IResource,
Resource,
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
* and return a {@link Resource} populated with data about the environment.
* Returns an empty Resource if detection fails.
*/
export class AwsLambdaDetectorSync implements DetectorSync {
detect(_config?: ResourceDetectionConfig): IResource {
detect(): IResource {
const awsRegion = process.env.AWS_REGION;
const functionName = process.env.AWS_LAMBDA_FUNCTION_NAME;
if (!functionName) {
return Resource.empty();
}

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 = {
[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,
'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

0 comments on commit 36007c8

Please sign in to comment.