Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f519b2c
feature: add predis to dev deps
mtk3d Apr 29, 2025
6e34684
feature: implement redis client interface
mtk3d Apr 29, 2025
9e57f04
feature: implement predis and phpredis clients
mtk3d Apr 29, 2025
1c1f8b1
feature: add predis test
mtk3d Apr 29, 2025
23b938e
refactor: yagni for interface
mtk3d Apr 30, 2025
5e2e4d5
refactor: use abstract redis and keep full backward compatibility
mtk3d Apr 30, 2025
8bed4a4
fix: add phpdocs
mtk3d Apr 30, 2025
35af9bd
fix: bring _prefix() back
mtk3d Apr 30, 2025
07ca5fc
fix: fix prefix for PHPRedis
mtk3d Apr 30, 2025
b8c7f1a
fix: run phpcs
mtk3d Apr 30, 2025
7ddb21b
tests: add config for blackbox test
mtk3d Apr 30, 2025
4562801
fix: remove all redis notes from predis
mtk3d May 1, 2025
c9f0a97
fix: estabish connection after first call in predis
mtk3d May 1, 2025
c3080db
fix: add exception handling for predis connection
mtk3d May 1, 2025
880e7a2
chore: remove unecessary comment
mtk3d Mar 19, 2026
8dd074a
fix: remove redundant replicate commands
mtk3d Mar 19, 2026
a019eb2
fix: remove unnecessary comparison
mtk3d Mar 19, 2026
ddb503b
fix: remove dead code
mtk3d Mar 19, 2026
ef9c7db
rafactor: remove OPT_READ_TIMEOUT as it's phpredis internals
mtk3d Mar 19, 2026
6b9c8d6
refactor: make opt truely abstract
mtk3d Mar 19, 2026
55de57d
fix: apply stripPrefix only to phpredis implementation
mtk3d Mar 19, 2026
be47315
fix: normalise the return type of get for predis and phpredis
mtk3d Mar 19, 2026
87a5f91
fix: always check if connection exists
mtk3d Mar 19, 2026
7c0ef78
fix: fix mixed type of getOption
mtk3d Mar 19, 2026
8f3f2a8
cicd: add predis to blackbox tests
mtk3d Mar 19, 2026
5686202
fix: fix test name case
mtk3d Mar 19, 2026
72af24e
fix: fix failing tests
mtk3d Mar 19, 2026
c6692f4
fix: add return type of keys
mtk3d Mar 22, 2026
d2eda2c
style: add comment to document intentionally sharing prefix
mtk3d Mar 22, 2026
8a4e308
fix: bring back replicate_commands() to keep support for older redis …
mtk3d Mar 22, 2026
e0afae3
fix: pass previous exception
mtk3d Mar 22, 2026
7d0d27e
fix: connect predis earlier to fail fast same as phpredis
mtk3d Mar 22, 2026
bd428ff
refactor: create Predis\Client eagerly in constructor
mtk3d Mar 22, 2026
13068a3
refactor: remove no-op override
mtk3d Mar 22, 2026
19fb93f
refactor: replase universal getOption() to simple getPrefix()
mtk3d Mar 22, 2026
f101dd6
fix: add type to inform phpstan
mtk3d Mar 22, 2026
a603086
readme: add information about new predis adapter
mtk3d Mar 22, 2026
339f666
chore: fix regression in passing previous exception if thrown
mtk3d Mar 24, 2026
75b5017
docs: capital letter typo
mtk3d Mar 24, 2026
ae19f84
docs: update shared prefix note
mtk3d Mar 24, 2026
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
4 changes: 3 additions & 1 deletion .github/workflows/blackbox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ jobs:
run: docker compose run phpunit env ADAPTER=apc vendor/bin/phpunit tests/Test/
- name: Run Blackbox with APCng
run: docker compose run phpunit env ADAPTER=apcng vendor/bin/phpunit tests/Test/
- name: Run Blackbox with Redis
- name: Run Blackbox with PHPRedis
run: docker compose run phpunit env ADAPTER=redis vendor/bin/phpunit tests/Test/
- name: Run Blackbox with Predis
run: docker compose run phpunit env ADAPTER=predis vendor/bin/phpunit tests/Test/
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ If using Redis, we recommend running a local Redis instance next to your PHP wor
## How does it work?

