-
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.
Feature/auto instrumentation/cake php (#253)
- Loading branch information
0 parents
commit 1acecf1
Showing
12 changed files
with
298 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
* text=auto | ||
|
||
*.md diff=markdown | ||
*.php diff=php | ||
|
||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
/.phan export-ignore | ||
/.php-cs-fixer.php export-ignore | ||
/phpstan.neon.dist export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/psalm.xml.dist export-ignore | ||
/tests export-ignore |
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 @@ | ||
/vendor/ |
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,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); | ||
|
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,18 @@ | ||
|
||
This is a read-only subtree split of https://github.com/open-telemetry/opentelemetry-php-contrib. | ||
|
||
# OpenTelemetry Slim Framework auto-instrumentation | ||
Please read https://opentelemetry.io/docs/instrumentation/php/automatic/ for instructions on how to | ||
install and configure the extension and SDK. | ||
|
||
## Overview | ||
Auto-instrumentation hooks are registered via composer, and spans will automatically be created for: | ||
- `::handle()` - root span | ||
|
||
## Configuration | ||
|
||
The extension can be disabled via [runtime configuration](https://opentelemetry.io/docs/instrumentation/php/sdk/#configuration): | ||
|
||
```shell | ||
OTEL_PHP_DISABLED_INSTRUMENTATIONS=cakephp | ||
``` |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use OpenTelemetry\Contrib\Instrumentation\CakePHP\CakePHPInstrumentation; | ||
use OpenTelemetry\SDK\Sdk; | ||
|
||
if (class_exists(Sdk::class) && Sdk::isInstrumentationDisabled(CakePHPInstrumentation::NAME) === true) { | ||
return; | ||
} | ||
|
||
if (extension_loaded('opentelemetry') === false) { | ||
trigger_error('The opentelemetry extension must be loaded in order to autoload the OpenTelemetry CakePHP Framework auto-instrumentation', E_USER_WARNING); | ||
|
||
return; | ||
} | ||
|
||
CakePHPInstrumentation::register(); |
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,50 @@ | ||
{ | ||
"name": "open-telemetry/opentelemetry-auto-cakephp", | ||
"description": "OpenTelemetry auto-instrumentation for CakePHP Framework.", | ||
"keywords": ["opentelemetry", "otel", "open-telemetry", "tracing", "cakephp", "instrumentation"], | ||
"type": "library", | ||
"homepage": "https://opentelemetry.io/docs/php", | ||
"readme": "./README.md", | ||
"license": "Apache-2.0", | ||
"minimum-stability": "dev", | ||
"prefer-stable": true, | ||
"require": { | ||
"php": "^8.0", | ||
"ext-opentelemetry": "*", | ||
"cakephp/cakephp": ">=4.0", | ||
"open-telemetry/api": "^1.0", | ||
"open-telemetry/sem-conv": "^1.23" | ||
}, | ||
"require-dev": { | ||
"friendsofphp/php-cs-fixer": "^3", | ||
"mockery/mockery": "^1.5", | ||
"nyholm/psr7": "*", | ||
"phan/phan": "^5.0", | ||
"php-http/mock-client": "*", | ||
"phpstan/phpstan-mockery": "^1.1.0", | ||
"phpstan/phpstan": "^1.1", | ||
"phpstan/phpstan-phpunit": "^1.0", | ||
"psalm/plugin-phpunit": "^0.18.4", | ||
"open-telemetry/sdk": "^1.0", | ||
"phpunit/phpunit": "^9.5", | ||
"vimeo/psalm": "^5.24" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"OpenTelemetry\\Contrib\\Instrumentation\\CakePHP\\": "src/" | ||
}, | ||
"files": [ | ||
"_register.php" | ||
] | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"OpenTelemetry\\Tests\\Instrumentation\\CakePHP\\": "tests/" | ||
} | ||
}, | ||
"config": { | ||
"allow-plugins": { | ||
"php-http/discovery": true | ||
} | ||
} | ||
} |
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,11 @@ | ||
includes: | ||
- vendor/phpstan/phpstan-phpunit/extension.neon | ||
|
||
parameters: | ||
tmpDir: var/cache/phpstan | ||
level: 5 | ||
paths: | ||
- src | ||
- tests | ||
excludePaths: | ||
- tests/* |
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,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> |
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,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> |
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,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Instrumentation\CakePHP; | ||
|
||
use Cake\Controller\Controller; | ||
use OpenTelemetry\API\Globals; | ||
use OpenTelemetry\API\Instrumentation\CachedInstrumentation; | ||
use OpenTelemetry\API\Trace\Span; | ||
use OpenTelemetry\API\Trace\SpanKind; | ||
use OpenTelemetry\API\Trace\StatusCode; | ||
use OpenTelemetry\Context\Context; | ||
use function OpenTelemetry\Instrumentation\hook; | ||
use OpenTelemetry\SemConv\TraceAttributes; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Throwable; | ||
|
||
class CakePHPInstrumentation | ||
{ | ||
public const NAME = 'cakephp'; | ||
|
||
public static function register(): void | ||
{ | ||
$instrumentation = new CachedInstrumentation('io.opentelemetry.contrib.php.cakephp'); | ||
|
||
hook( | ||
Controller::class, | ||
'invokeAction', | ||
pre: static function (Controller $app, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($instrumentation) { | ||
$request = $app->getRequest(); | ||
/** @psalm-suppress ArgumentTypeCoercion */ | ||
$builder = $instrumentation->tracer() | ||
->spanBuilder($request->getMethod()) | ||
->setSpanKind(SpanKind::KIND_SERVER) | ||
->setAttribute(TraceAttributes::CODE_FUNCTION, $function) | ||
->setAttribute(TraceAttributes::CODE_NAMESPACE, $class) | ||
->setAttribute(TraceAttributes::CODE_FILEPATH, $filename) | ||
->setAttribute(TraceAttributes::CODE_LINENO, $lineno); | ||
|
||
$parent = Globals::propagator()->extract($request->getHeaders()); | ||
$span = $builder | ||
->setParent($parent) | ||
->setAttribute(TraceAttributes::URL_FULL, $request->getUri()->__toString()) | ||
->setAttribute(TraceAttributes::HTTP_REQUEST_METHOD, $request->getMethod()) | ||
->setAttribute(TraceAttributes::HTTP_REQUEST_BODY_SIZE, $request->getHeaderLine('Content-Length')) | ||
->setAttribute(TraceAttributes::USER_AGENT_ORIGINAL, $request->getHeaderLine('User-Agent')) | ||
->setAttribute(TraceAttributes::SERVER_ADDRESS, $request->getUri()->getHost()) | ||
->setAttribute(TraceAttributes::SERVER_PORT, $request->getUri()->getPort()) | ||
->setAttribute(TraceAttributes::URL_SCHEME, $request->getUri()->getScheme()) | ||
->setAttribute(TraceAttributes::URL_PATH, $request->getUri()->getPath()) | ||
->startSpan(); | ||
|
||
Context::storage()->attach($span->storeInContext($parent)); | ||
}, | ||
post: static function (Controller $app, array $params, ?ResponseInterface $response, ?Throwable $exception) { | ||
$scope = Context::storage()->scope(); | ||
if (!$scope) { | ||
return; | ||
} | ||
$scope->detach(); | ||
$span = Span::fromContext($scope->context()); | ||
$response = $app->getResponse(); | ||
if ($exception) { | ||
$span->recordException($exception, [TraceAttributes::EXCEPTION_ESCAPED => true]); | ||
$span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage()); | ||
} | ||
/** @var ResponseInterface|null $response */ | ||
if ($response) { | ||
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') ?: null); | ||
} | ||
|
||
$span->end(); | ||
}, | ||
); | ||
} | ||
} |
Empty file.
Empty file.