Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto instrumentation for open telemetry #102

Merged
merged 21 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/quality-assurance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
phpunit:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-version: [7.4, 8.0, 8.1, 8.2]
steps:
Expand All @@ -26,6 +27,7 @@ jobs:
php-cs-fixer:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-version: [7.4, 8.0, 8.1]
steps:
Expand Down
15 changes: 12 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@
},
"require-dev": {
"cego/php-cs-fixer": "^1.0",
"orchestra/testbench": "^6.18"
"orchestra/testbench": "^6.18",
"open-telemetry/sdk": "^1.0"
},
"autoload": {
"psr-4": {
"Cego\\": "src/"
}
},
"files": [
"src/RequestInsurance/OpenTelemetry/_register.php"
]
},
"autoload-dev": {
"psr-4": {
Expand All @@ -51,5 +55,10 @@
"Cego\\RequestInsurance\\RequestInsuranceServiceProvider"
]
}
},
"config": {
"allow-plugins": {
"php-http/discovery": true
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Cego\RequestInsurance\OpenTelemetry;

use Throwable;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\Context\Context;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\SemConv\TraceAttributes;

use function OpenTelemetry\Instrumentation\hook;

use Cego\RequestInsurance\RequestInsuranceWorker;
use OpenTelemetry\API\Instrumentation\CachedInstrumentation;

class RequestInsuranceInstrumentation
{
public const NAME = 'cego-request-insurance';

/**
* Instruments the given class method, and gives the span around it the given spanName.
*
* @param CachedInstrumentation $instrumentation
* @param string $className
* @param string $methodName
* @param string $spanName
*
* @return void
*/
private static function hookClassMethod(CachedInstrumentation $instrumentation, string $className, string $methodName, string $spanName): void
{
hook(
$className,
$methodName,
static function () use ($instrumentation, $spanName) {
$span = $instrumentation->tracer()->spanBuilder($spanName)->startSpan();
Context::storage()->attach($span->storeInContext(Context::getCurrent()));
},
static function ($object, $params, $result, ?Throwable $exception) {
$scope = Context::storage()->scope();

if ( ! $scope) {
return;
}

$scope->detach();
$span = Span::fromContext($scope->context());

if ($exception) {
$span->recordException($exception, [TraceAttributes::EXCEPTION_ESCAPED => true]);
$span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage());
} else {
$span->setStatus(StatusCode::STATUS_OK);
}

$span->end();
}
);
}

/**
* Initiated by ./_register.php
*
* @return void
*/
public static function register(): void
{
$instrumentation = new CachedInstrumentation('dk.cego.request-insurance-instrumentation');

$methodsToHook = [
[RequestInsuranceWorker::class, 'processRequestInsurances', 'Process request insurances'],
[RequestInsuranceWorker::class, 'readyWaitingRequestInsurances', 'Ready waiting request insurances'],
[RequestInsuranceWorker::class, 'processHttpRequestChunk', 'Process batch of http requests'],
];

foreach ($methodsToHook as $methodToHook) {
self::hookClassMethod($instrumentation, $methodToHook[0], $methodToHook[1], $methodToHook[2]);
}
}
}
16 changes: 16 additions & 0 deletions src/RequestInsurance/OpenTelemetry/_register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use OpenTelemetry\SDK\Sdk;
use Cego\RequestInsurance\OpenTelemetry\RequestInsuranceInstrumentation;

if (class_exists(Sdk::class) && Sdk::isInstrumentationDisabled(RequestInsuranceInstrumentation::NAME) === true) {
return;
}

if (extension_loaded('opentelemetry') === false) {
return;
}

RequestInsuranceInstrumentation::register();