From 4c69a8d43fdf5dcac4f8bbee2f70ac03f5adfa20 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 30 Jul 2025 01:49:21 +0000 Subject: [PATCH 1/4] chore: update dependabot configuration [skip ci] --- .github/dependabot.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 7fdc60cfd..ffc764237 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -57,10 +57,10 @@ updates: # Maintain dependencies for all packages - package-ecosystem: "composer" directories: - - "/examples/aws/AwsClientApp" - "/examples/instrumentation/Wordpress" - "/src/AutoInstrumentationInstaller" - "/src/Aws" + - "/src/Aws/examples/AwsClientApp" - "/src/Context/Swoole" - "/src/Exporter/Instana" - "/src/Instrumentation/AwsSdk" @@ -78,12 +78,12 @@ updates: - "/src/Instrumentation/MySqli" - "/src/Instrumentation/OpenAIPHP" - "/src/Instrumentation/PDO" - - "/src/Instrumentation/Psr3" - - "/src/Instrumentation/Psr6" - "/src/Instrumentation/Psr14" - "/src/Instrumentation/Psr15" - "/src/Instrumentation/Psr16" - "/src/Instrumentation/Psr18" + - "/src/Instrumentation/Psr3" + - "/src/Instrumentation/Psr6" - "/src/Instrumentation/ReactPHP" - "/src/Instrumentation/Slim" - "/src/Instrumentation/Symfony" From 2221d623bdfdee3fe5bd6a834e359d155db622b7 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 6 Aug 2025 01:50:03 +0000 Subject: [PATCH 2/4] chore: update dependabot configuration [skip ci] --- .github/dependabot.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index ffc764237..9be661d00 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -78,6 +78,7 @@ updates: - "/src/Instrumentation/MySqli" - "/src/Instrumentation/OpenAIPHP" - "/src/Instrumentation/PDO" + - "/src/Instrumentation/PostgreSql" - "/src/Instrumentation/Psr14" - "/src/Instrumentation/Psr15" - "/src/Instrumentation/Psr16" From bfdb728db72f3cbc8641fff01006a9633a672767 Mon Sep 17 00:00:00 2001 From: HeenaBansal20 Date: Tue, 16 Sep 2025 10:17:55 -0700 Subject: [PATCH 3/4] Add opcache instrumentation --- .github/workflows/php.yml | 3 + .gitsplit.yml | 2 + composer.json | 4 + docker-compose.yaml | 1 + docker/opcache/opcache.ini | 11 ++ .../PhpOpcache/.composer.json.swp | Bin 0 -> 12288 bytes src/Instrumentation/PhpOpcache/.gitattributes | 12 ++ src/Instrumentation/PhpOpcache/.gitignore | 1 + .../PhpOpcache/.php-cs-fixer.php | 43 ++++++ src/Instrumentation/PhpOpcache/README.md | 78 ++++++++++ src/Instrumentation/PhpOpcache/_register.php | 18 +++ src/Instrumentation/PhpOpcache/composer.json | 43 ++++++ .../PhpOpcache/phpstan.neon.dist | 9 ++ .../PhpOpcache/phpunit.xml.dist | 47 ++++++ src/Instrumentation/PhpOpcache/psalm.xml.dist | 15 ++ .../src/PhpOpcacheInstrumentation.php | 143 ++++++++++++++++++ .../PhpOpcacheInstrumentationTest.php | 122 +++++++++++++++ .../PhpOpcache/tests/Unit/.gitkeep | 0 18 files changed, 552 insertions(+) create mode 100644 docker/opcache/opcache.ini create mode 100644 src/Instrumentation/PhpOpcache/.composer.json.swp create mode 100644 src/Instrumentation/PhpOpcache/.gitattributes create mode 100644 src/Instrumentation/PhpOpcache/.gitignore create mode 100644 src/Instrumentation/PhpOpcache/.php-cs-fixer.php create mode 100644 src/Instrumentation/PhpOpcache/README.md create mode 100644 src/Instrumentation/PhpOpcache/_register.php create mode 100644 src/Instrumentation/PhpOpcache/composer.json create mode 100644 src/Instrumentation/PhpOpcache/phpstan.neon.dist create mode 100644 src/Instrumentation/PhpOpcache/phpunit.xml.dist create mode 100644 src/Instrumentation/PhpOpcache/psalm.xml.dist create mode 100644 src/Instrumentation/PhpOpcache/src/PhpOpcacheInstrumentation.php create mode 100644 src/Instrumentation/PhpOpcache/tests/Integration/PhpOpcacheInstrumentationTest.php create mode 100644 src/Instrumentation/PhpOpcache/tests/Unit/.gitkeep diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index e833c354f..4113ae60e 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -41,6 +41,7 @@ jobs: 'Instrumentation/MySqli', 'Instrumentation/OpenAIPHP', 'Instrumentation/PDO', + 'Instrumentation/PhpOpcache', 'Instrumentation/PostgreSql', # Sort PSRs numerically. 'Instrumentation/Psr3', @@ -82,6 +83,8 @@ jobs: php-version: 8.1 - project: 'Instrumentation/PostgreSql' php-version: 8.1 + - project: 'Instrumentation/PhpOpcache' + php-version: 8.1 steps: - uses: actions/checkout@v4 diff --git a/.gitsplit.yml b/.gitsplit.yml index 7830c9b64..a86d0f8bd 100644 --- a/.gitsplit.yml +++ b/.gitsplit.yml @@ -44,6 +44,8 @@ splits: target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-openai.git" - prefix: "src/Instrumentation/PDO" target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-pdo.git" + - prefix: "src/Instrumentation/PhpOpcache" + target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-opcache.git" - prefix: "src/Instrumentation/PostgreSql" target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-postgresql.git" - prefix: "src/Instrumentation/Psr3" diff --git a/composer.json b/composer.json index 3c64f2e40..c51a3fa5c 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,7 @@ "OpenTelemetry\\Contrib\\Instrumentation\\MySqli\\": "src/Instrumentation/MySqli/src", "OpenTelemetry\\Contrib\\Instrumentation\\OpenAIPHP\\": "src/Instrumentation/OpenAIPHP/src", "OpenTelemetry\\Contrib\\Instrumentation\\PDO\\": "src/Instrumentation/PDO/src", + "OpenTelemetry\\Contrib\\Instrumentation\\PhpOpcache\\": "src/Instrumentation/PhpOpcache/src", "OpenTelemetry\\Contrib\\Instrumentation\\Psr3\\": "src/Instrumentation/Psr3/src", "OpenTelemetry\\Contrib\\Instrumentation\\Psr6\\": "src/Instrumentation/Psr6/src", "OpenTelemetry\\Contrib\\Instrumentation\\Psr14\\": "src/Instrumentation/Psr14/src", @@ -74,6 +75,7 @@ "src/Instrumentation/MySqli/_register.php", "src/Instrumentation/OpenAIPHP/_register.php", "src/Instrumentation/PDO/_register.php", + "src/Instrumentation/phpOpcache/_register.php", "src/Instrumentation/Psr3/_register.php", "src/Instrumentation/Psr6/_register.php", "src/Instrumentation/Psr14/_register.php", @@ -111,6 +113,7 @@ "OpenTelemetry\\Tests\\Instrumentation\\MySqli\\": "src/Instrumentation/MySqli/tests", "OpenTelemetry\\Contrib\\Instrumentation\\OpenAIPHP\\Tests\\": "src/Instrumentation/OpenAIPHP/tests", "OpenTelemetry\\Tests\\Instrumentation\\PDO\\": "src/Instrumentation/PDO/tests", + "OpenTelemetry\\Tests\\Instrumentation\\PhpOpcache\\": "src/Instrumentation/PhpOpcache/tests", "OpenTelemetry\\Tests\\Instrumentation\\Psr6\\": "src/Instrumentation/Psr6/tests", "OpenTelemetry\\Instrumentation\\Psr14\\Tests\\": "src/Instrumentation/Psr14/tests", "OpenTelemetry\\Tests\\Instrumentation\\Psr16\\": "src/Instrumentation/Psr16/tests", @@ -149,6 +152,7 @@ "open-telemetry/opentelemetry-auto-mysqli": "self.version", "open-telemetry/opentelemetry-auto-openai-php": "self.version", "open-telemetry/opentelemetry-auto-pdo": "self.version", + "open-telemetry/opentelemetry-auto-phpOpcache": "self.version", "open-telemetry/opentelemetry-auto-psr3": "self.version", "open-telemetry/opentelemetry-auto-psr6": "self.version", "open-telemetry/opentelemetry-auto-psr14": "self.version", diff --git a/docker-compose.yaml b/docker-compose.yaml index a41f04f55..cfd4ab783 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -4,6 +4,7 @@ services: image: ghcr.io/open-telemetry/opentelemetry-php/opentelemetry-php-base:${PHP_VERSION} volumes: - ./:/usr/src/myapp + - ./docker/opcache/opcache.ini:/etc/php/${PHP_VERSION}/cli/conf.d/opcache.ini user: "${PHP_USER}:root" environment: XDEBUG_MODE: ${XDEBUG_MODE:-off} diff --git a/docker/opcache/opcache.ini b/docker/opcache/opcache.ini new file mode 100644 index 000000000..f060f8e72 --- /dev/null +++ b/docker/opcache/opcache.ini @@ -0,0 +1,11 @@ +; OPcache configuration +[opcache] +opcache.enable=1 +opcache.enable_cli=1 +opcache.memory_consumption=128 +opcache.interned_strings_buffer=8 +opcache.max_accelerated_files=10000 +opcache.revalidate_freq=60 +opcache.validate_timestamps=1 +opcache.save_comments=1 +opcache.enable_file_override=0 diff --git a/src/Instrumentation/PhpOpcache/.composer.json.swp b/src/Instrumentation/PhpOpcache/.composer.json.swp new file mode 100644 index 0000000000000000000000000000000000000000..d3aa2f917ab30fbc9c994b8011e8356749ff5075 GIT binary patch literal 12288 zcmeI2!HX0}6vj&l0Zoje&Bf8s(_OPu+3v2gsz!HM z$3RZG_9Q526S;CFEZSc=05Xuc~`zw`XDXs=R@3YNqPF*ROunJ+SOI zSor$V<>+*>$nZME*q{GA936Z5`JsKkyvvMI&QHJUrJN{ZJ<_pQ*UC6yddYBNL?Q=O zlB;T#e$N+7FA7m-snViGny7>`laQRN+8H@Wt;y3%wRL7(AtZBB)#>s`U(tD%k3@=s zT3KO|Z>_3lrRf6^AOgD)7`tJ7`oPN4VsF8Reaw$WM-H#-mO*PofCvx)B0vO)01+Sp zM1TmqWdxkw!@k3w_O|=_tR45vjqO(&hyW2F0z`la5CI}U1c(3;AOb{y2oQlckU&;2 z_Fz9_8}Ffc{QrOR@Bfzv8T$j8LLWizKu^JCFXbKh33iJhZ4B89*eSopQpeNAp z(9h6a=sI)``Vji>ea0R^_o1JlAE7B!L!UrLp~KJ%^zQ?>wGWSx9K*H^L4JVRIw3ce(pr tZvHle=PU9LZ#*@1?i@P@7Kd`!@>b>s`yVr|wXOgF literal 0 HcmV?d00001 diff --git a/src/Instrumentation/PhpOpcache/.gitattributes b/src/Instrumentation/PhpOpcache/.gitattributes new file mode 100644 index 000000000..1676cf825 --- /dev/null +++ b/src/Instrumentation/PhpOpcache/.gitattributes @@ -0,0 +1,12 @@ +* text=auto + +*.md diff=markdown +*.php diff=php + +/.gitattributes export-ignore +/.gitignore 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 diff --git a/src/Instrumentation/PhpOpcache/.gitignore b/src/Instrumentation/PhpOpcache/.gitignore new file mode 100644 index 000000000..57872d0f1 --- /dev/null +++ b/src/Instrumentation/PhpOpcache/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/src/Instrumentation/PhpOpcache/.php-cs-fixer.php b/src/Instrumentation/PhpOpcache/.php-cs-fixer.php new file mode 100644 index 000000000..e35fa078c --- /dev/null +++ b/src/Instrumentation/PhpOpcache/.php-cs-fixer.php @@ -0,0 +1,43 @@ +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, + 'type_declaration_spaces' => true, + 'include' => true, + 'lowercase_cast' => true, + 'new_with_parentheses' => 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, + 'blank_lines_before_namespace' => true, + 'single_quote' => true, + 'trailing_comma_in_multiline' => true, + ]) + ->setRiskyAllowed(true) + ->setFinder($finder); + diff --git a/src/Instrumentation/PhpOpcache/README.md b/src/Instrumentation/PhpOpcache/README.md new file mode 100644 index 000000000..2453ededd --- /dev/null +++ b/src/Instrumentation/PhpOpcache/README.md @@ -0,0 +1,78 @@ +[![Releases](https://img.shields.io/badge/releases-purple)](https://github.com/opentelemetry-php/contrib-auto-opcache/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/PhpOpcache) +[![Mirror](https://img.shields.io/badge/mirror-opentelemetry--php--contrib-blue)](https://github.com/opentelemetry-php/contrib-auto-opcache) +[![Latest Version](http://poser.pugx.org/open-telemetry/opentelemetry-auto-opcache/v/unstable)](https://packagist.org/packages/open-telemetry/opentelemetry-auto-opcache/) +[![Stable](http://poser.pugx.org/open-telemetry/opentelemetry-auto-opcache/v/stable)](https://packagist.org/packages/open-telemetry/opentelemetry-auto-opcache/) + +This is a read-only subtree split of https://github.com/open-telemetry/opentelemetry-php-contrib. + +# OpenTelemetry PHP OPcache Instrumentation + +Please read https://opentelemetry.io/docs/instrumentation/php/automatic/ for instructions on how to +install and configure the extension and SDK. + +## Overview + +This instrumentation package captures PHP OPcache metrics and adds them as attributes to the active span. +It automatically registers a shutdown function to collect OPcache metrics at the end of the request. + +The following OPcache metrics are captured: + +### Basic Status +- `opcache.enabled` - Whether OPcache is enabled +- `opcache.available` - Whether OPcache is available + +### Memory Usage +- `opcache.memory.used_bytes` - Memory used by OPcache in bytes +- `opcache.memory.free_bytes` - Free memory available to OPcache in bytes +- `opcache.memory.wasted_bytes` - Wasted memory in bytes +- `opcache.memory.used_percentage` - Percentage of total memory used +- `opcache.memory.wasted_percentage` - Percentage of total memory wasted + +### Cache Statistics +- `opcache.scripts.cached` - Number of cached scripts +- `opcache.hits.total` - Total number of cache hits +- `opcache.misses.total` - Total number of cache misses +- `opcache.hit_rate.percentage` - Cache hit rate percentage +- `opcache.keys.cached` - Number of cached keys +- `opcache.keys.max_cached` - Maximum number of cached keys + +### Restart Statistics +- `opcache.restarts.oom` - Number of out-of-memory restarts +- `opcache.restarts.hash` - Number of hash restarts +- `opcache.restarts.manual` - Number of manual restarts + +### Interned Strings +- `opcache.interned_strings.buffer_size` - Interned strings buffer size +- `opcache.interned_strings.used_memory` - Memory used by interned strings +- `opcache.interned_strings.free_memory` - Free memory for interned strings +- `opcache.interned_strings.strings_count` - Number of interned strings +- `opcache.interned_strings.usage_percentage` - Percentage of interned strings buffer used + +## Usage + +The instrumentation is automatically registered via composer. No additional configuration is required. + +You can also manually add OPcache metrics to the current active span: + +```php +use OpenTelemetry\Contrib\Instrumentation\PhpOpcache\PhpOpcacheInstrumentation; + +// Add OPcache metrics to the current active span +PhpOpcacheInstrumentation::addOpcacheMetricsToRootSpan(); +``` + +## Configuration + +The extension can be disabled via [runtime configuration](https://opentelemetry.io/docs/instrumentation/php/sdk/#configuration): + +```shell +OTEL_PHP_DISABLED_INSTRUMENTATIONS=opcache +``` + +## Requirements + +- PHP 8.0 or higher +- OPcache extension +- OpenTelemetry extension diff --git a/src/Instrumentation/PhpOpcache/_register.php b/src/Instrumentation/PhpOpcache/_register.php new file mode 100644 index 000000000..e84550f6d --- /dev/null +++ b/src/Instrumentation/PhpOpcache/_register.php @@ -0,0 +1,18 @@ + + + + + + + src + + + + + + + + + + + + + tests/Unit + + + tests/Integration + + + + diff --git a/src/Instrumentation/PhpOpcache/psalm.xml.dist b/src/Instrumentation/PhpOpcache/psalm.xml.dist new file mode 100644 index 000000000..155711712 --- /dev/null +++ b/src/Instrumentation/PhpOpcache/psalm.xml.dist @@ -0,0 +1,15 @@ + + + + + + + + + + diff --git a/src/Instrumentation/PhpOpcache/src/PhpOpcacheInstrumentation.php b/src/Instrumentation/PhpOpcache/src/PhpOpcacheInstrumentation.php new file mode 100644 index 000000000..9e823b23e --- /dev/null +++ b/src/Instrumentation/PhpOpcache/src/PhpOpcacheInstrumentation.php @@ -0,0 +1,143 @@ +getContext()->isValid()) { + return; // No active span, nothing to do + } + + self::captureOpcacheMetrics($span); + } + + private static function captureOpcacheMetrics(SpanInterface $span): void + { + if (!function_exists('opcache_get_status')) { + $span->setAttribute('opcache.enabled', false); + + return; + } + + $status = @opcache_get_status(false); + if (!$status) { + $span->setAttribute('opcache.available', false); + + return; + } + + $span->setAttribute('opcache.enabled', true); + $span->setAttribute('opcache.available', true); + + // Memory metrics + if (isset($status['memory_usage'])) { + $memory = $status['memory_usage']; + self::addMemoryAttributes($span, $memory); + } + + // Statistics metrics + if (isset($status['opcache_statistics'])) { + $stats = $status['opcache_statistics']; + self::addStatisticsAttributes($span, $stats); + } + + // Interned strings metrics + if (isset($status['interned_strings_usage'])) { + $interned = $status['interned_strings_usage']; + self::addInternedStringsAttributes($span, $interned); + } + } + + private static function addMemoryAttributes(SpanInterface $span, array $memory): void + { + $span->setAttribute('opcache.memory.used_bytes', $memory['used_memory'] ?? 0); + $span->setAttribute('opcache.memory.free_bytes', $memory['free_memory'] ?? 0); + $span->setAttribute('opcache.memory.wasted_bytes', $memory['wasted_memory'] ?? 0); + + $used = $memory['used_memory'] ?? 0; + $free = $memory['free_memory'] ?? 0; + $wasted = $memory['wasted_memory'] ?? 0; + $total = (int) $used + (int) $free + (int) $wasted; + + if ($total > 0) { + $span->setAttribute('opcache.memory.used_percentage', round(($used / $total) * 100, 2)); + $span->setAttribute('opcache.memory.wasted_percentage', round(($wasted / $total) * 100, 2)); + } + } + + private static function addStatisticsAttributes(SpanInterface $span, array $stats): void + { + $span->setAttribute('opcache.scripts.cached', $stats['num_cached_scripts'] ?? 0); + $span->setAttribute('opcache.hits.total', $stats['hits'] ?? 0); + $span->setAttribute('opcache.misses.total', $stats['misses'] ?? 0); + + $hits = (int) ($stats['hits'] ?? 0); + $misses = (int) ($stats['misses'] ?? 0); + $total = $hits + $misses; + + if ($total > 0) { + $span->setAttribute('opcache.hit_rate.percentage', round(($hits / $total) * 100, 2)); + } + + // Restart metrics + $span->setAttribute('opcache.restarts.oom', $stats['oom_restarts'] ?? 0); + $span->setAttribute('opcache.restarts.hash', $stats['hash_restarts'] ?? 0); + $span->setAttribute('opcache.restarts.manual', $stats['manual_restarts'] ?? 0); + + // Cache key metrics + $span->setAttribute('opcache.keys.cached', $stats['num_cached_keys'] ?? 0); + $span->setAttribute('opcache.keys.max_cached', $stats['max_cached_keys'] ?? 0); + } + + private static function addInternedStringsAttributes(SpanInterface $span, array $interned): void + { + $span->setAttribute('opcache.interned_strings.buffer_size', $interned['buffer_size'] ?? 0); + $span->setAttribute('opcache.interned_strings.used_memory', $interned['used_memory'] ?? 0); + $span->setAttribute('opcache.interned_strings.free_memory', $interned['free_memory'] ?? 0); + $span->setAttribute('opcache.interned_strings.strings_count', $interned['number_of_strings'] ?? 0); + + $used = $interned['used_memory'] ?? 0; + $size = $interned['buffer_size'] ?? 0; + + if ($size > 0) { + $span->setAttribute('opcache.interned_strings.usage_percentage', round(($used / $size) * 100, 2)); + } + } +} diff --git a/src/Instrumentation/PhpOpcache/tests/Integration/PhpOpcacheInstrumentationTest.php b/src/Instrumentation/PhpOpcache/tests/Integration/PhpOpcacheInstrumentationTest.php new file mode 100644 index 000000000..f7b69acd6 --- /dev/null +++ b/src/Instrumentation/PhpOpcache/tests/Integration/PhpOpcacheInstrumentationTest.php @@ -0,0 +1,122 @@ +storage = new ArrayObject(); + $this->tracerProvider = new TracerProvider( + new SimpleSpanProcessor( + new InMemoryExporter($this->storage) + ) + ); + + $this->scope = Configurator::create() + ->withTracerProvider($this->tracerProvider) + ->activate(); + } + + public function tearDown(): void + { + $this->scope->detach(); + } + + public function test_add_opcache_metrics_to_root_span(): void + { + + // Create a root span + $tracer = $this->tracerProvider->getTracer('test'); + $rootSpan = $tracer->spanBuilder('root_span') + ->setSpanKind(SpanKind::KIND_SERVER) + ->startSpan(); + + // Activate the root span + $scope = $rootSpan->activate(); + + try { + // Call the method to add opcache metrics to the root span + PhpOpcacheInstrumentation::addOpcacheMetricsToRootSpan(); + + // End the span + $rootSpan->end(); + + // Verify that the span has opcache attributes + $this->assertCount(1, $this->storage); + $span = $this->storage->offsetGet(0); + + // At minimum, it should have the opcache.enabled attribute + $attributes = $span->getAttributes(); + $this->assertTrue($attributes->has('opcache.enabled')); + + // If OPcache is enabled and available, check for more attributes + if (function_exists('opcache_get_status') && @opcache_get_status(false)) { + $this->assertTrue($attributes->has('opcache.available')); + $this->assertTrue($attributes->has('opcache.memory.used_bytes')); + $this->assertTrue($attributes->has('opcache.memory.free_bytes')); + $this->assertTrue($attributes->has('opcache.memory.wasted_bytes')); + $this->assertTrue($attributes->has('opcache.scripts.cached')); + $this->assertTrue($attributes->has('opcache.hits.total')); + $this->assertTrue($attributes->has('opcache.misses.total')); + } + } finally { + $scope->detach(); + } + } + + public function test_capture_opcache_metrics(): void + { + + // Create a span + $tracer = $this->tracerProvider->getTracer('test'); + $span = $tracer->spanBuilder('test_span')->startSpan(); + + // Call the captureOpcacheMetrics method using reflection + $reflectionClass = new ReflectionClass(PhpOpcacheInstrumentation::class); + $method = $reflectionClass->getMethod('captureOpcacheMetrics'); + //$method->setAccessible(true); + $method->invoke(null, $span); + + // End the span + $span->end(); + + // Verify that the span has opcache attributes + $this->assertCount(1, $this->storage); + $storedSpan = $this->storage->offsetGet(0); + $attributes = $storedSpan->getAttributes(); + + // At minimum, it should have the opcache.enabled attribute + $this->assertTrue($attributes->has('opcache.enabled')); + + // If OPcache is enabled and available, check for more attributes + if (function_exists('opcache_get_status') && @opcache_get_status(false)) { + $this->assertTrue($attributes->has('opcache.available')); + $this->assertTrue($attributes->has('opcache.memory.used_bytes')); + $this->assertTrue($attributes->has('opcache.memory.free_bytes')); + $this->assertTrue($attributes->has('opcache.memory.wasted_bytes')); + $this->assertTrue($attributes->has('opcache.scripts.cached')); + $this->assertTrue($attributes->has('opcache.hits.total')); + $this->assertTrue($attributes->has('opcache.misses.total')); + } + } + +} diff --git a/src/Instrumentation/PhpOpcache/tests/Unit/.gitkeep b/src/Instrumentation/PhpOpcache/tests/Unit/.gitkeep new file mode 100644 index 000000000..e69de29bb From 8bb376b1e5dfc6395b47d0a66c020558172c86fe Mon Sep 17 00:00:00 2001 From: HeenaBansal20 Date: Tue, 16 Sep 2025 11:54:14 -0700 Subject: [PATCH 4/4] Renamed files --- .github/workflows/php.yml | 4 ++-- .gitsplit.yml | 2 +- composer.json | 8 ++++---- .../{PhpOpcache => Opcache}/.gitattributes | 0 .../{PhpOpcache => Opcache}/.gitignore | 0 .../{PhpOpcache => Opcache}/.php-cs-fixer.php | 0 .../{PhpOpcache => Opcache}/README.md | 6 +++--- .../{PhpOpcache => Opcache}/_register.php | 6 +++--- .../{PhpOpcache => Opcache}/composer.json | 6 +++--- .../{PhpOpcache => Opcache}/phpstan.neon.dist | 0 .../{PhpOpcache => Opcache}/phpunit.xml.dist | 0 .../{PhpOpcache => Opcache}/psalm.xml.dist | 0 .../src/OpcacheInstrumentation.php} | 10 +++------- .../Integration/OpcacheInstrumentationTest.php} | 10 +++++----- .../{PhpOpcache => Opcache}/tests/Unit/.gitkeep | 0 .../PhpOpcache/.composer.json.swp | Bin 12288 -> 0 bytes 16 files changed, 24 insertions(+), 28 deletions(-) rename src/Instrumentation/{PhpOpcache => Opcache}/.gitattributes (100%) rename src/Instrumentation/{PhpOpcache => Opcache}/.gitignore (100%) rename src/Instrumentation/{PhpOpcache => Opcache}/.php-cs-fixer.php (100%) rename src/Instrumentation/{PhpOpcache => Opcache}/README.md (95%) rename src/Instrumentation/{PhpOpcache => Opcache}/_register.php (55%) rename src/Instrumentation/{PhpOpcache => Opcache}/composer.json (83%) rename src/Instrumentation/{PhpOpcache => Opcache}/phpstan.neon.dist (100%) rename src/Instrumentation/{PhpOpcache => Opcache}/phpunit.xml.dist (100%) rename src/Instrumentation/{PhpOpcache => Opcache}/psalm.xml.dist (100%) rename src/Instrumentation/{PhpOpcache/src/PhpOpcacheInstrumentation.php => Opcache/src/OpcacheInstrumentation.php} (93%) rename src/Instrumentation/{PhpOpcache/tests/Integration/PhpOpcacheInstrumentationTest.php => Opcache/tests/Integration/OpcacheInstrumentationTest.php} (92%) rename src/Instrumentation/{PhpOpcache => Opcache}/tests/Unit/.gitkeep (100%) delete mode 100644 src/Instrumentation/PhpOpcache/.composer.json.swp diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 961ef0249..9d786a7dd 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -41,7 +41,7 @@ jobs: 'Instrumentation/MySqli', 'Instrumentation/OpenAIPHP', 'Instrumentation/PDO', - 'Instrumentation/PhpOpcache', + 'Instrumentation/Opcache', 'Instrumentation/PostgreSql', # Sort PSRs numerically. 'Instrumentation/Psr3', @@ -85,7 +85,7 @@ jobs: php-version: 8.1 - project: 'Instrumentation/PostgreSql' php-version: 8.1 - - project: 'Instrumentation/PhpOpcache' + - project: 'Instrumentation/Opcache' - project: 'Instrumentation/Session' php-version: 8.1 steps: diff --git a/.gitsplit.yml b/.gitsplit.yml index 756a3b62c..3ae31fc2d 100644 --- a/.gitsplit.yml +++ b/.gitsplit.yml @@ -44,7 +44,7 @@ splits: target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-openai.git" - prefix: "src/Instrumentation/PDO" target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-pdo.git" - - prefix: "src/Instrumentation/PhpOpcache" + - prefix: "src/Instrumentation/opcache" target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-opcache.git" - prefix: "src/Instrumentation/PostgreSql" target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-auto-postgresql.git" diff --git a/composer.json b/composer.json index 999922ce9..c2d4bedb1 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ "OpenTelemetry\\Contrib\\Instrumentation\\MySqli\\": "src/Instrumentation/MySqli/src", "OpenTelemetry\\Contrib\\Instrumentation\\OpenAIPHP\\": "src/Instrumentation/OpenAIPHP/src", "OpenTelemetry\\Contrib\\Instrumentation\\PDO\\": "src/Instrumentation/PDO/src", - "OpenTelemetry\\Contrib\\Instrumentation\\PhpOpcache\\": "src/Instrumentation/PhpOpcache/src", + "OpenTelemetry\\Contrib\\Instrumentation\\Opcache\\": "src/Instrumentation/Opcache/src", "OpenTelemetry\\Contrib\\Instrumentation\\Psr3\\": "src/Instrumentation/Psr3/src", "OpenTelemetry\\Contrib\\Instrumentation\\Psr6\\": "src/Instrumentation/Psr6/src", "OpenTelemetry\\Contrib\\Instrumentation\\Psr14\\": "src/Instrumentation/Psr14/src", @@ -76,7 +76,7 @@ "src/Instrumentation/MySqli/_register.php", "src/Instrumentation/OpenAIPHP/_register.php", "src/Instrumentation/PDO/_register.php", - "src/Instrumentation/phpOpcache/_register.php", + "src/Instrumentation/Opcache/_register.php", "src/Instrumentation/Psr3/_register.php", "src/Instrumentation/Psr6/_register.php", "src/Instrumentation/Psr14/_register.php", @@ -115,7 +115,7 @@ "OpenTelemetry\\Tests\\Instrumentation\\MySqli\\": "src/Instrumentation/MySqli/tests", "OpenTelemetry\\Contrib\\Instrumentation\\OpenAIPHP\\Tests\\": "src/Instrumentation/OpenAIPHP/tests", "OpenTelemetry\\Tests\\Instrumentation\\PDO\\": "src/Instrumentation/PDO/tests", - "OpenTelemetry\\Tests\\Instrumentation\\PhpOpcache\\": "src/Instrumentation/PhpOpcache/tests", + "OpenTelemetry\\Tests\\Instrumentation\\Opcache\\": "src/Instrumentation/Opcache/tests", "OpenTelemetry\\Tests\\Instrumentation\\Psr6\\": "src/Instrumentation/Psr6/tests", "OpenTelemetry\\Instrumentation\\Psr14\\Tests\\": "src/Instrumentation/Psr14/tests", "OpenTelemetry\\Tests\\Instrumentation\\Psr16\\": "src/Instrumentation/Psr16/tests", @@ -155,7 +155,7 @@ "open-telemetry/opentelemetry-auto-mysqli": "self.version", "open-telemetry/opentelemetry-auto-openai-php": "self.version", "open-telemetry/opentelemetry-auto-pdo": "self.version", - "open-telemetry/opentelemetry-auto-phpOpcache": "self.version", + "open-telemetry/opentelemetry-auto-opcache": "self.version", "open-telemetry/opentelemetry-auto-psr3": "self.version", "open-telemetry/opentelemetry-auto-psr6": "self.version", "open-telemetry/opentelemetry-auto-psr14": "self.version", diff --git a/src/Instrumentation/PhpOpcache/.gitattributes b/src/Instrumentation/Opcache/.gitattributes similarity index 100% rename from src/Instrumentation/PhpOpcache/.gitattributes rename to src/Instrumentation/Opcache/.gitattributes diff --git a/src/Instrumentation/PhpOpcache/.gitignore b/src/Instrumentation/Opcache/.gitignore similarity index 100% rename from src/Instrumentation/PhpOpcache/.gitignore rename to src/Instrumentation/Opcache/.gitignore diff --git a/src/Instrumentation/PhpOpcache/.php-cs-fixer.php b/src/Instrumentation/Opcache/.php-cs-fixer.php similarity index 100% rename from src/Instrumentation/PhpOpcache/.php-cs-fixer.php rename to src/Instrumentation/Opcache/.php-cs-fixer.php diff --git a/src/Instrumentation/PhpOpcache/README.md b/src/Instrumentation/Opcache/README.md similarity index 95% rename from src/Instrumentation/PhpOpcache/README.md rename to src/Instrumentation/Opcache/README.md index 2453ededd..e4f5faec0 100644 --- a/src/Instrumentation/PhpOpcache/README.md +++ b/src/Instrumentation/Opcache/README.md @@ -1,6 +1,6 @@ [![Releases](https://img.shields.io/badge/releases-purple)](https://github.com/opentelemetry-php/contrib-auto-opcache/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/PhpOpcache) +[![Source](https://img.shields.io/badge/source-contrib-green)](https://github.com/open-telemetry/opentelemetry-php-contrib/tree/main/src/Instrumentation/opcache) [![Mirror](https://img.shields.io/badge/mirror-opentelemetry--php--contrib-blue)](https://github.com/opentelemetry-php/contrib-auto-opcache) [![Latest Version](http://poser.pugx.org/open-telemetry/opentelemetry-auto-opcache/v/unstable)](https://packagist.org/packages/open-telemetry/opentelemetry-auto-opcache/) [![Stable](http://poser.pugx.org/open-telemetry/opentelemetry-auto-opcache/v/stable)](https://packagist.org/packages/open-telemetry/opentelemetry-auto-opcache/) @@ -57,10 +57,10 @@ The instrumentation is automatically registered via composer. No additional conf You can also manually add OPcache metrics to the current active span: ```php -use OpenTelemetry\Contrib\Instrumentation\PhpOpcache\PhpOpcacheInstrumentation; +use OpenTelemetry\Contrib\Instrumentation\opcache\opcacheInstrumentation; // Add OPcache metrics to the current active span -PhpOpcacheInstrumentation::addOpcacheMetricsToRootSpan(); +opcacheInstrumentation::addOpcacheMetricsToRootSpan(); ``` ## Configuration diff --git a/src/Instrumentation/PhpOpcache/_register.php b/src/Instrumentation/Opcache/_register.php similarity index 55% rename from src/Instrumentation/PhpOpcache/_register.php rename to src/Instrumentation/Opcache/_register.php index e84550f6d..3475ed5c5 100644 --- a/src/Instrumentation/PhpOpcache/_register.php +++ b/src/Instrumentation/Opcache/_register.php @@ -2,10 +2,10 @@ declare(strict_types=1); -use OpenTelemetry\Contrib\Instrumentation\PhpOpcache\PhpOpcacheInstrumentation; +use OpenTelemetry\Contrib\Instrumentation\Opcache\OpcacheInstrumentation; use OpenTelemetry\SDK\Sdk; -if (class_exists(Sdk::class) && Sdk::isInstrumentationDisabled(PhpOpcacheInstrumentation::NAME) === true) { +if (class_exists(Sdk::class) && Sdk::isInstrumentationDisabled(OpcacheInstrumentation::NAME) === true) { return; } @@ -15,4 +15,4 @@ return; } -PhpOpcacheInstrumentation::register(); +OpcacheInstrumentation::register(); diff --git a/src/Instrumentation/PhpOpcache/composer.json b/src/Instrumentation/Opcache/composer.json similarity index 83% rename from src/Instrumentation/PhpOpcache/composer.json rename to src/Instrumentation/Opcache/composer.json index ebba1d251..e99207818 100644 --- a/src/Instrumentation/PhpOpcache/composer.json +++ b/src/Instrumentation/Opcache/composer.json @@ -1,5 +1,5 @@ { - "name": "open-telemetry/opentelemetry-auto-phpopcache", + "name": "open-telemetry/opentelemetry-auto-opcache", "description": "OpenTelemetry auto-instrumentation for PHP opcache", "keywords": ["opentelemetry", "otel", "open-telemetry", "tracing", "opcache", "instrumentation"], "type": "library", @@ -29,7 +29,7 @@ }, "autoload": { "psr-4": { - "OpenTelemetry\\Contrib\\Instrumentation\\PhpOpcache\\": "src/" + "OpenTelemetry\\Contrib\\Instrumentation\\Opcache\\": "src/" }, "files": [ "_register.php" @@ -37,7 +37,7 @@ }, "autoload-dev": { "psr-4": { - "OpenTelemetry\\Tests\\Instrumentation\\PhpOpcache\\": "tests/" + "OpenTelemetry\\Tests\\Instrumentation\\Opcache\\": "tests/" } } } diff --git a/src/Instrumentation/PhpOpcache/phpstan.neon.dist b/src/Instrumentation/Opcache/phpstan.neon.dist similarity index 100% rename from src/Instrumentation/PhpOpcache/phpstan.neon.dist rename to src/Instrumentation/Opcache/phpstan.neon.dist diff --git a/src/Instrumentation/PhpOpcache/phpunit.xml.dist b/src/Instrumentation/Opcache/phpunit.xml.dist similarity index 100% rename from src/Instrumentation/PhpOpcache/phpunit.xml.dist rename to src/Instrumentation/Opcache/phpunit.xml.dist diff --git a/src/Instrumentation/PhpOpcache/psalm.xml.dist b/src/Instrumentation/Opcache/psalm.xml.dist similarity index 100% rename from src/Instrumentation/PhpOpcache/psalm.xml.dist rename to src/Instrumentation/Opcache/psalm.xml.dist diff --git a/src/Instrumentation/PhpOpcache/src/PhpOpcacheInstrumentation.php b/src/Instrumentation/Opcache/src/OpcacheInstrumentation.php similarity index 93% rename from src/Instrumentation/PhpOpcache/src/PhpOpcacheInstrumentation.php rename to src/Instrumentation/Opcache/src/OpcacheInstrumentation.php index 9e823b23e..25a1e48d2 100644 --- a/src/Instrumentation/PhpOpcache/src/PhpOpcacheInstrumentation.php +++ b/src/Instrumentation/Opcache/src/OpcacheInstrumentation.php @@ -2,17 +2,13 @@ declare(strict_types=1); -namespace OpenTelemetry\Contrib\Instrumentation\PhpOpcache; +namespace OpenTelemetry\Contrib\Instrumentation\opcache; -use OpenTelemetry\API\Instrumentation\CachedInstrumentation; use OpenTelemetry\API\Trace\Span; use OpenTelemetry\API\Trace\SpanInterface; -use OpenTelemetry\API\Trace\SpanKind; use OpenTelemetry\Context\Context; -use function OpenTelemetry\Instrumentation\hook; -use OpenTelemetry\SemConv\Version; -class PhpOpcacheInstrumentation +class OpcacheInstrumentation { public const NAME = 'opcache'; public const VERSION = '1.0.0'; @@ -32,7 +28,7 @@ public static function register(): void // Register our own shutdown function to capture OPcache metrics // register_shutdown_function([self::class, 'captureMetricsOnShutdown']); - register_shutdown_function('OpenTelemetry\Contrib\Instrumentation\PhpOpcache\PhpOpcacheInstrumentation::addOpcacheMetricsToRootSpan'); + register_shutdown_function('OpenTelemetry\Contrib\Instrumentation\opcache\opcacheInstrumentation::addOpcacheMetricsToRootSpan'); self::$registered = true; } diff --git a/src/Instrumentation/PhpOpcache/tests/Integration/PhpOpcacheInstrumentationTest.php b/src/Instrumentation/Opcache/tests/Integration/OpcacheInstrumentationTest.php similarity index 92% rename from src/Instrumentation/PhpOpcache/tests/Integration/PhpOpcacheInstrumentationTest.php rename to src/Instrumentation/Opcache/tests/Integration/OpcacheInstrumentationTest.php index f7b69acd6..d12bc41c5 100644 --- a/src/Instrumentation/PhpOpcache/tests/Integration/PhpOpcacheInstrumentationTest.php +++ b/src/Instrumentation/Opcache/tests/Integration/OpcacheInstrumentationTest.php @@ -2,21 +2,21 @@ declare(strict_types=1); -namespace OpenTelemetry\Tests\Instrumentation\PhpOpcache\tests\Integration; +namespace OpenTelemetry\Tests\Instrumentation\opcache\tests\Integration; use ArrayObject; use OpenTelemetry\API\Instrumentation\Configurator; use OpenTelemetry\API\Trace\Span; use OpenTelemetry\API\Trace\SpanKind; use OpenTelemetry\Context\ScopeInterface; -use OpenTelemetry\Contrib\Instrumentation\PhpOpcache\PhpOpcacheInstrumentation; +use OpenTelemetry\Contrib\Instrumentation\opcache\OpcacheInstrumentation; use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter; use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; use OpenTelemetry\SDK\Trace\TracerProvider; use PHPUnit\Framework\TestCase; use ReflectionClass; -class PhpOpcacheInstrumentationTest extends TestCase +class OpcacheInstrumentationTest extends TestCase { private ScopeInterface $scope; private ArrayObject $storage; @@ -55,7 +55,7 @@ public function test_add_opcache_metrics_to_root_span(): void try { // Call the method to add opcache metrics to the root span - PhpOpcacheInstrumentation::addOpcacheMetricsToRootSpan(); + opcacheInstrumentation::addOpcacheMetricsToRootSpan(); // End the span $rootSpan->end(); @@ -91,7 +91,7 @@ public function test_capture_opcache_metrics(): void $span = $tracer->spanBuilder('test_span')->startSpan(); // Call the captureOpcacheMetrics method using reflection - $reflectionClass = new ReflectionClass(PhpOpcacheInstrumentation::class); + $reflectionClass = new ReflectionClass(opcacheInstrumentation::class); $method = $reflectionClass->getMethod('captureOpcacheMetrics'); //$method->setAccessible(true); $method->invoke(null, $span); diff --git a/src/Instrumentation/PhpOpcache/tests/Unit/.gitkeep b/src/Instrumentation/Opcache/tests/Unit/.gitkeep similarity index 100% rename from src/Instrumentation/PhpOpcache/tests/Unit/.gitkeep rename to src/Instrumentation/Opcache/tests/Unit/.gitkeep diff --git a/src/Instrumentation/PhpOpcache/.composer.json.swp b/src/Instrumentation/PhpOpcache/.composer.json.swp deleted file mode 100644 index d3aa2f917ab30fbc9c994b8011e8356749ff5075..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2!HX0}6vj&l0Zoje&Bf8s(_OPu+3v2gsz!HM z$3RZG_9Q526S;CFEZSc=05Xuc~`zw`XDXs=R@3YNqPF*ROunJ+SOI zSor$V<>+*>$nZME*q{GA936Z5`JsKkyvvMI&QHJUrJN{ZJ<_pQ*UC6yddYBNL?Q=O zlB;T#e$N+7FA7m-snViGny7>`laQRN+8H@Wt;y3%wRL7(AtZBB)#>s`U(tD%k3@=s zT3KO|Z>_3lrRf6^AOgD)7`tJ7`oPN4VsF8Reaw$WM-H#-mO*PofCvx)B0vO)01+Sp zM1TmqWdxkw!@k3w_O|=_tR45vjqO(&hyW2F0z`la5CI}U1c(3;AOb{y2oQlckU&;2 z_Fz9_8}Ffc{QrOR@Bfzv8T$j8LLWizKu^JCFXbKh33iJhZ4B89*eSopQpeNAp z(9h6a=sI)``Vji>ea0R^_o1JlAE7B!L!UrLp~KJ%^zQ?>wGWSx9K*H^L4JVRIw3ce(pr tZvHle=PU9LZ#*@1?i@P@7Kd`!@>b>s`yVr|wXOgF