From 7bdb2745392810ca2b5cfe2007c8ddfd013f6f44 Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Mon, 12 Apr 2021 15:40:31 +0900 Subject: [PATCH] feat: add AWS Lambda detector --- .../src/detectors/AwsLambdaDetector.ts | 58 +++++++++++++++++ .../src/detectors/index.ts | 1 + .../test/detectors/AwsLambdaDetector.test.ts | 64 +++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 packages/opentelemetry-resource-detector-aws/src/detectors/AwsLambdaDetector.ts create mode 100644 packages/opentelemetry-resource-detector-aws/test/detectors/AwsLambdaDetector.test.ts diff --git a/packages/opentelemetry-resource-detector-aws/src/detectors/AwsLambdaDetector.ts b/packages/opentelemetry-resource-detector-aws/src/detectors/AwsLambdaDetector.ts new file mode 100644 index 0000000000..0f42442398 --- /dev/null +++ b/packages/opentelemetry-resource-detector-aws/src/detectors/AwsLambdaDetector.ts @@ -0,0 +1,58 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + Detector, + Resource, + CLOUD_RESOURCE, + ResourceDetectionConfig, +} from '@opentelemetry/resources'; + +/** + * 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 AwsLambdaDetector implements Detector { + async detect(_config?: ResourceDetectionConfig): Promise { + 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 attributes = { + [CLOUD_RESOURCE.PROVIDER]: 'aws', + }; + if (region) { + attributes[CLOUD_RESOURCE.REGION] = region; + } + + // TODO(anuraaga): Migrate to FAAS_RESOURCE when defined. + if (functionName) { + attributes['faas.name'] = functionName; + } + if (functionVersion) { + attributes['faas.version'] = functionVersion; + } + + return new Resource(attributes); + } +} + +export const awsLambdaDetector = new AwsLambdaDetector(); diff --git a/packages/opentelemetry-resource-detector-aws/src/detectors/index.ts b/packages/opentelemetry-resource-detector-aws/src/detectors/index.ts index e873f14e89..b475e4a208 100644 --- a/packages/opentelemetry-resource-detector-aws/src/detectors/index.ts +++ b/packages/opentelemetry-resource-detector-aws/src/detectors/index.ts @@ -18,3 +18,4 @@ export * from './AwsEc2Detector'; export * from './AwsBeanstalkDetector'; export * from './AwsEcsDetector'; export * from './AwsEksDetector'; +export * from './AwsLambdaDetector'; diff --git a/packages/opentelemetry-resource-detector-aws/test/detectors/AwsLambdaDetector.test.ts b/packages/opentelemetry-resource-detector-aws/test/detectors/AwsLambdaDetector.test.ts new file mode 100644 index 0000000000..0487a0b708 --- /dev/null +++ b/packages/opentelemetry-resource-detector-aws/test/detectors/AwsLambdaDetector.test.ts @@ -0,0 +1,64 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as assert from 'assert'; +import { + assertCloudResource, + assertEmptyResource, +} from '@opentelemetry/resources/test/util/resource-assertions'; + +import { awsLambdaDetector } from '../../src'; + +describe('awsLambdaDetector', () => { + let oldEnv: NodeJS.ProcessEnv; + + beforeEach(() => { + oldEnv = { ...process.env }; + }); + + afterEach(() => { + process.env = oldEnv; + }); + + describe('on lambda', () => { + it('fills resource', async () => { + process.env.AWS_LAMBDA_FUNCTION_NAME = 'name'; + process.env.AWS_LAMBDA_FUNCTION_VERSION = 'v1'; + process.env.AWS_REGION = 'us-east-1'; + + const resource = await awsLambdaDetector.detect(); + + assertCloudResource(resource, { + provider: 'aws', + region: 'us-east-1', + }); + + assert.strictEqual(resource.attributes['faas.name'], 'name'); + assert.strictEqual(resource.attributes['faas.version'], 'v1'); + }); + }); + + describe('not on lambda', () => { + it('returns empty resource', async () => { + process.env.AWS_LAMBDA_FUNCTION_VERSION = 'v1'; + process.env.AWS_REGION = 'us-east-1'; + + const resource = await awsLambdaDetector.detect(); + + assertEmptyResource(resource); + }); + }); +});