Skip to content

Commit 27f64d9

Browse files
Anuraag Agrawalvmarchaud
andauthored
feat: add AWS Lambda detector (#2102)
* feat: add AWS Lambda detector * chore: document detectors * chore: issue Co-authored-by: Valentin Marchaud <[email protected]>
1 parent d268bc6 commit 27f64d9

File tree

4 files changed

+132
-1
lines changed

4 files changed

+132
-1
lines changed

packages/opentelemetry-resource-detector-aws/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ const resource = await detectResources({
2727
const tracerProvider = new NodeTracerProvider({ resource });
2828
```
2929

30-
**Note**: Besides `awsEc2Detector` there are also the following detectors available: `awsBeanstalkDetector`, `awsEksDetector` and `awsEcsDetector`
30+
## Available detectors
31+
32+
- `awsBeanstalkDetector`: Populates `service` for processes running on [AWS Elastic Beanstalk](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/Welcome.html)
33+
- `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
34+
- `awsEcsDetector`: Populates `container` for containers running on [Amazon ECS](https://aws.amazon.com/ecs/)
35+
- `awsEksDetector`: Populates `container` and `k8s.cluster_name` for containers running on [Amazon EKS](https://aws.amazon.com/eks/)
36+
- `k8s.cluster_name` is not always available depending on the configuration of CloudWatch monitoring for the EKS cluster
37+
- `awsLambdaDetector`: Populates `faas` and `cloud` for functions running on [AWS Lambda](https://aws.amazon.com/lambda/)
38+
- `faas.id` is currently not populated as it is not provided by the runtime at startup
3139

3240
## Useful links
3341

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {
18+
Detector,
19+
Resource,
20+
CLOUD_RESOURCE,
21+
ResourceDetectionConfig,
22+
} from '@opentelemetry/resources';
23+
24+
/**
25+
* The AwsLambdaDetector can be used to detect if a process is running in AWS Lambda
26+
* and return a {@link Resource} populated with data about the environment.
27+
* Returns an empty Resource if detection fails.
28+
*/
29+
export class AwsLambdaDetector implements Detector {
30+
async detect(_config?: ResourceDetectionConfig): Promise<Resource> {
31+
const functionName = process.env.AWS_LAMBDA_FUNCTION_NAME;
32+
if (!functionName) {
33+
return Resource.empty();
34+
}
35+
36+
const functionVersion = process.env.AWS_LAMBDA_FUNCTION_VERSION;
37+
const region = process.env.AWS_REGION;
38+
39+
const attributes = {
40+
[CLOUD_RESOURCE.PROVIDER]: 'aws',
41+
};
42+
if (region) {
43+
attributes[CLOUD_RESOURCE.REGION] = region;
44+
}
45+
46+
// TODO(https://github.com/open-telemetry/opentelemetry-js/issues/2123): Migrate to FAAS_RESOURCE when defined.
47+
if (functionName) {
48+
attributes['faas.name'] = functionName;
49+
}
50+
if (functionVersion) {
51+
attributes['faas.version'] = functionVersion;
52+
}
53+
54+
return new Resource(attributes);
55+
}
56+
}
57+
58+
export const awsLambdaDetector = new AwsLambdaDetector();

packages/opentelemetry-resource-detector-aws/src/detectors/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export * from './AwsEc2Detector';
1818
export * from './AwsBeanstalkDetector';
1919
export * from './AwsEcsDetector';
2020
export * from './AwsEksDetector';
21+
export * from './AwsLambdaDetector';
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as assert from 'assert';
18+
import {
19+
assertCloudResource,
20+
assertEmptyResource,
21+
} from '@opentelemetry/resources/test/util/resource-assertions';
22+
23+
import { awsLambdaDetector } from '../../src';
24+
25+
describe('awsLambdaDetector', () => {
26+
let oldEnv: NodeJS.ProcessEnv;
27+
28+
beforeEach(() => {
29+
oldEnv = { ...process.env };
30+
});
31+
32+
afterEach(() => {
33+
process.env = oldEnv;
34+
});
35+
36+
describe('on lambda', () => {
37+
it('fills resource', async () => {
38+
process.env.AWS_LAMBDA_FUNCTION_NAME = 'name';
39+
process.env.AWS_LAMBDA_FUNCTION_VERSION = 'v1';
40+
process.env.AWS_REGION = 'us-east-1';
41+
42+
const resource = await awsLambdaDetector.detect();
43+
44+
assertCloudResource(resource, {
45+
provider: 'aws',
46+
region: 'us-east-1',
47+
});
48+
49+
assert.strictEqual(resource.attributes['faas.name'], 'name');
50+
assert.strictEqual(resource.attributes['faas.version'], 'v1');
51+
});
52+
});
53+
54+
describe('not on lambda', () => {
55+
it('returns empty resource', async () => {
56+
process.env.AWS_LAMBDA_FUNCTION_VERSION = 'v1';
57+
process.env.AWS_REGION = 'us-east-1';
58+
59+
const resource = await awsLambdaDetector.detect();
60+
61+
assertEmptyResource(resource);
62+
});
63+
});
64+
});

0 commit comments

Comments
 (0)