-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from cego/lejo/open-telemetry
Add auto instrumentation for open telemetry
- Loading branch information
Showing
4 changed files
with
110 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
80 changes: 80 additions & 0 deletions
80
src/RequestInsurance/OpenTelemetry/RequestInsuranceInstrumentation.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,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]); | ||
} | ||
} | ||
} |
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,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(); |