Skip to content

Commit 1e2fa80

Browse files
Auto instrumentation hooks for PHP Session (#433)
* Add PHp Session extension auto instrumentation hooks * Run Session testcase in CI * Fix 8.4 CI * Add empty Unit test folder * Add more session functions to the list * Addressed review comments * redacted cookie value from span * Refactored code to use switch statement * Updated session-destroy instrumentation to ad sessionid and name * Using return value to set the span status * removed exception handler since session function doesnt throw exception in any case * Storing cookie keys in array and setting only error status * Removed unnecessary check around cookie value --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent afc575f commit 1e2fa80

16 files changed

+681
-0
lines changed

.github/workflows/php.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ jobs:
5050
'Instrumentation/Psr16',
5151
'Instrumentation/Psr18',
5252
'Instrumentation/ReactPHP',
53+
'Instrumentation/Session',
5354
'Instrumentation/Slim',
5455
'Instrumentation/Symfony',
5556
'Instrumentation/Yii',
@@ -83,6 +84,8 @@ jobs:
8384
php-version: 8.1
8485
- project: 'Instrumentation/PostgreSql'
8586
php-version: 8.1
87+
- project: 'Instrumentation/Session'
88+
php-version: 8.1
8689
steps:
8790
- uses: actions/checkout@v4
8891

.gitsplit.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ splits:
6060
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-psr18.git"
6161
- prefix: "src/Instrumentation/ReactPHP"
6262
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-reactphp.git"
63+
- prefix: "src/Instrumentation/Session"
64+
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-session.git"
6365
- prefix: "src/Instrumentation/Slim"
6466
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-slim.git"
6567
- prefix: "src/Instrumentation/Symfony"

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"OpenTelemetry\\Contrib\\Instrumentation\\Psr16\\": "src/Instrumentation/Psr16/src",
4141
"OpenTelemetry\\Contrib\\Instrumentation\\Psr18\\": "src/Instrumentation/Psr18/src",
4242
"OpenTelemetry\\Contrib\\Instrumentation\\ReactPHP\\": "src/Instrumentation/ReactPHP/src",
43+
"OpenTelemetry\\Contrib\\Instrumentation\\Session\\": "src/Instrumentation/Session/src",
4344
"OpenTelemetry\\Contrib\\Instrumentation\\Slim\\": "src/Instrumentation/Slim/src",
4445
"OpenTelemetry\\Contrib\\Instrumentation\\Symfony\\": "src/Instrumentation/Symfony/src",
4546
"OpenTelemetry\\Contrib\\Instrumentation\\Wordpress\\": "src/Instrumentation/Wordpress/src",
@@ -81,6 +82,7 @@
8182
"src/Instrumentation/Psr16/_register.php",
8283
"src/Instrumentation/Psr18/_register.php",
8384
"src/Instrumentation/ReactPHP/_register.php",
85+
"src/Instrumentation/Session/_register.php",
8486
"src/Instrumentation/Slim/_register.php",
8587
"src/Instrumentation/Symfony/_register.php",
8688
"src/Instrumentation/Wordpress/_register.php",
@@ -115,6 +117,7 @@
115117
"OpenTelemetry\\Instrumentation\\Psr14\\Tests\\": "src/Instrumentation/Psr14/tests",
116118
"OpenTelemetry\\Tests\\Instrumentation\\Psr16\\": "src/Instrumentation/Psr16/tests",
117119
"OpenTelemetry\\Tests\\Instrumentation\\ReactPHP\\": "src/Instrumentation/ReactPHP/tests",
120+
"OpenTelemetry\\Tests\\Instrumentation\\Session\\": "src/Instrumentation/Session/tests",
118121
"OpenTelemetry\\Tests\\Instrumentation\\Slim\\": "src/Instrumentation/Slim/tests",
119122
"OpenTelemetry\\Tests\\Instrumentation\\Symfony\\tests\\": "src/Instrumentation/Symfony/tests",
120123
"OpenTelemetry\\Tests\\Instrumentation\\Wordpress\\": "src/Instrumentation/Wordpress/tests",
@@ -156,6 +159,7 @@
156159
"open-telemetry/opentelemetry-auto-psr16": "self.version",
157160
"open-telemetry/opentelemetry-auto-psr18": "self.version",
158161
"open-telemetry/opentelemetry-auto-reactphp": "self.version",
162+
"open-telemetry/opentelemetry-auto-php-session": "self.version",
159163
"open-telemetry/opentelemetry-auto-slim": "self.version",
160164
"open-telemetry/opentelemetry-auto-symfony": "self.version",
161165
"open-telemetry/opentelemetry-auto-wordpress": "self.version",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
* text=auto
2+
3+
*.md diff=markdown
4+
*.php diff=php
5+
6+
/.gitattributes export-ignore
7+
/.gitignore export-ignore
8+
/.php-cs-fixer.php export-ignore
9+
/phpstan.neon.dist export-ignore
10+
/phpunit.xml.dist export-ignore
11+
/psalm.xml.dist export-ignore
12+
/tests export-ignore
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
$finder = PhpCsFixer\Finder::create()
3+
->exclude('vendor')
4+
->exclude('var/cache')
5+
->in(__DIR__);
6+
7+
$config = new PhpCsFixer\Config();
8+
return $config->setRules([
9+
'concat_space' => ['spacing' => 'one'],
10+
'declare_equal_normalize' => ['space' => 'none'],
11+
'is_null' => true,
12+
'modernize_types_casting' => true,
13+
'ordered_imports' => true,
14+
'php_unit_construct' => true,
15+
'single_line_comment_style' => true,
16+
'yoda_style' => false,
17+
'@PSR2' => true,
18+
'array_syntax' => ['syntax' => 'short'],
19+
'blank_line_after_opening_tag' => true,
20+
'blank_line_before_statement' => true,
21+
'cast_spaces' => true,
22+
'declare_strict_types' => true,
23+
'type_declaration_spaces' => true,
24+
'include' => true,
25+
'lowercase_cast' => true,
26+
'new_with_parentheses' => true,
27+
'no_extra_blank_lines' => true,
28+
'no_leading_import_slash' => true,
29+
'echo_tag_syntax' => true,
30+
'no_unused_imports' => true,
31+
'no_useless_else' => true,
32+
'no_useless_return' => true,
33+
'phpdoc_order' => true,
34+
'phpdoc_scalar' => true,
35+
'phpdoc_types' => true,
36+
'short_scalar_cast' => true,
37+
'blank_lines_before_namespace' => true,
38+
'single_quote' => true,
39+
'trailing_comma_in_multiline' => true,
40+
])
41+
->setRiskyAllowed(true)
42+
->setFinder($finder);

src/Instrumentation/Session/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# PHP Session Auto-instrumentation for OpenTelemetry
2+
3+
This package provides auto-instrumentation for PHP's native session functions.
4+
5+
## Installation
6+
7+
```bash
8+
composer require open-telemetry/opentelemetry-auto-session
9+
```
10+
11+
## Usage
12+
13+
The instrumentation hooks into PHP's native session functions to provide tracing capabilities. It automatically creates spans for session operations like `session_start()` and `session_destroy()`.
14+
15+
```php
16+
<?php
17+
// Make sure to initialize the OpenTelemetry SDK before using sessions
18+
// ...
19+
20+
// Register the PHP Session instrumentation
21+
\OpenTelemetry\Contrib\Instrumentation\PhpSession\PhpSessionInstrumentation::register();
22+
23+
// Now any session operations will be automatically traced
24+
session_start();
25+
// ... your code ...
26+
session_destroy();
27+
```
28+
29+
## Spans and Attributes
30+
31+
### session.start
32+
33+
When `session_start()` is called, a span named `session.start` is created with the following attributes:
34+
35+
- `code.function_name`: The function name (`session_start`)
36+
- `php.session.options.*`: Any options passed to `session_start()`
37+
- `php.session.id`: The session ID (if session was successfully started)
38+
- `php.session.name`: The session name (if session was successfully started)
39+
- `php.session.status`: Either "active" or "inactive"
40+
- `php.session.cookie.*`: Session cookie parameters
41+
42+
### session.destroy
43+
44+
When `session_destroy()` is called, a span named `session.destroy` is created with the following attributes:
45+
46+
- `code.function_name`: The function name (`session_destroy`)
47+
- `php.session.id`: The session ID (if available)
48+
- `php.session.name`: The session name (if available)
49+
50+
### session.write_close
51+
52+
When `session_write_close()` is called, a span named `session.write_close` is created with the following attributes:
53+
54+
- `code.function.name`: The function name (`session_write_close`)
55+
- `php.session.id`: The session ID (if available)
56+
- `php.session.name`: The session name (if available)
57+
58+
### session.unset
59+
60+
When `session_unset()` is called, a span named `session.unset` is created with the following attributes:
61+
62+
- `code.function.name`: The function name (`session_unset`)
63+
- `php.session.id`: The session ID (if available)
64+
- `php.session.name`: The session name (if available)
65+
66+
### session.abort
67+
68+
When `session_abort()` is called, a span named `session.abort` is created with the following attributes:
69+
70+
- `code.function.name`: The function name (`session_abort`)
71+
- `php.session.id`: The session ID (if available)
72+
- `php.session.name`: The session name (if available)
73+
74+
## License
75+
76+
Apache 2.0
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use OpenTelemetry\Contrib\Instrumentation\Session\PhpSessionInstrumentation;
6+
use OpenTelemetry\SDK\Sdk;
7+
8+
if (class_exists(Sdk::class) && Sdk::isInstrumentationDisabled(PhpSessionInstrumentation::NAME) === true) {
9+
return;
10+
}
11+
12+
if (extension_loaded('opentelemetry') === false) {
13+
trigger_error('The opentelemetry extension must be loaded in order to autoload the OpenTelemetry Session auto-instrumentation', E_USER_WARNING);
14+
15+
return;
16+
}
17+
18+
PhpSessionInstrumentation::register();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "open-telemetry/opentelemetry-auto-session",
3+
"description": "OpenTelemetry auto-instrumentation for PHP Session functions",
4+
"keywords": ["opentelemetry", "otel", "open-telemetry", "tracing", "php", "session"],
5+
"type": "library",
6+
"homepage": "https://opentelemetry.io/docs/php",
7+
"readme": "./README.md",
8+
"license": "Apache-2.0",
9+
"require": {
10+
"php": "^8.2",
11+
"ext-opentelemetry": "*",
12+
"ext-session": "*",
13+
"open-telemetry/api": "^1.0",
14+
"open-telemetry/sem-conv": "^1.0"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"OpenTelemetry\\Contrib\\Instrumentation\\PhpSession\\": "src/"
19+
}
20+
},
21+
"require-dev": {
22+
"friendsofphp/php-cs-fixer": "^3",
23+
"phan/phan": "^5.0",
24+
"php-http/mock-client": "*",
25+
"phpstan/phpstan": "^1.1",
26+
"phpstan/phpstan-phpunit": "^1.0",
27+
"psalm/plugin-phpunit": "^0.19.2",
28+
"open-telemetry/sdk": "^1.0",
29+
"phpunit/phpunit": "^9.5",
30+
"vimeo/psalm": "6.4.0"
31+
}
32+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
includes:
2+
- vendor/phpstan/phpstan-phpunit/extension.neon
3+
4+
parameters:
5+
tmpDir: var/cache/phpstan
6+
level: 5
7+
paths:
8+
- src
9+
- tests

0 commit comments

Comments
 (0)