Skip to content

Commit d466baf

Browse files
Merge pull request #13 from open-o11y/LambdaDetector
Add Lambda Detector
2 parents 92f0e66 + f2fe2ad commit d466baf

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

detectors/LambdaDetector.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright The OpenTelemetry Authors
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* https://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Detectors\Aws;
22+
23+
use OpenTelemetry\Sdk\Resource\ResourceConstants;
24+
use OpenTelemetry\Sdk\Resource\ResourceInfo;
25+
use OpenTelemetry\Sdk\Trace\Attributes;
26+
27+
/**
28+
* The AwsLambdaDetector can be used to detect if a process is running in AWS Lambda
29+
* and return a {@link Resource} populated with data about the environment.
30+
* Returns an empty Resource if detection fails.
31+
*/
32+
class LambdaDetector
33+
{
34+
private const LAMBDA_NAME_ENV = 'AWS_LAMBDA_FUNCTION_NAME';
35+
private const LAMBDA_VERSION_ENV = 'AWS_LAMBDA_FUNCTION_VERSION';
36+
private const AWS_REGION_ENV = 'AWS_REGION';
37+
private const CLOUD_PROVIDER = 'aws';
38+
39+
public function detect(): ResourceInfo
40+
{
41+
$lambdaName = getenv(self::LAMBDA_NAME_ENV);
42+
$functionVersion = getenv(self::LAMBDA_VERSION_ENV);
43+
$awsRegion = getenv(self::AWS_REGION_ENV);
44+
45+
// The following ternary operations are created because
46+
// the attributes class will only NOT create a variable
47+
// when it is set to null. getenv returns false when unsuccessful
48+
$lambdaName = $lambdaName ? $lambdaName : null;
49+
$functionVersion = $functionVersion ? $functionVersion : null;
50+
$awsRegion = $awsRegion ? $awsRegion : null;
51+
52+
return !$lambdaName && !$awsRegion && !$functionVersion
53+
? ResourceInfo::emptyResource()
54+
: ResourceInfo::create(new Attributes([
55+
ResourceConstants::FAAS_NAME => $lambdaName,
56+
ResourceConstants::FAAS_VERSION => $functionVersion,
57+
ResourceConstants::CLOUD_REGION => $awsRegion,
58+
ResourceConstants::CLOUD_PROVIDER => self::CLOUD_PROVIDER,
59+
]));
60+
}
61+
}

tests/unit/LambdaDetectorTest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Detectors\Aws\LambdaDetector;
6+
use OpenTelemetry\Sdk\Resource\ResourceConstants;
7+
use OpenTelemetry\Sdk\Resource\ResourceInfo;
8+
use OpenTelemetry\Sdk\Trace\Attributes;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class LambdaDetectorTest extends TestCase
12+
{
13+
private const LAMBDA_NAME_ENV = 'AWS_LAMBDA_FUNCTION_NAME';
14+
private const LAMBDA_VERSION_ENV = 'AWS_LAMBDA_FUNCTION_VERSION';
15+
private const AWS_REGION_ENV = 'AWS_REGION';
16+
private const CLOUD_PROVIDER = 'aws';
17+
18+
private const LAMBDA_NAME_VAL = 'my-lambda';
19+
private const LAMBDA_VERSION_VAL = 'lambda-version';
20+
private const AWS_REGION_VAL = 'us-west-1';
21+
22+
/**
23+
* @test
24+
*/
25+
public function TestValidLambda()
26+
{
27+
putenv(self::LAMBDA_NAME_ENV . '=' . self::LAMBDA_NAME_VAL);
28+
putenv(self::LAMBDA_VERSION_ENV . '=' . self::LAMBDA_VERSION_VAL);
29+
putenv(self::AWS_REGION_ENV . '=' . self::AWS_REGION_VAL);
30+
31+
$detector = new LambdaDetector();
32+
33+
$this->assertEquals(ResourceInfo::create(
34+
new Attributes([
35+
ResourceConstants::FAAS_NAME => self::LAMBDA_NAME_VAL,
36+
ResourceConstants::FAAS_VERSION => self::LAMBDA_VERSION_VAL,
37+
ResourceConstants::CLOUD_REGION => self::AWS_REGION_VAL,
38+
ResourceConstants::CLOUD_PROVIDER => self::CLOUD_PROVIDER,
39+
])
40+
), $detector->detect());
41+
42+
//unset environment variable
43+
putenv(self::LAMBDA_NAME_ENV);
44+
putenv(self::LAMBDA_VERSION_ENV);
45+
putenv(self::AWS_REGION_ENV);
46+
}
47+
48+
/**
49+
* @test
50+
*/
51+
public function TestInvalidLambda()
52+
{
53+
$detector = new LambdaDetector();
54+
$this->assertEquals(ResourceInfo::emptyResource(), $detector->detect());
55+
}
56+
57+
/**
58+
* @test
59+
*/
60+
public function TestIncompleteLambda1()
61+
{
62+
putenv(self::LAMBDA_NAME_ENV . '=' . self::LAMBDA_NAME_VAL);
63+
64+
$detector = new LambdaDetector();
65+
66+
$this->assertEquals(ResourceInfo::create(
67+
new Attributes([
68+
ResourceConstants::FAAS_NAME => self::LAMBDA_NAME_VAL,
69+
ResourceConstants::CLOUD_PROVIDER => self::CLOUD_PROVIDER,
70+
])
71+
), $detector->detect());
72+
73+
//unset environment variable
74+
putenv(self::LAMBDA_NAME_ENV);
75+
}
76+
77+
/**
78+
* @test
79+
*/
80+
public function TestIncompleteLambda2()
81+
{
82+
putenv(self::LAMBDA_NAME_ENV . '=' . self::LAMBDA_NAME_VAL);
83+
putenv(self::AWS_REGION_ENV . '=' . self::AWS_REGION_VAL);
84+
85+
$detector = new LambdaDetector();
86+
87+
$this->assertEquals(ResourceInfo::create(
88+
new Attributes([
89+
ResourceConstants::FAAS_NAME => self::LAMBDA_NAME_VAL,
90+
ResourceConstants::CLOUD_REGION => self::AWS_REGION_VAL,
91+
ResourceConstants::CLOUD_PROVIDER => self::CLOUD_PROVIDER,
92+
])
93+
), $detector->detect());
94+
95+
//unset environment variable
96+
putenv(self::LAMBDA_NAME_ENV);
97+
putenv(self::AWS_REGION_ENV);
98+
}
99+
}

0 commit comments

Comments
 (0)