Skip to content

Commit 3335913

Browse files
authored
migrate context/swoole from main repo (#87)
copied source from opentelemetry-php into -contrib
1 parent 602bfce commit 3335913

File tree

13 files changed

+398
-1
lines changed

13 files changed

+398
-1
lines changed

.github/workflows/php.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ jobs:
1616
matrix:
1717
operating-system: [ubuntu-latest]
1818
php-version: ['7.4', '8.0', '8.1']
19-
project: ['Aws', 'Symfony', 'Instrumentation/Slim', 'Instrumentation/Psr15']
19+
project: [
20+
'Aws',
21+
'Context/Swoole',
22+
'Instrumentation/Slim',
23+
'Instrumentation/Psr15',
24+
'Symfony'
25+
]
2026
exclude:
2127
- project: 'Instrumentation/Slim'
2228
php-version: 7.4

.gitsplit.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ splits:
1414
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-psr15.git"
1515
- prefix: "src/Instrumentation/Slim"
1616
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-slim.git"
17+
- prefix: "src/Context/Swoole"
18+
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/context-swoole.git"
1719

1820
# List of references to split (defined as regexp)
1921
origins:
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
$finder = PhpCsFixer\Finder::create()
3+
->exclude('vendor')
4+
->exclude('var/cache')
5+
->in(__DIR__);
6+
7+
$config = new PhpCsFixer\Config();
8+
return $config->setRules([
9+
'concat_space' => ['spacing' => 'one'],
10+
'declare_equal_normalize' => ['space' => 'none'],
11+
'is_null' => true,
12+
'modernize_types_casting' => true,
13+
'ordered_imports' => true,
14+
'php_unit_construct' => true,
15+
'single_line_comment_style' => true,
16+
'yoda_style' => false,
17+
'@PSR2' => true,
18+
'array_syntax' => ['syntax' => 'short'],
19+
'blank_line_after_opening_tag' => true,
20+
'blank_line_before_statement' => true,
21+
'cast_spaces' => true,
22+
'declare_strict_types' => true,
23+
'function_typehint_space' => true,
24+
'include' => true,
25+
'lowercase_cast' => true,
26+
'new_with_braces' => true,
27+
'no_extra_blank_lines' => true,
28+
'no_leading_import_slash' => true,
29+
'echo_tag_syntax' => true,
30+
'no_unused_imports' => true,
31+
'no_useless_else' => true,
32+
'no_useless_return' => true,
33+
'phpdoc_order' => true,
34+
'phpdoc_scalar' => true,
35+
'phpdoc_types' => true,
36+
'short_scalar_cast' => true,
37+
'single_blank_line_before_namespace' => true,
38+
'single_quote' => true,
39+
'trailing_comma_in_multiline' => true,
40+
])
41+
->setRiskyAllowed(true)
42+
->setFinder($finder);
43+

src/Context/Swoole/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# OpenTelemetry Swoole context

src/Context/Swoole/composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "open-telemetry/context-swoole",
3+
"description": "Async Swoole/OpenSwoole context implementation for OpenTelemetry PHP.",
4+
"keywords": ["opentelemetry", "otel", "contrib", "context", "swoole", "openswoole", "coroutine"],
5+
"type": "library",
6+
"license": "Apache-2.0",
7+
"authors": [
8+
{
9+
"name": "opentelemetry-php contributors",
10+
"homepage": "https://github.com/open-telemetry/opentelemetry-php/graphs/contributors"
11+
}
12+
],
13+
"require": {
14+
"php": "^7.4 || ^8.0",
15+
"open-telemetry/context": "^0"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"OpenTelemetry\\Contrib\\Context\\Swoole\\": "src/"
20+
}
21+
},
22+
"require-dev": {
23+
"friendsofphp/php-cs-fixer": "^3",
24+
"nyholm/psr7": "*",
25+
"phan/phan": "^5.0",
26+
"php-http/mock-client": "*",
27+
"phpstan/phpstan": "^1.1",
28+
"phpstan/phpstan-phpunit": "^1.0",
29+
"psalm/plugin-phpunit": "^0.16",
30+
"open-telemetry/sdk": "^0",
31+
"phpunit/phpunit": "^9.5",
32+
"vimeo/psalm": "^4.0"
33+
}
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
includes:
2+
- vendor/phpstan/phpstan-phpunit/extension.neon
3+
4+
parameters:
5+
tmpDir: var/cache/phpstan
6+
level: 8
7+
paths:
8+
- src
9+
- tests
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
6+
backupGlobals="false"
7+
backupStaticAttributes="false"
8+
cacheResult="false"
9+
colors="false"
10+
convertErrorsToExceptions="true"
11+
convertNoticesToExceptions="true"
12+
convertWarningsToExceptions="true"
13+
forceCoversAnnotation="false"
14+
processIsolation="false"
15+
stopOnError="false"
16+
stopOnFailure="false"
17+
stopOnIncomplete="false"
18+
stopOnSkipped="false"
19+
stopOnRisky="false"
20+
timeoutForSmallTests="1"
21+
timeoutForMediumTests="10"
22+
timeoutForLargeTests="60"
23+
verbose="true">
24+
25+
<coverage processUncoveredFiles="true" disableCodeCoverageIgnore="false">
26+
<include>
27+
<directory>src</directory>
28+
</include>
29+
</coverage>
30+
31+
<php>
32+
<ini name="date.timezone" value="UTC" />
33+
<ini name="display_errors" value="On" />
34+
<ini name="display_startup_errors" value="On" />
35+
<ini name="error_reporting" value="E_ALL" />
36+
</php>
37+
38+
<testsuites>
39+
<testsuite name="unit">
40+
<directory>tests/Unit</directory>
41+
</testsuite>
42+
</testsuites>
43+
44+
</phpunit>

