Skip to content

Commit

Permalink
Add host.id for non-containerized systems (#1266)
Browse files Browse the repository at this point in the history
Co-authored-by: Tobias Bachert <[email protected]>
Co-authored-by: Chris Lightfoot-Wild <[email protected]>
  • Loading branch information
3 people authored Apr 1, 2024
1 parent 7ec0e90 commit bb9eb40
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/SDK/Resource/Detectors/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,85 @@
*/
final class Host implements ResourceDetectorInterface
{
private const PATH_ETC_MACHINEID = 'etc/machine-id';
private const PATH_VAR_LIB_DBUS_MACHINEID = 'var/lib/dbus/machine-id';
private const PATH_ETC_HOSTID = 'etc/hostid';

public function __construct(
private readonly string $dir = '/',
private readonly string $os = PHP_OS_FAMILY,
) {
}

public function getResource(): ResourceInfo
{
$attributes = [
ResourceAttributes::HOST_NAME => php_uname('n'),
ResourceAttributes::HOST_ARCH => php_uname('m'),
ResourceAttributes::HOST_ID => $this->getMachineId(),
];

return ResourceInfo::create(Attributes::create($attributes), ResourceAttributes::SCHEMA_URL);
}

private function getMachineId(): ?string
{
return match ($this->os) {
'Linux' => $this->getLinuxId(),
'BSD' => $this->getBsdId(),
'Darwin' => $this->getMacOsId(),
'Windows' => $this->getWindowsId(),
default => null,
};
}

private function getLinuxId(): ?string
{
$paths = [self::PATH_ETC_MACHINEID, self::PATH_VAR_LIB_DBUS_MACHINEID];

foreach ($paths as $path) {
if (file_exists($this->dir . $path)) {
return trim(file_get_contents($this->dir . $path));
}
}

return null;
}

private function getBsdId(): ?string
{
if (file_exists($this->dir . self::PATH_ETC_HOSTID)) {
return trim(file_get_contents($this->dir . self::PATH_ETC_HOSTID));
}

$out = exec('kenv -q smbios.system.uuid');

if ($out != false) {
return $out;
}

return null;
}

private function getMacOsId(): ?string
{
$out = exec('ioreg -rd1 -c IOPlatformExpertDevice | awk \'/IOPlatformUUID/ { split($0, line, "\""); printf("%s\n", line[4]); }\'');

if ($out != false) {
return $out;
}

return null;
}

private function getWindowsId(): ?string
{
$out = exec('powershell.exe -Command "Get-ItemPropertyValue -Path HKLM:\SOFTWARE\Microsoft\Cryptography -Name MachineGuid"');

if ($out != false) {
return $out;
}

return null;
}
}
53 changes: 53 additions & 0 deletions tests/Unit/SDK/Resource/Detectors/HostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use OpenTelemetry\SDK\Resource\Detectors;
use OpenTelemetry\SemConv\ResourceAttributes;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -22,4 +23,56 @@ public function test_host_get_resource(): void
$this->assertIsString($resource->getAttributes()->get(ResourceAttributes::HOST_NAME));
$this->assertIsString($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH));
}

/**
* @dataProvider hostIdData
*/
public function test_host_id_filesystem(string $os, array $files, ?string $expectedId): void
{
$root = vfsStream::setup('/', null, $files);
$resouceDetector = new Detectors\Host($root->url(), $os);
$resource = $resouceDetector->getResource();

if ($expectedId === null) {
$this->assertFalse($resource->getAttributes()->has(ResourceAttributes::HOST_ID));

return;
}

$hostId = $resource->getAttributes()->get(ResourceAttributes::HOST_ID);
$this->assertIsString($hostId);
$this->assertSame($expectedId, $hostId);
}

public static function hostIdData(): array
{
$etc_machineid = [
'etc' => [
'machine-id' => '1234567890',
],
];
$varLibDbus = [
'var' => [
'lib' => [
'dbus' => [
'machine-id' => '0987654321',
],
],
],
];
$etc_hostid = [
'etc' => [
'hostid' => '1234567890',
],
];

return [
['Linux', [], null],
['Linux', $etc_machineid, '1234567890'],
['Linux', array_merge($etc_machineid, $varLibDbus), '1234567890'],
['Linux', $etc_machineid, '1234567890'],
['BSD', [], null],
['BSD', $etc_hostid, '1234567890'],
];
}
}

0 comments on commit bb9eb40

Please sign in to comment.