Skip to content

Commit

Permalink
finish off psr-18 auto-instrumentation (#88)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
brettmc authored Nov 30, 2022
1 parent 1510539 commit 78d4ed9
Show file tree
Hide file tree
Showing 15 changed files with 415 additions and 109 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ jobs:
'Context/Swoole',
'Instrumentation/Slim',
'Instrumentation/Psr15',
'Instrumentation/Psr18',
'Symfony'
]
exclude:
- project: 'Instrumentation/Slim'
php-version: 7.4
- project: 'Instrumentation/Psr15'
php-version: 7.4
- project: 'Instrumentation/Psr18'
php-version: 7.4

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 2 additions & 0 deletions .gitsplit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ splits:
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-sdk-bundle.git"
- prefix: "src/Instrumentation/Psr15"
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-psr15.git"
- prefix: "src/Instrumentation/Psr18"
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-psr18.git"
- prefix: "src/Instrumentation/Slim"
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-slim.git"
- prefix: "src/Context/Swoole"
Expand Down
1 change: 0 additions & 1 deletion examples/instrumentation/Psr18/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use OpenTelemetry\SDK\Trace\TracerProvider;

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

$tracerProvider = new TracerProvider(
new BatchSpanProcessor(new ConsoleSpanExporter(), ClockFactory::getDefault()),
Expand Down
43 changes: 43 additions & 0 deletions src/Instrumentation/Psr18/.php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->exclude('var/cache')
->in(__DIR__);

$config = new PhpCsFixer\Config();
return $config->setRules([
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => ['space' => 'none'],
'is_null' => true,
'modernize_types_casting' => true,
'ordered_imports' => true,
'php_unit_construct' => true,
'single_line_comment_style' => true,
'yoda_style' => false,
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'blank_line_after_opening_tag' => true,
'blank_line_before_statement' => true,
'cast_spaces' => true,
'declare_strict_types' => true,
'function_typehint_space' => true,
'include' => true,
'lowercase_cast' => true,
'new_with_braces' => true,
'no_extra_blank_lines' => true,
'no_leading_import_slash' => true,
'echo_tag_syntax' => true,
'no_unused_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'phpdoc_order' => true,
'phpdoc_scalar' => true,
'phpdoc_types' => true,
'short_scalar_cast' => true,
'single_blank_line_before_namespace' => true,
'single_quote' => true,
'trailing_comma_in_multiline' => true,
])
->setRiskyAllowed(true)
->setFinder($finder);

32 changes: 32 additions & 0 deletions src/Instrumentation/Psr18/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# OpenTelemetry PSR-18 auto-instrumentation

## Requirements

* OpenTelemetry extension
* OpenTelemetry SDK an exporter (required to actually export traces)
* A psr-18 client
* (optional) OpenTelemetry [SDK Autoloading](https://github.com/open-telemetry/opentelemetry-php/blob/main/examples/autoload_sdk.php) configured

## Overview
Auto-instrumentation hooks are registered via composer, which will:

* create spans automatically for each PSR-18 request that is sent
* add a `traceparent` header to the request to facilitate distributed tracing.

## Manual configuration
If you are not using SDK autoloading, you will need to create and register a `TracerProvider` early in your application's lifecycle:

```php
<?php
require_once 'vendor/autoload.php';

$tracerProvider = /*create tracer provider*/;
$scope = \OpenTelemetry\API\Common\Instrumentation\Configurator::create()
->withTracerProvider($tracerProvider)
->activate();

$client->sendRequest($request);

$scope->detach();
$tracerProvider->shutdown();
```
5 changes: 5 additions & 0 deletions src/Instrumentation/Psr18/_register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

\OpenTelemetry\Contrib\Instrumentation\Psr18\Psr18Instrumentation::register();
105 changes: 0 additions & 105 deletions src/Instrumentation/Psr18/client_tracing.php

This file was deleted.

36 changes: 36 additions & 0 deletions src/Instrumentation/Psr18/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "open-telemetry/opentelemetry-auto-psr18",
"description": "OpenTelemetry auto-instrumentation for psr18 http clients.",
"keywords": ["opentelemetry", "otel", "open-telemetry", "tracing", "psr18", "instrumentation"],
"type": "library",
"homepage": "https://opentelemetry.io/docs/php",
"readme": "./README.md",
"license": "Apache-2.0",
"minimum-stability": "dev",
"require": {
"php": "^8.0",
"ext-otel_instrumentation": "*",
"open-telemetry/api": "^0",
"psr/http-client": "^1"
},
"autoload": {
"psr-4": {
"OpenTelemetry\\Contrib\\Instrumentation\\Psr18\\": "src/"
},
"files": [
"_register.php"
]
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3",
"nyholm/psr7": "*",
"phan/phan": "^5.0",
"php-http/mock-client": "*",
"phpstan/phpstan": "^1.1",
"phpstan/phpstan-phpunit": "^1.0",
"psalm/plugin-phpunit": "^0.16",
"open-telemetry/sdk": "^0",
"phpunit/phpunit": "^9.5",
"vimeo/psalm": "^4.0"
}
}
9 changes: 9 additions & 0 deletions src/Instrumentation/Psr18/phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon

parameters:
tmpDir: var/cache/phpstan
level: 5
paths:
- src
- tests
47 changes: 47 additions & 0 deletions src/Instrumentation/Psr18/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
cacheResult="false"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
stopOnRisky="false"
timeoutForSmallTests="1"
timeoutForMediumTests="10"
timeoutForLargeTests="60"
verbose="true">

<coverage processUncoveredFiles="true" disableCodeCoverageIgnore="false">
<include>
<directory>src</directory>
</include>
</coverage>

<php>
<ini name="date.timezone" value="UTC" />
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
<ini name="error_reporting" value="E_ALL" />
</php>

<testsuites>
<testsuite name="unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="integration">
<directory>tests/Integration</directory>
</testsuite>
</testsuites>

</phpunit>
15 changes: 15 additions & 0 deletions src/Instrumentation/Psr18/psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
errorLevel="3"
cacheDirectory="var/cache/psalm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd">
<projectFiles>
<directory name="src"/>
<directory name="tests"/>
</projectFiles>
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>
</psalm>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace OpenTelemetry\Instrumentation\Psr18;
namespace OpenTelemetry\Contrib\Instrumentation\Psr18;

use function assert;
use OpenTelemetry\Context\Propagation\PropagationSetterInterface;
Expand All @@ -11,9 +11,14 @@
/**
* @internal
*/
enum HeadersPropagator implements PropagationSetterInterface
class HeadersPropagator implements PropagationSetterInterface
{
case Instance;
public static function instance(): self
{
static $instance;

return $instance ??= new self();
}

public function set(&$carrier, string $key, string $value): void
{
Expand Down
Loading

0 comments on commit 78d4ed9

Please sign in to comment.