Skip to content

Commit 78d4ed9

Browse files
authored
finish off psr-18 auto-instrumentation (open-telemetry#88)
- add tests - replace enum with class for php8.0 compatibility - set up auto-registration via composer autoloader - mention sdk autoloading in readme - document some todos/questions
1 parent 1510539 commit 78d4ed9

File tree

15 files changed

+415
-109
lines changed

15 files changed

+415
-109
lines changed

.github/workflows/php.yml

+3
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ jobs:
2121
'Context/Swoole',
2222
'Instrumentation/Slim',
2323
'Instrumentation/Psr15',
24+
'Instrumentation/Psr18',
2425
'Symfony'
2526
]
2627
exclude:
2728
- project: 'Instrumentation/Slim'
2829
php-version: 7.4
2930
- project: 'Instrumentation/Psr15'
3031
php-version: 7.4
32+
- project: 'Instrumentation/Psr18'
33+
php-version: 7.4
3134

3235
steps:
3336
- uses: actions/checkout@v3

.gitsplit.yml

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ splits:
1212
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-sdk-bundle.git"
1313
- prefix: "src/Instrumentation/Psr15"
1414
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-psr15.git"
15+
- prefix: "src/Instrumentation/Psr18"
16+
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-psr18.git"
1517
- prefix: "src/Instrumentation/Slim"
1618
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-slim.git"
1719
- prefix: "src/Context/Swoole"

examples/instrumentation/Psr18/request.php

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use OpenTelemetry\SDK\Trace\TracerProvider;
1515

1616
require_once dirname(__DIR__, 3) . '/vendor/autoload.php';
17-
require_once dirname(__DIR__, 3) . '/src/Instrumentation/Psr18/client_tracing.php';
1817

1918
$tracerProvider = new TracerProvider(
2019
new BatchSpanProcessor(new ConsoleSpanExporter(), ClockFactory::getDefault()),
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/Instrumentation/Psr18/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# OpenTelemetry PSR-18 auto-instrumentation
2+
3+
## Requirements
4+
5+
* OpenTelemetry extension
6+
* OpenTelemetry SDK an exporter (required to actually export traces)
7+
* A psr-18 client
8+
* (optional) OpenTelemetry [SDK Autoloading](https://github.com/open-telemetry/opentelemetry-php/blob/main/examples/autoload_sdk.php) configured
9+
10+
## Overview
11+
Auto-instrumentation hooks are registered via composer, which will:
12+
13+
* create spans automatically for each PSR-18 request that is sent
14+
* add a `traceparent` header to the request to facilitate distributed tracing.
15+
16+
## Manual configuration
17+
If you are not using SDK autoloading, you will need to create and register a `TracerProvider` early in your application's lifecycle:
18+
19+
```php
20+
<?php
21+
require_once 'vendor/autoload.php';
22+
23+
$tracerProvider = /*create tracer provider*/;
24+
$scope = \OpenTelemetry\API\Common\Instrumentation\Configurator::create()
25+
->withTracerProvider($tracerProvider)
26+
->activate();
27+
28+
$client->sendRequest($request);
29+
30+
$scope->detach();
31+
$tracerProvider->shutdown();
32+
```
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
\OpenTelemetry\Contrib\Instrumentation\Psr18\Psr18Instrumentation::register();

src/Instrumentation/Psr18/client_tracing.php

-105
This file was deleted.
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "open-telemetry/opentelemetry-auto-psr18",
3+
"description": "OpenTelemetry auto-instrumentation for psr18 http clients.",
4+
"keywords": ["opentelemetry", "otel", "open-telemetry", "tracing", "psr18", "instrumentation"],
5+
"type": "library",
6+
"homepage": "https://opentelemetry.io/docs/php",
7+
"readme": "./README.md",
8+
"license": "Apache-2.0",
9+
"minimum-stability": "dev",
10+
"require": {
11+
"php": "^8.0",
12+
"ext-otel_instrumentation": "*",
13+
"open-telemetry/api": "^0",
14+
"psr/http-client": "^1"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"OpenTelemetry\\Contrib\\Instrumentation\\Psr18\\": "src/"
19+
},
20+
"files": [
21+
"_register.php"
22+
]
23+
},
24+
"require-dev": {
25+
"friendsofphp/php-cs-fixer": "^3",
26+
"nyholm/psr7": "*",
27+
"phan/phan": "^5.0",
28+
"php-http/mock-client": "*",
29+
"phpstan/phpstan": "^1.1",
30+
"phpstan/phpstan-phpunit": "^1.0",
31+
"psalm/plugin-phpunit": "^0.16",
32+
"open-telemetry/sdk": "^0",
33+
"phpunit/phpunit": "^9.5",
34+
"vimeo/psalm": "^4.0"
35+
}
36+
}
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: 5
7+
paths:
8+
- src
9+
- tests
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
<testsuite name="integration">
43+
<directory>tests/Integration</directory>
44+
</testsuite>
45+
</testsuites>
46+
47+
</phpunit>
+15
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>

src/Instrumentation/Psr18/HeadersPropagator.php renamed to src/Instrumentation/Psr18/src/HeadersPropagator.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace OpenTelemetry\Instrumentation\Psr18;
5+
namespace OpenTelemetry\Contrib\Instrumentation\Psr18;
66

77
use function assert;
88
use OpenTelemetry\Context\Propagation\PropagationSetterInterface;
@@ -11,9 +11,14 @@
1111
/**
1212
* @internal
1313
*/
14-
enum HeadersPropagator implements PropagationSetterInterface
14+
class HeadersPropagator implements PropagationSetterInterface
1515
{
16-
case Instance;
16+
public static function instance(): self
17+
{
18+
static $instance;
19+
20+
return $instance ??= new self();
21+
}
1722

1823
public function set(&$carrier, string $key, string $value): void
1924
{

0 commit comments

Comments
 (0)