src/Context/Swoole/psalm.xml.dist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="3"
4+
cacheDirectory="var/cache/psalm"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd">
8+
<projectFiles>
9+
<directory name="src"/>
10+
<directory name="tests"/>
11+
</projectFiles>
12+
<plugins>
13+
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
14+
</plugins>
15+
</psalm>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Contrib\Context\Swoole;
6+
7+
use OpenTelemetry\Context\ExecutionContextAwareInterface;
8+
9+
/**
10+
* @internal
11+
*/
12+
final class SwooleContextDestructor
13+
{
14+
private ExecutionContextAwareInterface $storage;
15+
private int $cid;
16+
17+
public function __construct(ExecutionContextAwareInterface $storage, int $cid)
18+
{
19+
$this->storage = $storage;
20+
$this->cid = $cid;
21+
}
22+
23+
public function __destruct()
24+
{
25+
$this->storage->destroy($this->cid);
26+
}
27+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/** @noinspection PhpComposerExtensionStubsInspection */
4+
5+
declare(strict_types=1);
6+
7+
namespace OpenTelemetry\Contrib\Context\Swoole;
8+
9+
use OpenTelemetry\Context\ExecutionContextAwareInterface;
10+
use Swoole\Coroutine;
11+
12+
/**
13+
* @internal
14+
*
15+
* @phan-file-suppress PhanUndeclaredClassMethod
16+
* @psalm-suppress UndefinedClass
17+
*/
18+
final class SwooleContextHandler
19+
{
20+
private ExecutionContextAwareInterface $storage;
21+
22+
public function __construct(ExecutionContextAwareInterface $storage)
23+
{
24+
$this->storage = $storage;
25+
}
26+
27+
public function switchToActiveCoroutine(): void
28+
{
29+
$cid = Coroutine::getCid();
30+
if ($cid !== -1 && !$this->isForked($cid)) {
31+
for ($pcid = $cid; ($pcid = Coroutine::getPcid($pcid)) !== -1 && !$this->isForked($pcid);) {
32+
}
33+
34+
$this->storage->switch($pcid);
35+
$this->forkCoroutine($cid);
36+
}
37+
38+
$this->storage->switch($cid);
39+
}
40+
41+
public function splitOffChildCoroutines(): void
42+
{
43+
$pcid = Coroutine::getCid();
44+
foreach (Coroutine::listCoroutines() as $cid) {
45+
if ($pcid === Coroutine::getPcid($cid) && !$this->isForked($cid)) {
46+
$this->forkCoroutine($cid);
47+
}
48+
}
49+
}
50+
51+
private function isForked(int $cid): bool
52+
{
53+
return isset(Coroutine::getContext($cid)[__CLASS__]);
54+
}
55+
56+
private function forkCoroutine(int $cid): void
57+
{
58+
$this->storage->fork($cid);
59+
Coroutine::getContext($cid)[__CLASS__] = new SwooleContextDestructor($this->storage, $cid);
60+
}
61+
}

0 commit comments

Comments
 (0)