Skip to content

Commit

Permalink
build 8.2 (#91)
Browse files Browse the repository at this point in the history
* httplug async client auto-instrumentation

* experimental 8.2 build
  • Loading branch information
brettmc authored Dec 12, 2022
1 parent 78d4ed9 commit 1e328ca
Show file tree
Hide file tree
Showing 12 changed files with 499 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ on:
jobs:
php:
runs-on: ubuntu-latest
continue-on-error: true
continue-on-error: false
strategy:
fail-fast: false
matrix:
operating-system: [ubuntu-latest]
php-version: ['7.4', '8.0', '8.1']
php-version: ['7.4', '8.0', '8.1', '8.2']
project: [
'Aws',
'Context/Swoole',
Expand Down Expand Up @@ -65,6 +64,7 @@ jobs:

- name: Check Style
working-directory: src/${{ matrix.project }}
continue-on-error: ${{ matrix.php-version == 8.2 }} #temporary, until php-cs-fixer is happy with php8.2
run: vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php -v --dry-run --stop-on-violation --using-cache=no -vvv

- name: Run Phan
Expand Down
43 changes: 43 additions & 0 deletions src/Instrumentation/HttpAsyncClient/.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);

31 changes: 31 additions & 0 deletions src/Instrumentation/HttpAsyncClient/README.md
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();
```
5 changes: 5 additions & 0 deletions src/Instrumentation/HttpAsyncClient/_register.php
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();
39 changes: 39 additions & 0 deletions src/Instrumentation/HttpAsyncClient/composer.json
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 src/Instrumentation/HttpAsyncClient/examples/async_requests.php
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();
}
9 changes: 9 additions & 0 deletions src/Instrumentation/HttpAsyncClient/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/HttpAsyncClient/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/HttpAsyncClient/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>
Loading

0 comments on commit 1e328ca

Please sign in to comment.