-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More signals for cakephp auto instrumentation (#274)
* refactor: move controller hook into a separate class * feat: add http.route attribute to controller span * feat: add traces and metrics for Server::run calls * feat(CakePHP): add traces for Command::execute calls * style(CakePHP): fix code style * chore: add suggestions for other auto-instrumentation libraries * fix: add schema URL to cached instrumentation * fix: remove metrics because of issues with shared-nothing setups * fix: use TraceAttributes::SCHEMA_URL instead of string constant
- Loading branch information
1 parent
7bf707e
commit 662e25a
Showing
23 changed files
with
1,475 additions
and
194 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/vendor/ | ||
/tests/Integration/App/tmp/ |
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 |
---|---|---|
@@ -1,47 +1,22 @@ | ||
<?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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" bootstrap="tests/bootstrap.php" cacheResult="false" colors="false" processIsolation="false" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false" stopOnRisky="false" timeoutForSmallTests="1" timeoutForMediumTests="10" timeoutForLargeTests="60" cacheDirectory=".phpunit.cache" backupStaticProperties="false" requireCoverageMetadata="false"> | ||
<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> | ||
<source> | ||
<include> | ||
<directory>src</directory> | ||
</include> | ||
</source> | ||
</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
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
66 changes: 66 additions & 0 deletions
66
src/Instrumentation/CakePHP/src/Hooks/Cake/Command/Command.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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Instrumentation\CakePHP\Hooks\Cake\Command; | ||
|
||
use OpenTelemetry\API\Trace\Span; | ||
use OpenTelemetry\API\Trace\StatusCode; | ||
use OpenTelemetry\Context\Context; | ||
use OpenTelemetry\Contrib\Instrumentation\CakePHP\Hooks\CakeHook; | ||
use OpenTelemetry\Contrib\Instrumentation\CakePHP\Hooks\CakeHookTrait; | ||
use function OpenTelemetry\Instrumentation\hook; | ||
use OpenTelemetry\SemConv\TraceAttributes; | ||
use Throwable; | ||
|
||
class Command implements CakeHook | ||
{ | ||
use CakeHookTrait; | ||
|
||
public function instrument(): void | ||
{ | ||
hook( | ||
\Cake\Command\Command::class, | ||
'execute', | ||
pre: function (\Cake\Command\Command $command, array $params, string $class, string $function, ?string $filename, ?int $lineno) { | ||
$builder = $this->instrumentation | ||
->tracer() | ||
->spanBuilder(sprintf('Command %s', $command->getName() ?: 'unknown')) | ||
->setAttribute(TraceAttributes::CODE_FUNCTION, $function) | ||
->setAttribute(TraceAttributes::CODE_NAMESPACE, $class) | ||
->setAttribute(TraceAttributes::CODE_FILEPATH, $filename) | ||
->setAttribute(TraceAttributes::CODE_LINENO, $lineno); | ||
|
||
$parent = Context::getCurrent(); | ||
$span = $builder->startSpan(); | ||
Context::storage()->attach($span->storeInContext($parent)); | ||
|
||
return $params; | ||
}, | ||
post: function (\Cake\Command\Command $command, array $params, ?int $exitCode, ?Throwable $exception) { | ||
$scope = Context::storage()->scope(); | ||
if (!$scope) { | ||
return; | ||
} | ||
|
||
$span = Span::fromContext($scope->context()); | ||
$span->addEvent('command finished', [ | ||
'exit-code' => $exitCode, | ||
]); | ||
|
||
$scope->detach(); | ||
$span = Span::fromContext($scope->context()); | ||
|
||
if ($exception) { | ||
$span->recordException($exception, [ | ||
TraceAttributes::EXCEPTION_ESCAPED => true, | ||
]); | ||
$span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage()); | ||
} | ||
|
||
$span->end(); | ||
|
||
}, | ||
); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/Instrumentation/CakePHP/src/Hooks/Cake/Controller/Controller.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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Instrumentation\CakePHP\Hooks\Cake\Controller; | ||
|
||
use Cake\Controller\Controller as CakeController; | ||
use OpenTelemetry\API\Trace\StatusCode; | ||
use OpenTelemetry\Context\Context; | ||
use OpenTelemetry\Contrib\Instrumentation\CakePHP\Hooks\CakeHook; | ||
use OpenTelemetry\Contrib\Instrumentation\CakePHP\Hooks\CakeHookTrait; | ||
use function OpenTelemetry\Instrumentation\hook; | ||
use OpenTelemetry\SemConv\TraceAttributes; | ||
use Throwable; | ||
|
||
class Controller implements CakeHook | ||
{ | ||
use CakeHookTrait; | ||
|
||
public function instrument(): void | ||
{ | ||
hook( | ||
CakeController::class, | ||
'invokeAction', | ||
pre: function (CakeController $app, array $params, string $class, string $function, ?string $filename, ?int $lineno) { | ||
$request = $app->getRequest(); | ||
$request = $this->buildSpan($request, $class, $function, $filename, $lineno); | ||
$app->setRequest($request); | ||
}, | ||
post: static function (CakeController $app, array $params, $return, ?Throwable $exception) { | ||
$scope = Context::storage()->scope(); | ||
if (!$scope) { | ||
return; | ||
} | ||
$scope->detach(); | ||
$span = \OpenTelemetry\API\Trace\Span::fromContext($scope->context()); | ||
if ($exception) { | ||
$span->recordException($exception, [TraceAttributes::EXCEPTION_ESCAPED => true]); | ||
$span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage()); | ||
} | ||
$response = $app->getResponse(); | ||
if ($response->getStatusCode() >= 400) { | ||
$span->setStatus(StatusCode::STATUS_ERROR); | ||
} | ||
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_STATUS_CODE, $response->getStatusCode()); | ||
$span->setAttribute(TraceAttributes::NETWORK_PROTOCOL_VERSION, $response->getProtocolVersion()); | ||
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_BODY_SIZE, $response->getHeaderLine('Content-Length')); | ||
|
||
$span->end(); | ||
}, | ||
); | ||
} | ||
} |
Oops, something went wrong.