Skip to content

Commit

Permalink
add bare-metal host ID
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-hensley committed Mar 26, 2024
1 parent a1489d3 commit 68e6b09
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
116 changes: 116 additions & 0 deletions src/SDK/Resource/Detectors/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,136 @@
use OpenTelemetry\SDK\Resource\ResourceDetectorInterface;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SemConv\ResourceAttributes;
use MachineIdSource;

Check warning on line 11 in src/SDK/Resource/Detectors/Host.php

View workflow job for this annotation

GitHub Actions / php (8.4, true, --ignore-platform-reqs)

OpenTelemetry\SDK\Resource\Detectors\Host has uncovered dependency on MachineIdSource (SDK)
use function php_uname;

/**
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.8.0/specification/resource/semantic_conventions/host.md#host
*/
final class Host implements ResourceDetectorInterface
{
private string $dir;

public function __construct(string $dir = '/')
{
$this->dir = $dir;
}

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()
{
switch (strtolower(PHP_OS_FAMILY))
{
case 'linux':
{
return $this->getLinuxId();
}
case 'freebsd':
case 'netbsd':
case 'openbsd':
{
return $this->getBsdId();
}
case 'darwin':
{
$out = $this->getMacOsId();
return Host::parseMacOsId($out);
}
case 'windows':
{
$out = $this->getWindowsId();
return Host::parseWindowsId($out);
}
}

return '';
}

private function getLinuxId(): string
{
$paths = ['etc/machine-id', 'var/lib/dbus/machine-id'];

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

return '';
}

private function getBsdId(): string
{
if (file_exists('/etc/hostid'))
{
return trim(file_get_contents('/etc/hostid'));
}

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

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

return '';
}

private function getMacOsId(): string
{
$out = exec('ioreg -rd1 -c "IOPlatformExpertDevice"');

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

return '';
}

private function getWindowsId(): string
{
$out = exec('%windir%\System32\REG.exe QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography /v MachineGuid');

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

return '';
}

public static function parseMacOsId(string $out): string
{
$lines = explode("\n", $out);

foreach ($lines as $line)
{
if (strpos($line, 'IOPlatformUUID') !== FALSE)
{
$parts = explode('=', $line);
return trim($parts[1]);
}
}

return '';
}

public static function parseWindowsId(string $out): string
{
$parts = explode('REG_SZ', $out);
return trim($parts[1]);
}
}
56 changes: 56 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 @@ -21,5 +22,60 @@ public function test_host_get_resource(): void
$this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl());
$this->assertIsString($resource->getAttributes()->get(ResourceAttributes::HOST_NAME));
$this->assertIsString($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH));
$this->assertIsString($resource->getAttributes()->get(ResourceAttributes::HOST_ID));
}

public function test_host_parse_macos_id(): void
{
$out = 'IOPlatformUUID=1234567890';
$hostId = Detectors\Host::parseMacOsId($out);
$this->assertIsString($hostId);
$this->assertSame('1234567890', $hostId);
}

public function test_host_parse_windows_id(): void
{
$out = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\MachineGuid REG_SZ 1234567890';
$hostId = Detectors\Host::parseWindowsId($out);
$this->assertIsString($hostId);
$this->assertSame('1234567890', $hostId);
}

/**
* @dataProvider hostIdData
*/
public function test_host_id_linux(array $files, string $expectedId): void
{
$root = vfsStream::setup('/', null, $files);
$resouceDetector = new Detectors\Host($root->url());
$resource = $resouceDetector->getResource();
$hostId = $resource->getAttributes()->get(ResourceAttributes::HOST_ID);
$this->assertIsString($hostId);
$this->assertSame($expectedId, $hostId);
}

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

return [
[[], ''],
[$etc, '1234567890'],
[array_merge($etc, $varLibDbus), '1234567890'],
[$varLibDbus, '0987654321'],
];
}
}

0 comments on commit 68e6b09

Please sign in to comment.