-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* httplug async client auto-instrumentation * experimental 8.2 build
- Loading branch information
Showing
12 changed files
with
499 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# OpenTelemetry HTTPlug async auto-instrumentation | ||
|
||
## Requirements | ||
|
||
* OpenTelemetry extension | ||
* OpenTelemetry SDK an exporter (required to actually export traces) | ||
* An HTTPlug async 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 async request that is sent | ||
|
||
## 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->sendAsyncRequest($request); | ||
|
||
$scope->detach(); | ||
$tracerProvider->shutdown(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
\OpenTelemetry\Contrib\Instrumentation\HttpAsyncClient\HttpAsyncClientInstrumentation::register(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name": "open-telemetry/opentelemetry-auto-http-async", | ||
"description": "OpenTelemetry auto-instrumentation for HTTPlug async clients.", | ||
"keywords": ["opentelemetry", "otel", "open-telemetry", "tracing", "httplug", "async", "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", | ||
"php-http/httplug": "^2" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"OpenTelemetry\\Contrib\\Instrumentation\\HttpAsyncClient\\": "src/" | ||
}, | ||
"files": [ | ||
"_register.php" | ||
] | ||
}, | ||
"require-dev": { | ||
"friendsofphp/php-cs-fixer": "^3", | ||
"monolog/monolog": "*", | ||
"open-telemetry/exporter-zipkin": "*", | ||
"php-http/guzzle7-adapter": "*", | ||
"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" | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/Instrumentation/HttpAsyncClient/examples/async_requests.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use GuzzleHttp\Psr7\Request; | ||
use Http\Adapter\Guzzle7\Client; | ||
use Monolog\Handler\StreamHandler; | ||
use Monolog\Logger; | ||
use OpenTelemetry\API\Common\Instrumentation; | ||
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; | ||
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransport; | ||
use OpenTelemetry\SDK\Common\Log\LoggerHolder; | ||
use OpenTelemetry\SDK\Common\Time\ClockFactory; | ||
use OpenTelemetry\SDK\Resource\ResourceInfoFactory; | ||
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler; | ||
use OpenTelemetry\SDK\Trace\SpanExporter\ConsoleSpanExporter; | ||
use OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor; | ||
use OpenTelemetry\SDK\Trace\TracerProvider; | ||
use Psr\Log\LogLevel; | ||
|
||
require_once dirname(__DIR__) . '/vendor/autoload.php'; | ||
|
||
LoggerHolder::set( | ||
new Logger('otel-php', [new StreamHandler(STDOUT, LogLevel::DEBUG)]) | ||
); | ||
|
||
$transport = new StreamTransport(fopen('php://stdout', 'a'), 'application/json'); | ||
$exporter = new ConsoleSpanExporter($transport); | ||
|
||
$tracerProvider = new TracerProvider( | ||
new BatchSpanProcessor($exporter, ClockFactory::getDefault()), | ||
new AlwaysOnSampler(), | ||
ResourceInfoFactory::emptyResource(), | ||
); | ||
|
||
$scope = Instrumentation\Configurator::create() | ||
->withTracerProvider($tracerProvider) | ||
->withPropagator(TraceContextPropagator::getInstance()) | ||
->activate(); | ||
|
||
$root = $tracerProvider->getTracer('async-requests-demo')->spanBuilder('root')->startSpan(); | ||
$rootScope = $root->activate(); | ||
|
||
try { | ||
$requests = [ | ||
new Request('GET', 'https://postman-echo.com/get'), | ||
new Request('POST', 'https://postman-echo.com/post'), | ||
new Request('GET', 'https://httpbin.org/does-not-exist'), | ||
new Request('GET', 'https://httpbin.org/get'), | ||
]; | ||
$client = new Client(); | ||
$promises = []; | ||
foreach ($requests as $request) { | ||
$promises[] = [$request, $client->sendAsyncRequest($request)]; | ||
} | ||
foreach ($promises as [$request, $promise]) { | ||
try { | ||
$response = $promise->wait(); | ||
echo sprintf('[%d] ', $response->getStatusCode()) . json_encode(json_decode($response->getBody()->getContents()), JSON_PRETTY_PRINT), PHP_EOL; | ||
} catch (\Exception $e) { | ||
var_dump($e->getMessage()); | ||
} | ||
} | ||
|
||
$root->end(); | ||
} finally { | ||
$rootScope->detach(); | ||
$scope->detach(); | ||
$tracerProvider->shutdown(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.