Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add host.id for non-containerized systems #1266

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions src/SDK/Resource/Detectors/Host.php
matt-hensley marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,128 @@
*/
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';
private readonly string $dir;
private readonly string $os;

public function __construct(string $dir = '/', string $os = PHP_OS_FAMILY)
{
$this->dir = $dir;
$this->os = $os;
}
matt-hensley marked this conversation as resolved.
Show resolved Hide resolved

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($this->os)) {
case 'linux':
{
return $this->getLinuxId();
}
case 'freebsd':
case 'netbsd':
case 'openbsd':
matt-hensley marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->getBsdId();
}
case 'darwin':
{
$out = $this->getMacOsId();

return self::parseMacOsId($out);
}
case 'windows':
{
$out = $this->getWindowsId();

return self::parseWindowsId($out);
}
}

return '';
matt-hensley marked this conversation as resolved.
Show resolved Hide resolved
}

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 '';
}

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 '';
}

private function getMacOsId(): string
{
$out = exec('ioreg -rd1 -c "IOPlatformExpertDevice"');
matt-hensley marked this conversation as resolved.
Show resolved Hide resolved

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');
matt-hensley marked this conversation as resolved.
Show resolved Hide resolved

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

return '';
}

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

foreach ($lines as $line) {
if (str_contains($line, 'IOPlatformUUID')) {
$parts = explode('=', $line);

return trim(str_replace('"', '', $parts[1]));
}
}

return '';
}

public static function parseWindowsId(string $out): string
{
$parts = explode('REG_SZ', $out);

return trim($parts[1]);
}
}
92 changes: 92 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,96 @@
$this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl());
$this->assertIsString($resource->getAttributes()->get(ResourceAttributes::HOST_NAME));
$this->assertIsString($resource->getAttributes()->get(ResourceAttributes::HOST_ARCH));
$this->assertTrue($resource->getAttributes()->has(ResourceAttributes::HOST_ID));
}

public function test_host_parse_macos_id(): void
{
$out = <<<END
+-o J293AP <class IOPlatformExpertDevice, id 0x100000227, registered, matched,$
{
"IOPolledInterface" = "AppleARMWatchdogTimerHibernateHandler is not seria$
"#address-cells" = <02000000>
"AAPL,phandle" = <01000000>
"serial-number" = <432123465233514651303544000000000000000000000000000000$
"IOBusyInterest" = "IOCommand is not serializable"
"target-type" = <"J293">
"platform-name" = <743831303300000000000000000000000000000000000000000000$
"secure-root-prefix" = <"md">
"name" = <"device-tree">
"region-info" = <4c4c2f41000000000000000000000000000000000000000000000000$
"manufacturer" = <"Apple Inc.">
"compatible" = <"J293AP","MacBookPro17,1","AppleARM">
"config-number" = <000000000000000000000000000000000000000000000000000000$
"IOPlatformSerialNumber" = "A01BC3QFQ05D"
"regulatory-model-number" = <41323333380000000000000000000000000000000000$
"time-stamp" = <"Mon Jun 27 20:12:10 PDT 2022">
"clock-frequency" = <00366e01>
"model" = <"MacBookPro17,1">
"mlb-serial-number" = <432123413230363030455151384c4c314a0000000000000000$
"model-number" = <4d59443832000000000000000000000000000000000000000000000$
"IONWInterrupts" = "IONWInterrupts"
"model-config" = <"SUNWAY;MoPED=0x803914B08BE6C5AF0E6C990D7D8240DA4CAC2FF$
"device_type" = <"bootrom">
"#size-cells" = <02000000>
"IOPlatformUUID" = "1AB2345C-03E4-57D4-A375-1234D48DE123"
}
END;
$hostId = Detectors\Host::parseMacOsId($out);
$this->assertIsString($hostId);

Check failure on line 61 in tests/Unit/SDK/Resource/Detectors/HostTest.php

View workflow job for this annotation

GitHub Actions / php (8.1, false)

RedundantCondition

tests/Unit/SDK/Resource/Detectors/HostTest.php:61:16: RedundantCondition: Type string for $hostId is always string (see https://psalm.dev/122)

Check failure on line 61 in tests/Unit/SDK/Resource/Detectors/HostTest.php

View workflow job for this annotation

GitHub Actions / php (8.2, false)

RedundantCondition

tests/Unit/SDK/Resource/Detectors/HostTest.php:61:16: RedundantCondition: Type string for $hostId is always string (see https://psalm.dev/122)

Check failure on line 61 in tests/Unit/SDK/Resource/Detectors/HostTest.php

View workflow job for this annotation

GitHub Actions / php (8.3, false)

RedundantCondition

tests/Unit/SDK/Resource/Detectors/HostTest.php:61:16: RedundantCondition: Type string for $hostId is always string (see https://psalm.dev/122)
$this->assertSame('1AB2345C-03E4-57D4-A375-1234D48DE123', $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);

Check failure on line 69 in tests/Unit/SDK/Resource/Detectors/HostTest.php

View workflow job for this annotation

GitHub Actions / php (8.1, false)

RedundantCondition

tests/Unit/SDK/Resource/Detectors/HostTest.php:69:16: RedundantCondition: Type string for $hostId is always string (see https://psalm.dev/122)

Check failure on line 69 in tests/Unit/SDK/Resource/Detectors/HostTest.php

View workflow job for this annotation

GitHub Actions / php (8.2, false)

RedundantCondition

tests/Unit/SDK/Resource/Detectors/HostTest.php:69:16: RedundantCondition: Type string for $hostId is always string (see https://psalm.dev/122)

Check failure on line 69 in tests/Unit/SDK/Resource/Detectors/HostTest.php

View workflow job for this annotation

GitHub Actions / php (8.3, false)

RedundantCondition

tests/Unit/SDK/Resource/Detectors/HostTest.php:69:16: RedundantCondition: Type string for $hostId is always string (see https://psalm.dev/122)
$this->assertSame('1234567890', $hostId);
}

/**
* @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();
$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', [], ''],
['Linux', $etc_machineid, '1234567890'],
['Linux', array_merge($etc_machineid, $varLibDbus), '1234567890'],
['Linux', $etc_machineid, '1234567890'],
['OpenBSD', [], ''],
['OpenBSD', $etc_hostid, '1234567890'],
];
}
}
Loading