Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
'Instrumentation/MySqli',
'Instrumentation/OpenAIPHP',
'Instrumentation/PDO',
'Instrumentation/Opcache',
'Instrumentation/PostgreSql',
# Sort PSRs numerically.
'Instrumentation/Psr3',
Expand Down Expand Up @@ -84,6 +85,7 @@ jobs:
php-version: 8.1
- project: 'Instrumentation/PostgreSql'
php-version: 8.1
- project: 'Instrumentation/Opcache'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line excludes all versions. Since you're not actually hooking methods, you can just remove this line since it should work against 8.1

Suggested change
- project: 'Instrumentation/Opcache'

- project: 'Instrumentation/Session'
php-version: 8.1
steps:
Expand Down
2 changes: 2 additions & 0 deletions .gitsplit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/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"
- prefix: "src/Instrumentation/Psr3"
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\\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",
Expand Down Expand Up @@ -75,6 +76,7 @@
"src/Instrumentation/MySqli/_register.php",
"src/Instrumentation/OpenAIPHP/_register.php",
"src/Instrumentation/PDO/_register.php",
"src/Instrumentation/Opcache/_register.php",
"src/Instrumentation/Psr3/_register.php",
"src/Instrumentation/Psr6/_register.php",
"src/Instrumentation/Psr14/_register.php",
Expand Down Expand Up @@ -113,6 +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\\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",
Expand Down Expand Up @@ -152,6 +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-opcache": "self.version",
"open-telemetry/opentelemetry-auto-psr3": "self.version",
"open-telemetry/opentelemetry-auto-psr6": "self.version",
"open-telemetry/opentelemetry-auto-psr14": "self.version",
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
11 changes: 11 additions & 0 deletions docker/opcache/opcache.ini
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions src/Instrumentation/Opcache/.gitattributes
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/Instrumentation/Opcache/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
43 changes: 43 additions & 0 deletions src/Instrumentation/Opcache/.php-cs-fixer.php
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,
'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);

78 changes: 78 additions & 0 deletions src/Instrumentation/Opcache/README.md
Original file line number Diff line number Diff line change
@@ -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/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/)

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\opcache\opcacheInstrumentation;

// Add OPcache metrics to the current active span
opcacheInstrumentation::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
18 changes: 18 additions & 0 deletions src/Instrumentation/Opcache/_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\Opcache\OpcacheInstrumentation;
use OpenTelemetry\SDK\Sdk;

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
trigger_error('The opentelemetry extension must be loaded in order to autoload the OpenTelemetry IO auto-instrumentation', E_USER_WARNING);
trigger_error('The opentelemetry extension must be loaded in order to autoload the OpenTelemetry opcache auto-instrumentation', E_USER_WARNING);


return;
}

OpcacheInstrumentation::register();
43 changes: 43 additions & 0 deletions src/Instrumentation/Opcache/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "open-telemetry/opentelemetry-auto-opcache",
"description": "OpenTelemetry auto-instrumentation for PHP opcache",
"keywords": ["opentelemetry", "otel", "open-telemetry", "tracing", "opcache", "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.2",
"ext-opentelemetry": "*",
"open-telemetry/api": "^1.0",
"open-telemetry/sem-conv": "^1.32",
"ext-zend-opcache": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3",
"phan/phan": "^5.0",
"php-http/mock-client": "*",
"phpstan/phpstan": "^1.1",
"phpstan/phpstan-phpunit": "^1.0",
"psalm/plugin-phpunit": "^0.19.2",
"open-telemetry/sdk": "^1.0",
"phpunit/phpunit": "^9.5",
"vimeo/psalm": "6.4.0",
"mockery/mockery": "^1.4"
},
"autoload": {
"psr-4": {
"OpenTelemetry\\Contrib\\Instrumentation\\Opcache\\": "src/"
},
"files": [
"_register.php"
]
},
"autoload-dev": {
"psr-4": {
"OpenTelemetry\\Tests\\Instrumentation\\Opcache\\": "tests/"
}
}
}
9 changes: 9 additions & 0 deletions src/Instrumentation/Opcache/phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon

parameters:
tmpDir: var/cache/phpstan
level: 5
paths:
- src
- tests
47 changes: 47 additions & 0 deletions src/Instrumentation/Opcache/phpunit.xml.dist
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>
15 changes: 15 additions & 0 deletions src/Instrumentation/Opcache/psalm.xml.dist
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>
Loading
Loading