Skip to content

Commit 032c60f

Browse files
authored
feat(lambda): read cloud.account.id from symlink in Lambda detector (#507)
* feat(lambda): read cloud.account.id from /tmp/.otel-account-id symlink The Lambda resource detector now reads the /tmp/.otel-account-id symlink via readlink() and sets cloud.account.id on the resource. If the symlink is missing or unreadable, detection proceeds without the attribute. * Rename symlink to .otel-aws-account-id
1 parent e757ff0 commit 032c60f

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

src/Aws/src/Lambda/Detector.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Detector implements ResourceDetectorInterface
3737
private const LAMBDA_VERSION_ENV = 'AWS_LAMBDA_FUNCTION_VERSION';
3838
private const AWS_REGION_ENV = 'AWS_REGION';
3939
private const CLOUD_PROVIDER = 'aws';
40+
private const ACCOUNT_ID_SYMLINK_PATH = '/tmp/.otel-aws-account-id';
4041

4142
public function getResource(): ResourceInfo
4243
{
@@ -51,13 +52,23 @@ public function getResource(): ResourceInfo
5152
$functionVersion = $functionVersion ? $functionVersion : null;
5253
$awsRegion = $awsRegion ? $awsRegion : null;
5354

54-
return !$lambdaName && !$awsRegion && !$functionVersion
55+
$accountId = null;
56+
$symlinkPath = self::ACCOUNT_ID_SYMLINK_PATH;
57+
if (is_link($symlinkPath)) {
58+
$target = readlink($symlinkPath);
59+
if ($target !== false) {
60+
$accountId = $target;
61+
}
62+
}
63+
64+
return !$lambdaName && !$awsRegion && !$functionVersion && !$accountId
5565
? ResourceInfoFactory::emptyResource()
5666
: ResourceInfo::create(Attributes::create([
5767
ResourceAttributes::FAAS_NAME => $lambdaName,
5868
ResourceAttributes::FAAS_VERSION => $functionVersion,
5969
ResourceAttributes::CLOUD_REGION => $awsRegion,
6070
ResourceAttributes::CLOUD_PROVIDER => self::CLOUD_PROVIDER,
71+
ResourceAttributes::CLOUD_ACCOUNT_ID => $accountId,
6172
]));
6273
}
6374
}

src/Aws/tests/Unit/Lambda/DetectorTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,71 @@ public function TestIncompleteLambda2()
9999
putenv(self::LAMBDA_NAME_ENV);
100100
putenv(self::AWS_REGION_ENV);
101101
}
102+
103+
private const ACCOUNT_ID_SYMLINK_PATH = '/tmp/.otel-aws-account-id';
104+
105+
private function removeSymlinkIfExists(): ?string
106+
{
107+
if (is_link(self::ACCOUNT_ID_SYMLINK_PATH)) {
108+
$previous = readlink(self::ACCOUNT_ID_SYMLINK_PATH);
109+
unlink(self::ACCOUNT_ID_SYMLINK_PATH);
110+
111+
return $previous !== false ? $previous : null;
112+
}
113+
114+
return null;
115+
}
116+
117+
/**
118+
* @test
119+
*/
120+
public function TestAccountIdFromSymlink()
121+
{
122+
$previous = $this->removeSymlinkIfExists();
123+
124+
$accountId = '123456789012';
125+
symlink($accountId, self::ACCOUNT_ID_SYMLINK_PATH);
126+
127+
try {
128+
putenv(self::LAMBDA_NAME_ENV . '=' . self::LAMBDA_NAME_VAL);
129+
130+
$detector = new Detector();
131+
$resource = $detector->getResource();
132+
133+
$this->assertEquals(
134+
$accountId,
135+
$resource->getAttributes()->get(ResourceAttributes::CLOUD_ACCOUNT_ID)
136+
);
137+
} finally {
138+
unlink(self::ACCOUNT_ID_SYMLINK_PATH);
139+
if ($previous !== null) {
140+
symlink($previous, self::ACCOUNT_ID_SYMLINK_PATH);
141+
}
142+
putenv(self::LAMBDA_NAME_ENV);
143+
}
144+
}
145+
146+
/**
147+
* @test
148+
*/
149+
public function TestMissingAccountIdSymlink()
150+
{
151+
$previous = $this->removeSymlinkIfExists();
152+
153+
try {
154+
putenv(self::LAMBDA_NAME_ENV . '=' . self::LAMBDA_NAME_VAL);
155+
156+
$detector = new Detector();
157+
$resource = $detector->getResource();
158+
159+
$this->assertNull(
160+
$resource->getAttributes()->get(ResourceAttributes::CLOUD_ACCOUNT_ID)
161+
);
162+
} finally {
163+
if ($previous !== null) {
164+
symlink($previous, self::ACCOUNT_ID_SYMLINK_PATH);
165+
}
166+
putenv(self::LAMBDA_NAME_ENV);
167+
}
168+
}
102169
}

0 commit comments

Comments
 (0)