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(detector-aws): add more lambda semconv attributes #2589

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import {
ResourceDetectionConfig,
} from '@opentelemetry/resources';
import {
ATTR_AWS_LOG_GROUP_NAMES,
ATTR_CLOUD_PROVIDER,
ATTR_CLOUD_PLATFORM,
ATTR_CLOUD_REGION,
ATTR_FAAS_VERSION,
ATTR_FAAS_NAME,
ATTR_FAAS_VERSION,
ATTR_FAAS_INSTANCE,
ATTR_FAAS_MAX_MEMORY,
CLOUD_PROVIDER_VALUE_AWS,
CLOUD_PLATFORM_VALUE_AWS_LAMBDA,
} from '../semconv';
Expand All @@ -38,27 +41,37 @@ import {
*/
export class AwsLambdaDetectorSync implements DetectorSync {
detect(_config?: ResourceDetectionConfig): IResource {
garysassano marked this conversation as resolved.
Show resolved Hide resolved
const functionName = process.env.AWS_LAMBDA_FUNCTION_NAME;
if (!functionName) {
garysassano marked this conversation as resolved.
Show resolved Hide resolved
// Check if running inside AWS Lambda environment
const executionEnv = process.env.AWS_EXECUTION_ENV;
if (!executionEnv?.startsWith('AWS_Lambda_')) {
return Resource.empty();
}

const functionVersion = process.env.AWS_LAMBDA_FUNCTION_VERSION;
// These environment variables are guaranteed to be present in Lambda environment
// https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime
const region = process.env.AWS_REGION;
const functionName = process.env.AWS_LAMBDA_FUNCTION_NAME;
const functionVersion = process.env.AWS_LAMBDA_FUNCTION_VERSION;
const memorySize = process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE;

// These environment variables are not available in Lambda SnapStart functions
const logGroupName = process.env.AWS_LAMBDA_LOG_GROUP_NAME;
const logStreamName = process.env.AWS_LAMBDA_LOG_STREAM_NAME;

const attributes: ResourceAttributes = {
garysassano marked this conversation as resolved.
Show resolved Hide resolved
[ATTR_CLOUD_PROVIDER]: String(CLOUD_PROVIDER_VALUE_AWS),
[ATTR_CLOUD_PLATFORM]: String(CLOUD_PLATFORM_VALUE_AWS_LAMBDA),
[ATTR_CLOUD_PROVIDER]: CLOUD_PROVIDER_VALUE_AWS,
[ATTR_CLOUD_PLATFORM]: CLOUD_PLATFORM_VALUE_AWS_LAMBDA,
[ATTR_CLOUD_REGION]: region,
[ATTR_FAAS_NAME]: functionName,
[ATTR_FAAS_VERSION]: functionVersion,
[ATTR_FAAS_MAX_MEMORY]: parseInt(memorySize!) * 1024 * 1024,
};
if (region) {
attributes[ATTR_CLOUD_REGION] = region;
}

if (functionName) {
attributes[ATTR_FAAS_NAME] = functionName;
if (logGroupName) {
attributes[ATTR_AWS_LOG_GROUP_NAMES] = [logGroupName];
}
if (functionVersion) {
attributes[ATTR_FAAS_VERSION] = functionVersion;
if (logStreamName) {
attributes[ATTR_FAAS_INSTANCE] = logStreamName;
}

return new Resource(attributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,28 @@ export const ATTR_CONTAINER_NAME = 'container.name';
*/
export const ATTR_FAAS_NAME = 'faas.name';

/**
* The execution environment ID as a string, that will be potentially reused for other invocations to the same function/function version.
*
* @example 2021/06/28/[$LATEST]2f399eb14537447da05ab2a2e39309de
*
* @note * **AWS Lambda:** Use the (full) log stream name.
*
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
*/
export const ATTR_FAAS_INSTANCE = 'faas.instance';

/**
* The amount of memory available to the serverless function converted to Bytes.
*
* @example 134217728
*
* @note It's recommended to set this attribute since e.g. too little memory can easily stop a Java AWS Lambda function from working correctly. On AWS Lambda, the environment variable `AWS_LAMBDA_FUNCTION_MEMORY_SIZE` provides this information (which must be multiplied by 1,048,576).
*
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
*/
export const ATTR_FAAS_MAX_MEMORY = 'faas.max_memory';

/**
* The immutable version of the function being executed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('awsLambdaDetector', () => {

describe('on lambda', () => {
it('fills resource', async () => {
process.env.AWS_EXECUTION_ENV = 'AWS_Lambda_nodejs22.x';
process.env.AWS_LAMBDA_FUNCTION_NAME = 'name';
process.env.AWS_LAMBDA_FUNCTION_VERSION = 'v1';
process.env.AWS_REGION = 'us-east-1';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@
*/

import * as assert from 'assert';
import {
assertCloudResource,
assertEmptyResource,
} from '@opentelemetry/contrib-test-utils';

import { assertEmptyResource } from '@opentelemetry/contrib-test-utils';
import { awsLambdaDetectorSync } from '../../src';
import {
ATTR_CLOUD_PROVIDER,
ATTR_CLOUD_PLATFORM,
ATTR_CLOUD_REGION,
ATTR_FAAS_NAME,
ATTR_FAAS_VERSION,
ATTR_FAAS_INSTANCE,
ATTR_FAAS_MAX_MEMORY,
ATTR_AWS_LOG_GROUP_NAMES,
CLOUD_PROVIDER_VALUE_AWS,
CLOUD_PLATFORM_VALUE_AWS_LAMBDA,
} from '../../src/semconv';

describe('awsLambdaDetectorSync', () => {
let oldEnv: NodeJS.ProcessEnv;
Expand All @@ -35,25 +43,54 @@ describe('awsLambdaDetectorSync', () => {

describe('on lambda', () => {
it('fills resource', async () => {
process.env.AWS_EXECUTION_ENV = 'AWS_Lambda_nodejs22.x';
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_FUNCTION_MEMORY_SIZE = '128';
process.env.AWS_LAMBDA_LOG_GROUP_NAME = '/aws/lambda/name';
process.env.AWS_LAMBDA_LOG_STREAM_NAME = '2024/03/14/[$LATEST]123456';

const resource = awsLambdaDetectorSync.detect();

assertCloudResource(resource, {
provider: 'aws',
region: 'us-east-1',
});

assert.strictEqual(resource.attributes['faas.name'], 'name');
assert.strictEqual(resource.attributes['faas.version'], 'v1');
assert.strictEqual(
resource.attributes[ATTR_CLOUD_PROVIDER],
CLOUD_PROVIDER_VALUE_AWS
);
assert.strictEqual(
resource.attributes[ATTR_CLOUD_PLATFORM],
CLOUD_PLATFORM_VALUE_AWS_LAMBDA
);
assert.strictEqual(resource.attributes[ATTR_CLOUD_REGION], 'us-east-1');
assert.strictEqual(resource.attributes[ATTR_FAAS_NAME], 'name');
assert.strictEqual(resource.attributes[ATTR_FAAS_VERSION], 'v1');
assert.strictEqual(
resource.attributes[ATTR_FAAS_INSTANCE],
'2024/03/14/[$LATEST]123456'
);
assert.strictEqual(
resource.attributes[ATTR_FAAS_MAX_MEMORY],
128 * 1024 * 1024
);
assert.deepStrictEqual(resource.attributes[ATTR_AWS_LOG_GROUP_NAMES], [
'/aws/lambda/name',
]);
});
});

describe('not on lambda', () => {
it('returns empty resource', async () => {
process.env.AWS_LAMBDA_FUNCTION_VERSION = 'v1';
it('returns empty resource if AWS_EXECUTION_ENV is not set', async () => {
process.env.AWS_LAMBDA_FUNCTION_NAME = 'name';
process.env.AWS_REGION = 'us-east-1';

const resource = awsLambdaDetectorSync.detect();

assertEmptyResource(resource);
});

it('returns empty resource if AWS_EXECUTION_ENV is not Lambda', async () => {
process.env.AWS_EXECUTION_ENV = 'AWS_ECS_EC2';
process.env.AWS_LAMBDA_FUNCTION_NAME = 'name';
process.env.AWS_REGION = 'us-east-1';

const resource = awsLambdaDetectorSync.detect();
Expand Down
Loading