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

handle host detector with custom error handler and open_basedir #1454

Merged
merged 5 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 17 additions & 6 deletions src/SDK/Resource/Detectors/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SemConv\ResourceAttributes;
use function php_uname;
use Throwable;

/**
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.8.0/specification/resource/semantic_conventions/host.md#host
Expand Down Expand Up @@ -53,10 +54,15 @@ private function getLinuxId(): ?string

foreach ($paths as $path) {
$file = $this->dir . $path;
if (is_file($file) && is_readable($file)) {
$contents = file_get_contents($file);

return $contents !== false ? trim($contents) : null;
try {
if (is_file($file) && is_readable($file)) {
$contents = file_get_contents($file);

return $contents !== false ? trim($contents) : null;
}
} catch (Throwable $t) {
//do nothing
}
}

Expand All @@ -66,10 +72,15 @@ private function getLinuxId(): ?string
private function getBsdId(): ?string
{
$file = $this->dir . self::PATH_ETC_HOSTID;
if (is_file($file) && is_readable($file)) {
$contents = file_get_contents($file);

return $contents !== false ? trim($contents) : null;
try {
if (is_file($file) && is_readable($file)) {
$contents = file_get_contents($file);

return $contents !== false ? trim($contents) : null;
brettmc marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (Throwable $t) {
//do nothing
}

$out = exec('which kenv && kenv -q smbios.system.uuid');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Host detector with custom error handler and open_basedir
--DESCRIPTION--
An error handler is installed which converts PHP warnings to exceptions, and open_basedir
is configured such that /etc/machine-id and friends cannot be read
--SKIPIF--
<?php if (!in_array(PHP_OS_FAMILY, ['Linux', 'BSD'])) die('skip requires Linux or BSD'); ?>
--ENV--
OTEL_PHP_FIBERS_ENABLED=0
brettmc marked this conversation as resolved.
Show resolved Hide resolved
--INI--
open_basedir=${PWD}
--FILE--
<?php
use OpenTelemetry\SDK\Resource\Detectors\Host;

require_once 'vendor/autoload.php';

function warningToException($errno, $errstr, $errfile, $errline)
{
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler('warningToException');

$detector = new Host();
$resource = $detector->getResource();
var_dump($resource->getAttributes()->toArray());
?>
--EXPECTF--
array(2) {
["host.name"]=>
string(%d) "%s"
["host.arch"]=>
string(%d) "%s"
}
Loading