Usually PHP worker processes don't share any state.
You can pick from four adapters.
Redis, APC, APCng, or an in-memory adapter.
You can pick from five adapters.
Redis, Predis, APC, APCng, or an in-memory adapter.
While the first needs a separate binary running, the second and third just need the [APC](https://pecl.php.net/package/APCU) extension to be installed. If you don't need persistent metrics between requests (e.g. a long running cron job or script) the in-memory adapter might be suitable to use.

## Installation
Expand All @@ -24,13 +24,15 @@ composer require promphp/prometheus_client_php
## Usage

A simple counter:

```php
\Prometheus\CollectorRegistry::getDefault()
->getOrRegisterCounter('', 'some_quick_counter', 'just a quick measurement')
->inc();
```

Write some enhanced metrics:

```php
$registry = \Prometheus\CollectorRegistry::getDefault();

Expand All @@ -48,6 +50,7 @@ $summary->observe(5, ['blue']);
```

Manually register and retrieve metrics (these steps are combined in the `getOrRegister...` methods):

```php
$registry = \Prometheus\CollectorRegistry::getDefault();

Expand All @@ -60,6 +63,7 @@ $counterB->incBy(2, ['red']);
```

Expose the metrics:

```php
$registry = \Prometheus\CollectorRegistry::getDefault();

Expand All @@ -71,6 +75,7 @@ echo $result;
```

Change the Redis options (the example shows the defaults):

```php
\Prometheus\Storage\Redis::setDefaultOptions(
[
Expand All @@ -84,7 +89,23 @@ Change the Redis options (the example shows the defaults):
);
```

Using the Predis storage (requires `predis/predis`):

```php
$registry = new CollectorRegistry(new \Prometheus\Storage\Predis());
```

Or with an existing connection:

```php
$client = new \Predis\Client(['host' => '127.0.0.1']);
$registry = new CollectorRegistry(\Prometheus\Storage\Predis::fromExistingConnection($client));
```

> **Note:** Using `Redis::setPrefix()` and `Predis::setPrefix()` share the same prefix. Using both adapters with different prefixes in the same application is not supported.

Using the InMemory storage:

```php
$registry = new CollectorRegistry(new InMemory());

Expand All @@ -96,14 +117,17 @@ $result = $renderer->render($registry->getMetricFamilySamples());
```

Using the APC or APCng storage:

```php
$registry = new CollectorRegistry(new APCng());
or
$registry = new CollectorRegistry(new APC());
```

(see the `README.APCng.md` file for more details)

Using the PDO storage:

```php
$registry = new CollectorRegistry(new \PDO('mysql:host=localhost;dbname=prometheus', 'username', 'password'));
or
Expand All @@ -113,11 +137,13 @@ $registry = new CollectorRegistry(new \PDO('sqlite::memory:'));
### Advanced Usage

#### Advanced Histogram Usage

On passing an empty array for the bucket parameter on instantiation, a set of default buckets will be used instead.
Whilst this is a good base for a typical web application, there is named constructor to assist in the generation of
exponential / geometric buckets.

Eg:

```
Histogram::exponentialBuckets(0.05, 1.5, 10);
```
Expand All @@ -127,7 +153,9 @@ This will start your buckets with a value of 0.05, grow them by a factor of 1.5
Also look at the [examples](examples).

#### PushGateway Support
As of Version 2.0.0 this library doesn't support the Prometheus PushGateway anymore because we want to have this package as small als possible. If you need Prometheus PushGateway support, you could use the companion library: https://github.com/PromPHP/prometheus_push_gateway_php

As of Version 2.0.0 this library doesn't support the Prometheus PushGateway anymore because we want to have this package as small als possible. If you need Prometheus PushGateway support, you could use the companion library: <https://github.com/PromPHP/prometheus_push_gateway_php>

```
composer require promphp/prometheus_push_gateway_php
```
Expand All @@ -143,11 +171,13 @@ composer require promphp/prometheus_push_gateway_php
* Redis

Start a Redis instance:

```
docker-compose up redis
```

Run the tests:

```
composer install

Expand All @@ -159,9 +189,11 @@ composer install
## Black box testing

Just start the nginx, fpm & Redis setup with docker-compose:

```
docker-compose up
```

Pick the adapter you want to test.

```
Expand All @@ -173,11 +205,13 @@ docker-compose run phpunit env ADAPTER=redis vendor/bin/phpunit tests/Test/
## Performance testing

This currently tests the APC and APCng adapters head-to-head and reports if the APCng adapter is slower for any actions.

```
phpunit vendor/bin/phpunit tests/Test/ --group Performance
```

The test can also be run inside a container.

```
docker-compose up
docker-compose run phpunit vendor/bin/phpunit tests/Test/ --group Performance
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
"phpstan/phpstan-phpunit": "^1.1.0",
"phpstan/phpstan-strict-rules": "^1.1.0",
"phpunit/phpunit": "^9.4",
"predis/predis": "^2.3",
"squizlabs/php_codesniffer": "^3.6",
"symfony/polyfill-apcu": "^1.6"
},
"suggest": {
"ext-redis": "Required if using Redis.",
"predis/predis": "Required if using Predis.",
"ext-apc": "Required if using APCu.",
"ext-pdo": "Required if using PDO.",
"promphp/prometheus_push_gateway_php": "An easy client for using Prometheus PushGateway.",
Expand Down
2 changes: 2 additions & 0 deletions examples/flush_adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
define('REDIS_HOST', $_SERVER['REDIS_HOST'] ?? '127.0.0.1');

$adapter = new Prometheus\Storage\Redis(['host' => REDIS_HOST]);
} elseif ($adapterName === 'predis') {
$adapter = new Prometheus\Storage\Predis(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
} elseif ($adapterName === 'apc') {
$adapter = new Prometheus\Storage\APC();
} elseif ($adapterName === 'apcng') {
Expand Down
2 changes: 2 additions & 0 deletions examples/metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
if ($adapter === 'redis') {
Redis::setDefaultOptions(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
$adapter = new Prometheus\Storage\Redis();
} elseif ($adapter === 'predis') {
$adapter = new Prometheus\Storage\Predis(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
} elseif ($adapter === 'apc') {
$adapter = new Prometheus\Storage\APC();
} elseif ($adapter === 'apcng') {
Expand Down
2 changes: 2 additions & 0 deletions examples/some_counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
if ($adapter === 'redis') {
Redis::setDefaultOptions(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
$adapter = new Prometheus\Storage\Redis();
} elseif ($adapter === 'predis') {
$adapter = new Prometheus\Storage\Predis(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
} elseif ($adapter === 'apc') {
$adapter = new Prometheus\Storage\APC();
} elseif ($adapter === 'apcng') {
Expand Down
3 changes: 2 additions & 1 deletion examples/some_gauge.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
use Prometheus\CollectorRegistry;
use Prometheus\Storage\Redis;


error_log('c=' . $_GET['c']);

$adapter = $_GET['adapter'];

if ($adapter === 'redis') {
Redis::setDefaultOptions(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
$adapter = new Prometheus\Storage\Redis();
} elseif ($adapter === 'predis') {
$adapter = new Prometheus\Storage\Predis(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
} elseif ($adapter === 'apc') {
$adapter = new Prometheus\Storage\APC();
} elseif ($adapter === 'apcng') {
Expand Down
2 changes: 2 additions & 0 deletions examples/some_histogram.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
if ($adapter === 'redis') {
Redis::setDefaultOptions(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
$adapter = new Prometheus\Storage\Redis();
} elseif ($adapter === 'predis') {
$adapter = new Prometheus\Storage\Predis(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
} elseif ($adapter === 'apc') {
$adapter = new Prometheus\Storage\APC();
} elseif ($adapter === 'apcng') {
Expand Down
2 changes: 2 additions & 0 deletions examples/some_summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
if ($adapter === 'redis') {
Redis::setDefaultOptions(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
$adapter = new Prometheus\Storage\Redis();
} elseif ($adapter === 'predis') {
$adapter = new Prometheus\Storage\Predis(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
} elseif ($adapter === 'apc') {
$adapter = new Prometheus\Storage\APC();
} elseif ($adapter === 'apcng') {
Expand Down
Loading
Loading