Skip to content

Commit

Permalink
feat: add AWS Lambda detector (#2102)
Browse files Browse the repository at this point in the history
* feat: add AWS Lambda detector

* chore: document detectors

* chore: issue

Co-authored-by: Valentin Marchaud <[email protected]>
  • Loading branch information
Anuraag Agrawal and vmarchaud authored Apr 20, 2021
1 parent d268bc6 commit 27f64d9
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/opentelemetry-resource-detector-aws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ const resource = await detectResources({
const tracerProvider = new NodeTracerProvider({ resource });
```

**Note**: Besides `awsEc2Detector` there are also the following detectors available: `awsBeanstalkDetector`, `awsEksDetector` and `awsEcsDetector`
## Available detectors

- `awsBeanstalkDetector`: Populates `service` for processes running on [AWS Elastic Beanstalk](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/Welcome.html)
- `awsEc2Detector`: Populates `cloud` and `host` for processes running on [Amazon EC2](https://aws.amazon.com/ec2/), including abstractions such as ECS on EC2. Notably, it does not populate anything on AWS Fargate
- `awsEcsDetector`: Populates `container` for containers running on [Amazon ECS](https://aws.amazon.com/ecs/)
- `awsEksDetector`: Populates `container` and `k8s.cluster_name` for containers running on [Amazon EKS](https://aws.amazon.com/eks/)
- `k8s.cluster_name` is not always available depending on the configuration of CloudWatch monitoring for the EKS cluster
- `awsLambdaDetector`: Populates `faas` and `cloud` for functions running on [AWS Lambda](https://aws.amazon.com/lambda/)
- `faas.id` is currently not populated as it is not provided by the runtime at startup

## Useful links

Expand Down
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(https://github.com/open-telemetry/opentelemetry-js/issues/2123): 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 27f64d9

Please sign in to comment.