Skip to content

Commit

Permalink
Merge pull request #102 from cego/lejo/open-telemetry
Browse files Browse the repository at this point in the history
Add auto instrumentation for open telemetry
  • Loading branch information
LauJosefsen authored Jan 29, 2024
2 parents 86435e2 + 19ab7b1 commit 518b8cd
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
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();

0 comments on commit 518b8cd

Please sign in to comment.