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 2 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
116 changes: 116 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 @@ -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':
matt-hensley marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->getBsdId();
}
case 'darwin':
{
$out = $this->getMacOsId();
return Host::parseMacOsId($out);
matt-hensley marked this conversation as resolved.
Show resolved Hide resolved
}
case 'windows':
{
$out = $this->getWindowsId();
return Host::parseWindowsId($out);
matt-hensley marked this conversation as resolved.
Show resolved Hide resolved
}
}

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

private function getLinuxId(): string
{
$paths = ['etc/machine-id', 'var/lib/dbus/machine-id'];
matt-hensley marked this conversation as resolved.
Show resolved Hide resolved

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'))
matt-hensley marked this conversation as resolved.
Show resolved Hide resolved
{
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"');
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("\n", $out);
matt-hensley marked this conversation as resolved.
Show resolved Hide resolved

foreach ($lines as $line)
{
if (strpos($line, 'IOPlatformUUID') !== FALSE)
matt-hensley marked this conversation as resolved.
Show resolved Hide resolved
{
$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';
matt-hensley marked this conversation as resolved.
Show resolved Hide resolved
$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'],
];
}
}
Loading