Skip to content

Commit

Permalink
feat: add AWS Lambda detector
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuraag Agrawal committed Apr 12, 2021
1 parent 9371029 commit 7bdb274
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Resource> {
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();
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './AwsEc2Detector';
export * from './AwsBeanstalkDetector';
export * from './AwsEcsDetector';
export * from './AwsEksDetector';
export * from './AwsLambdaDetector';
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 7bdb274

Please sign in to comment.