Skip to content

Commit

Permalink
Yii2 framework instrumentation (#212)
Browse files Browse the repository at this point in the history
* Yii2 instrumentation
* Address PR comments.
* Fix psalm issue
agoallikmaa authored Nov 22, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 8665109 commit f3e21f4
Showing 16 changed files with 680 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ jobs:
'Instrumentation/Laravel',
'Instrumentation/MongoDB',
'Instrumentation/CodeIgniter',
'Instrumentation/Yii',
'Logs/Monolog',
'Propagation/TraceResponse',
'ResourceDetectors/Container',
@@ -54,6 +55,8 @@ jobs:
php-version: 7.4
- project: 'Instrumentation/CodeIgniter'
php-version: 7.4
- project: 'Instrumentation/Yii'
php-version: 7.4
- project: 'Instrumentation/IO'
php-version: 8.0
- project: 'Instrumentation/IO'
2 changes: 2 additions & 0 deletions .gitsplit.yml
Original file line number Diff line number Diff line change
@@ -36,6 +36,8 @@ splits:
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-symfony.git"
- prefix: "src/Instrumentation/Wordpress"
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-wordpress.git"
- prefix: "src/Instrumentation/Yii"
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-yii.git"
- prefix: "src/Context/Swoole"
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/context-swoole.git"
- prefix: "src/AutoInstrumentationInstaller"
42 changes: 42 additions & 0 deletions src/Instrumentation/Yii/.php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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);
25 changes: 25 additions & 0 deletions src/Instrumentation/Yii/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[![Releases](https://img.shields.io/badge/releases-purple)](https://github.com/opentelemetry-php/contrib-auto-yii/releases)
[![Issues](https://img.shields.io/badge/issues-pink)](https://github.com/open-telemetry/opentelemetry-php/issues)
[![Source](https://img.shields.io/badge/source-contrib-green)](https://github.com/open-telemetry/opentelemetry-php-contrib/tree/main/src/Instrumentation/Yii)
[![Mirror](https://img.shields.io/badge/mirror-opentelemetry--php--contrib-blue)](https://github.com/opentelemetry-php/contrib-auto-yii)
[![Latest Version](http://poser.pugx.org/open-telemetry/opentelemetry-auto-yii/v/unstable)](https://packagist.org/packages/open-telemetry/opentelemetry-auto-yii/)
[![Stable](http://poser.pugx.org/open-telemetry/opentelemetry-auto-yii/v/stable)](https://packagist.org/packages/open-telemetry/opentelemetry-auto-yii/)

This is a read-only subtree split of https://github.com/open-telemetry/opentelemetry-php-contrib.

# OpenTelemetry Yii auto-instrumentation

Please read https://opentelemetry.io/docs/instrumentation/php/automatic/ for instructions on how to
install and configure the extension and SDK.

## Overview

Requires Yii 2.0.13+

## Configuration

The extension can be disabled via [runtime configuration](https://opentelemetry.io/docs/instrumentation/php/sdk/#configuration):

```shell
OTEL_PHP_DISABLED_INSTRUMENTATIONS=yii
```
18 changes: 18 additions & 0 deletions src/Instrumentation/Yii/_register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use OpenTelemetry\Contrib\Instrumentation\Yii\YiiInstrumentation;
use OpenTelemetry\SDK\Sdk;

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

if (extension_loaded('opentelemetry') === false) {
trigger_error('The opentelemetry extension must be loaded in order to autoload the OpenTelemetry Yii auto-instrumentation', E_USER_WARNING);

return;
}

YiiInstrumentation::register();
52 changes: 52 additions & 0 deletions src/Instrumentation/Yii/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "open-telemetry/opentelemetry-auto-yii",
"description": "OpenTelemetry auto-instrumentation for Yii",
"keywords": ["opentelemetry", "otel", "open-telemetry", "tracing", "yii", "instrumentation"],
"type": "library",
"homepage": "https://opentelemetry.io/docs/php",
"readme": "./README.md",
"license": "Apache-2.0",
"require": {
"php": "^8.0",
"ext-opentelemetry": "*",
"yiisoft/yii2": "^2.0.13",
"open-telemetry/api": "^1",
"open-telemetry/sem-conv": "^1.22"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3",
"open-telemetry/sdk": "^1.0",
"phan/phan": "^5.0",
"php-http/mock-client": "*",
"phpstan/phpstan": "^1.1",
"phpstan/phpstan-phpunit": "^1.0",
"psalm/plugin-phpunit": "^0.16",
"phpunit/phpunit": "^9.5",
"vimeo/psalm": "^4.0"
},
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
],
"autoload": {
"psr-4": {
"OpenTelemetry\\Contrib\\Instrumentation\\Yii\\": "src/"
},
"files": [
"_register.php"
]
},
"autoload-dev": {
"psr-4": {
"OpenTelemetry\\Tests\\Instrumentation\\Yii\\": "tests/"
}
},
"config": {
"allow-plugins": {
"php-http/discovery": false,
"yiisoft/yii2-composer": false
}
}
}
11 changes: 11 additions & 0 deletions src/Instrumentation/Yii/phpstan.neon.dist
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
bootstrapFiles:
- tests/bootstrap.php
level: 5
paths:
- src
- tests
48 changes: 48 additions & 0 deletions src/Instrumentation/Yii/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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"
bootstrap="tests/bootstrap.php"
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>
18 changes: 18 additions & 0 deletions src/Instrumentation/Yii/psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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"/>
<ignoreFiles>
<directory name="vendor/yiisoft/yii2" />
</ignoreFiles>
</projectFiles>
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>
</psalm>
44 changes: 44 additions & 0 deletions src/Instrumentation/Yii/src/RequestPropagationGetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Contrib\Instrumentation\Yii;

use function assert;
use OpenTelemetry\Context\Propagation\PropagationGetterInterface;
use yii\web\Request;

/**
* @internal
*/
final class RequestPropagationGetter implements PropagationGetterInterface
{
public static function instance(): self
{
static $instance;

return $instance ??= new self();
}

/** @psalm-suppress InvalidReturnType */
public function keys($carrier): array
{
assert($carrier instanceof Request);

return array_keys($carrier->getHeaders()->toArray());
}

public function get($carrier, string $key) : ?string
{
assert($carrier instanceof Request);

$result = $carrier->getHeaders()->get($key, null, true);

if (is_array($result)) {
return (string) array_values($result)[0];
}

return $result;

}
}
37 changes: 37 additions & 0 deletions src/Instrumentation/Yii/src/ResponsePropagationSetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Contrib\Instrumentation\Yii;

use function assert;
use OpenTelemetry\Context\Propagation\PropagationSetterInterface;
use yii\web\Response;

/**
* @internal
*/
final class ResponsePropagationSetter implements PropagationSetterInterface
{
public static function instance(): self
{
static $instance;

return $instance ??= new self();
}

/** @psalm-suppress InvalidReturnType */
public function keys($carrier): array
{
assert($carrier instanceof Response);

return array_keys($carrier->getHeaders()->toArray());
}

public function set(&$carrier, string $key, string $value): void
{
assert($carrier instanceof Response);

$carrier->getHeaders()->set($key, $value);
}
}
Loading

0 comments on commit f3e21f4

Please sign in to comment.