Skip to content

Commit 0e3b693

Browse files
authored
Fix fiber support not loaded in open-telemetry/context (#829)
* Add missing autoload entry * Fix fiber tests * Trigger error if fiber support cannot be loaded
1 parent 44d7b51 commit 0e3b693

File tree

3 files changed

+47
-33
lines changed

3 files changed

+47
-33
lines changed

ZendObserverFiber.php

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22

3-
/** @noinspection PhpUndefinedClassInspection */
4-
/** @noinspection PhpUndefinedNamespaceInspection */
5-
/** @phan-file-suppress PhanUndeclaredClassReference */
3+
/** @noinspection PhpUndefinedMethodInspection */
64
/** @phan-file-suppress PhanUndeclaredClassCatch */
75
/** @phan-file-suppress PhanUndeclaredClassMethod */
86
/** @phan-file-suppress PhanUndeclaredMethod */
@@ -11,43 +9,58 @@
119

1210
namespace OpenTelemetry\Context;
1311

12+
use function extension_loaded;
1413
use FFI;
15-
use FFI\Exception;
14+
use const FILTER_VALIDATE_BOOLEAN;
15+
use function filter_var;
16+
use function is_string;
17+
use const PHP_VERSION_ID;
18+
use function sprintf;
19+
use function trigger_error;
1620

17-
class ZendObserverFiber
21+
/**
22+
* @internal
23+
*/
24+
final class ZendObserverFiber
1825
{
19-
protected static $fibers = null;
20-
21-
public function isEnabled(): bool
26+
public static function isEnabled(): bool
2227
{
23-
return (
24-
PHP_VERSION_ID >= 80100 &&
25-
(in_array(getenv('OTEL_PHP_FIBERS_ENABLED'), ['true', 'on', '1'])) &&
26-
class_exists(FFI::class)
27-
);
28+
$enabled = $_SERVER['OTEL_PHP_FIBERS_ENABLED'] ?? false;
29+
30+
return is_string($enabled)
31+
? filter_var($enabled, FILTER_VALIDATE_BOOLEAN)
32+
: (bool) $enabled;
2833
}
2934

30-
/**
31-
* @psalm-suppress UndefinedClass
32-
*/
33-
public function init(): bool
35+
public static function init(): bool
3436
{
35-
if (null === self::$fibers) {
37+
static $fibers;
38+
if ($fibers) {
39+
return true;
40+
}
41+
42+
if (PHP_VERSION_ID < 80100 || !extension_loaded('ffi')) {
43+
trigger_error('Context: Fiber context switching not supported, requires PHP >= 8.1 and the FFI extension');
44+
45+
return false;
46+
}
47+
48+
try {
49+
$fibers = FFI::scope('OTEL_ZEND_OBSERVER_FIBER');
50+
} catch (FFI\Exception $e) {
3651
try {
37-
$fibers = FFI::scope('OTEL_ZEND_OBSERVER_FIBER');
38-
} catch (Exception $e) {
39-
try {
40-
$fibers = FFI::load(__DIR__ . '/fiber/zend_observer_fiber.h');
41-
} catch (Exception $e) {
42-
return false;
43-
}
52+
$fibers = FFI::load(__DIR__ . '/fiber/zend_observer_fiber.h');
53+
} catch (FFI\Exception $e) {
54+
trigger_error(sprintf('Context: Fiber context switching not supported, %s', $e->getMessage()));
55+
56+
return false;
4457
}
45-
$fibers->zend_observer_fiber_init_register(fn (int $initializing) => Context::storage()->fork($initializing)); //@phpstan-ignore-line
46-
$fibers->zend_observer_fiber_switch_register(fn (int $from, int $to) => Context::storage()->switch($to)); //@phpstan-ignore-line
47-
$fibers->zend_observer_fiber_destroy_register(fn (int $destroying) => Context::storage()->destroy($destroying)); //@phpstan-ignore-line
48-
self::$fibers = $fibers;
4958
}
5059

60+
$fibers->zend_observer_fiber_init_register(static fn (int $initializing) => Context::storage()->fork($initializing)); //@phpstan-ignore-line
61+
$fibers->zend_observer_fiber_switch_register(static fn (int $from, int $to) => Context::storage()->switch($to)); //@phpstan-ignore-line
62+
$fibers->zend_observer_fiber_destroy_register(static fn (int $destroying) => Context::storage()->destroy($destroying)); //@phpstan-ignore-line
63+
5164
return true;
5265
}
5366
}

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
"autoload": {
2020
"psr-4": {
2121
"OpenTelemetry\\Context\\": "."
22-
}
22+
},
23+
"files": [
24+
"fiber/initialize_fiber_handler.php"
25+
]
2326
},
2427
"suggest": {
2528
"ext-ffi": "To allow context switching in Fibers"

fiber/initialize_fiber_handler.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
return;
1414
}
1515

16-
$observer = new ZendObserverFiber();
17-
18-
if ($observer->isEnabled() && $observer->init()) {
16+
if (ZendObserverFiber::isEnabled() && ZendObserverFiber::init()) {
1917
// ffi fiber support enabled
2018
} else {
2119
Context::setStorage(new FiberBoundContextStorage(Context::storage()));

0 commit comments

Comments
 (0)