Skip to content

Commit e3d01c3

Browse files
committed
Add opcache instrumentation
1 parent fec51f9 commit e3d01c3

File tree

18 files changed

+765
-0
lines changed

18 files changed

+765
-0
lines changed

.github/workflows/php.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ jobs:
4141
'Instrumentation/MySqli',
4242
'Instrumentation/OpenAIPHP',
4343
'Instrumentation/PDO',
44+
'Instrumentation/PhpOpcache',
4445
'Instrumentation/PostgreSql',
4546
# Sort PSRs numerically.
4647
'Instrumentation/Psr3',
@@ -82,6 +83,8 @@ jobs:
8283
php-version: 8.1
8384
- project: 'Instrumentation/PostgreSql'
8485
php-version: 8.1
86+
- project: 'Instrumentation/PhpOpcache'
87+
php-version: 8.1
8588
steps:
8689
- uses: actions/checkout@v4
8790

.gitsplit.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ splits:
4444
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-openai.git"
4545
- prefix: "src/Instrumentation/PDO"
4646
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-pdo.git"
47+
- prefix: "src/Instrumentation/PhpOpcache"
48+
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-opcache.git"
4749
- prefix: "src/Instrumentation/PostgreSql"
4850
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-postgresql.git"
4951
- prefix: "src/Instrumentation/Psr3"

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"OpenTelemetry\\Contrib\\Instrumentation\\MySqli\\": "src/Instrumentation/MySqli/src",
3434
"OpenTelemetry\\Contrib\\Instrumentation\\OpenAIPHP\\": "src/Instrumentation/OpenAIPHP/src",
3535
"OpenTelemetry\\Contrib\\Instrumentation\\PDO\\": "src/Instrumentation/PDO/src",
36+
"OpenTelemetry\\Contrib\\Instrumentation\\PhpOpcache\\": "src/Instrumentation/PhpOpcache/src",
3637
"OpenTelemetry\\Contrib\\Instrumentation\\Psr3\\": "src/Instrumentation/Psr3/src",
3738
"OpenTelemetry\\Contrib\\Instrumentation\\Psr6\\": "src/Instrumentation/Psr6/src",
3839
"OpenTelemetry\\Contrib\\Instrumentation\\Psr14\\": "src/Instrumentation/Psr14/src",
@@ -74,6 +75,7 @@
7475
"src/Instrumentation/MySqli/_register.php",
7576
"src/Instrumentation/OpenAIPHP/_register.php",
7677
"src/Instrumentation/PDO/_register.php",
78+
"src/Instrumentation/phpOpcache/_register.php",
7779
"src/Instrumentation/Psr3/_register.php",
7880
"src/Instrumentation/Psr6/_register.php",
7981
"src/Instrumentation/Psr14/_register.php",
@@ -111,6 +113,7 @@
111113
"OpenTelemetry\\Tests\\Instrumentation\\MySqli\\": "src/Instrumentation/MySqli/tests",
112114
"OpenTelemetry\\Contrib\\Instrumentation\\OpenAIPHP\\Tests\\": "src/Instrumentation/OpenAIPHP/tests",
113115
"OpenTelemetry\\Tests\\Instrumentation\\PDO\\": "src/Instrumentation/PDO/tests",
116+
"OpenTelemetry\\Tests\\Instrumentation\\PhpOpcache\\": "src/Instrumentation/PhpOpcache/tests",
114117
"OpenTelemetry\\Tests\\Instrumentation\\Psr6\\": "src/Instrumentation/Psr6/tests",
115118
"OpenTelemetry\\Instrumentation\\Psr14\\Tests\\": "src/Instrumentation/Psr14/tests",
116119
"OpenTelemetry\\Tests\\Instrumentation\\Psr16\\": "src/Instrumentation/Psr16/tests",
@@ -149,6 +152,7 @@
149152
"open-telemetry/opentelemetry-auto-mysqli": "self.version",
150153
"open-telemetry/opentelemetry-auto-openai-php": "self.version",
151154
"open-telemetry/opentelemetry-auto-pdo": "self.version",
155+
"open-telemetry/opentelemetry-auto-phpOpcache": "self.version",
152156
"open-telemetry/opentelemetry-auto-psr3": "self.version",
153157
"open-telemetry/opentelemetry-auto-psr6": "self.version",
154158
"open-telemetry/opentelemetry-auto-psr14": "self.version",

docker-compose.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ services:
44
image: ghcr.io/open-telemetry/opentelemetry-php/opentelemetry-php-base:${PHP_VERSION}
55
volumes:
66
- ./:/usr/src/myapp
7+
- ./docker/opcache/opcache.ini:/etc/php/${PHP_VERSION}/cli/conf.d/opcache.ini
78
user: "${PHP_USER}:root"
89
environment:
910
XDEBUG_MODE: ${XDEBUG_MODE:-off}

docker/opcache/opcache.ini

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; OPcache configuration
2+
[opcache]
3+
opcache.enable=1
4+
opcache.enable_cli=1
5+
opcache.memory_consumption=128
6+
opcache.interned_strings_buffer=8
7+
opcache.max_accelerated_files=10000
8+
opcache.revalidate_freq=60
9+
opcache.validate_timestamps=1
10+
opcache.save_comments=1
11+
opcache.enable_file_override=0
12 KB
Binary file not shown.
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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);
43+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[![Releases](https://img.shields.io/badge/releases-purple)](https://github.com/opentelemetry-php/contrib-auto-opcache/releases)
2+
[![Issues](https://img.shields.io/badge/issues-pink)](https://github.com/open-telemetry/opentelemetry-php/issues)
3+
[![Source](https://img.shields.io/badge/source-contrib-green)](https://github.com/open-telemetry/opentelemetry-php-contrib/tree/main/src/Instrumentation/PhpOpcache)
4+
[![Mirror](https://img.shields.io/badge/mirror-opentelemetry--php--contrib-blue)](https://github.com/opentelemetry-php/contrib-auto-opcache)
5+
[![Latest Version](http://poser.pugx.org/open-telemetry/opentelemetry-auto-opcache/v/unstable)](https://packagist.org/packages/open-telemetry/opentelemetry-auto-opcache/)
6+
[![Stable](http://poser.pugx.org/open-telemetry/opentelemetry-auto-opcache/v/stable)](https://packagist.org/packages/open-telemetry/opentelemetry-auto-opcache/)
7+
8+
This is a read-only subtree split of https://github.com/open-telemetry/opentelemetry-php-contrib.
9+
10+
# OpenTelemetry PHP OPcache Instrumentation
11+
12+
Please read https://opentelemetry.io/docs/instrumentation/php/automatic/ for instructions on how to
13+
install and configure the extension and SDK.
14+
15+
## Overview
16+
17+
This instrumentation package captures PHP OPcache metrics and adds them as attributes to the active span.
18+
It automatically registers a shutdown function to collect OPcache metrics at the end of the request.
19+
20+
The following OPcache metrics are captured:
21+
22+
### Basic Status
23+
- `opcache.enabled` - Whether OPcache is enabled
24+
- `opcache.available` - Whether OPcache is available
25+
26+
### Memory Usage
27+
- `opcache.memory.used_bytes` - Memory used by OPcache in bytes
28+
- `opcache.memory.free_bytes` - Free memory available to OPcache in bytes
29+
- `opcache.memory.wasted_bytes` - Wasted memory in bytes
30+
- `opcache.memory.used_percentage` - Percentage of total memory used
31+
- `opcache.memory.wasted_percentage` - Percentage of total memory wasted
32+
33+
### Cache Statistics
34+
- `opcache.scripts.cached` - Number of cached scripts
35+
- `opcache.hits.total` - Total number of cache hits
36+
- `opcache.misses.total` - Total number of cache misses
37+
- `opcache.hit_rate.percentage` - Cache hit rate percentage
38+
- `opcache.keys.cached` - Number of cached keys
39+
- `opcache.keys.max_cached` - Maximum number of cached keys
40+
41+
### Restart Statistics
42+
- `opcache.restarts.oom` - Number of out-of-memory restarts
43+
- `opcache.restarts.hash` - Number of hash restarts
44+
- `opcache.restarts.manual` - Number of manual restarts
45+
46+
### Interned Strings
47+
- `opcache.interned_strings.buffer_size` - Interned strings buffer size
48+
- `opcache.interned_strings.used_memory` - Memory used by interned strings
49+
- `opcache.interned_strings.free_memory` - Free memory for interned strings
50+
- `opcache.interned_strings.strings_count` - Number of interned strings
51+
- `opcache.interned_strings.usage_percentage` - Percentage of interned strings buffer used
52+
53+
## Usage
54+
55+
The instrumentation is automatically registered via composer. No additional configuration is required.
56+
57+
You can also manually add OPcache metrics to the current active span:
58+
59+
```php
60+
use OpenTelemetry\Contrib\Instrumentation\PhpOpcache\PhpOpcacheInstrumentation;
61+
62+
// Add OPcache metrics to the current active span
63+
PhpOpcacheInstrumentation::addOpcacheMetricsToRootSpan();
64+
```
65+
66+
## Configuration
67+
68+
The extension can be disabled via [runtime configuration](https://opentelemetry.io/docs/instrumentation/php/sdk/#configuration):
69+
70+
```shell
71+
OTEL_PHP_DISABLED_INSTRUMENTATIONS=opcache
72+
```
73+
74+
## Requirements
75+
76+
- PHP 8.0 or higher
77+
- OPcache extension
78+
- OpenTelemetry extension

0 commit comments

Comments
 (0)