Skip to content

Commit da86f15

Browse files
authored
Add Predis support (#186)
Signed-off-by: Mateusz Cholewka <mateusz@cholewka.com.pl>
1 parent d50d083 commit da86f15

28 files changed

Lines changed: 1360 additions & 710 deletions

.github/workflows/blackbox.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,7 @@ jobs:
3939
run: docker compose run phpunit env ADAPTER=apc vendor/bin/phpunit tests/Test/
4040
- name: Run Blackbox with APCng
4141
run: docker compose run phpunit env ADAPTER=apcng vendor/bin/phpunit tests/Test/
42-
- name: Run Blackbox with Redis
42+
- name: Run Blackbox with PHPRedis
4343
run: docker compose run phpunit env ADAPTER=redis vendor/bin/phpunit tests/Test/
44+
- name: Run Blackbox with Predis
45+
run: docker compose run phpunit env ADAPTER=predis vendor/bin/phpunit tests/Test/

README.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ If using Redis, we recommend running a local Redis instance next to your PHP wor
99
## How does it work?
1010

1111
Usually PHP worker processes don't share any state.
12-
You can pick from four adapters.
13-
Redis, APC, APCng, or an in-memory adapter.
12+
You can pick from five adapters.
13+
Redis, Predis, APC, APCng, or an in-memory adapter.
1414
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.
1515

1616
## Installation
@@ -24,13 +24,15 @@ composer require promphp/prometheus_client_php
2424
## Usage
2525

2626
A simple counter:
27+
2728
```php
2829
\Prometheus\CollectorRegistry::getDefault()
2930
->getOrRegisterCounter('', 'some_quick_counter', 'just a quick measurement')
3031
->inc();
3132
```
3233

3334
Write some enhanced metrics:
35+
3436
```php
3537
$registry = \Prometheus\CollectorRegistry::getDefault();
3638

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

5052
Manually register and retrieve metrics (these steps are combined in the `getOrRegister...` methods):
53+
5154
```php
5255
$registry = \Prometheus\CollectorRegistry::getDefault();
5356

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

6265
Expose the metrics:
66+
6367
```php
6468
$registry = \Prometheus\CollectorRegistry::getDefault();
6569

@@ -71,6 +75,7 @@ echo $result;
7175
```
7276

7377
Change the Redis options (the example shows the defaults):
78+
7479
```php
7580
\Prometheus\Storage\Redis::setDefaultOptions(
7681
[
@@ -84,7 +89,23 @@ Change the Redis options (the example shows the defaults):
8489
);
8590
```
8691

92+
Using the Predis storage (requires `predis/predis`):
93+
94+
```php
95+
$registry = new CollectorRegistry(new \Prometheus\Storage\Predis());
96+
```
97+
98+
Or with an existing connection:
99+
100+
```php
101+
$client = new \Predis\Client(['host' => '127.0.0.1']);
102+
$registry = new CollectorRegistry(\Prometheus\Storage\Predis::fromExistingConnection($client));
103+
```
104+
105+
> **Note:** Using `Redis::setPrefix()` and `Predis::setPrefix()` share the same prefix. Using both adapters with different prefixes in the same application is not supported.
106+
87107
Using the InMemory storage:
108+
88109
```php
89110
$registry = new CollectorRegistry(new InMemory());
90111

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

98119
Using the APC or APCng storage:
120+
99121
```php
100122
$registry = new CollectorRegistry(new APCng());
101123
or
102124
$registry = new CollectorRegistry(new APC());
103125
```
126+
104127
(see the `README.APCng.md` file for more details)
105128

106129
Using the PDO storage:
130+
107131
```php
108132
$registry = new CollectorRegistry(new \PDO('mysql:host=localhost;dbname=prometheus', 'username', 'password'));
109133
or
@@ -113,11 +137,13 @@ $registry = new CollectorRegistry(new \PDO('sqlite::memory:'));
113137
### Advanced Usage
114138

115139
#### Advanced Histogram Usage
140+
116141
On passing an empty array for the bucket parameter on instantiation, a set of default buckets will be used instead.
117142
Whilst this is a good base for a typical web application, there is named constructor to assist in the generation of
118143
exponential / geometric buckets.
119144

120145
Eg:
146+
121147
```
122148
Histogram::exponentialBuckets(0.05, 1.5, 10);
123149
```
@@ -127,7 +153,9 @@ This will start your buckets with a value of 0.05, grow them by a factor of 1.5
127153
Also look at the [examples](examples).
128154

129155
#### PushGateway Support
130-
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
156+
157+
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>
158+
131159
```
132160
composer require promphp/prometheus_push_gateway_php
133161
```
@@ -143,11 +171,13 @@ composer require promphp/prometheus_push_gateway_php
143171
* Redis
144172

145173
Start a Redis instance:
174+
146175
```
147176
docker-compose up redis
148177
```
149178

150179
Run the tests:
180+
151181
```
152182
composer install
153183
@@ -159,9 +189,11 @@ composer install
159189
## Black box testing
160190

161191
Just start the nginx, fpm & Redis setup with docker-compose:
192+
162193
```
163194
docker-compose up
164195
```
196+
165197
Pick the adapter you want to test.
166198

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

175207
This currently tests the APC and APCng adapters head-to-head and reports if the APCng adapter is slower for any actions.
208+
176209
```
177210
phpunit vendor/bin/phpunit tests/Test/ --group Performance
178211
```
179212

180213
The test can also be run inside a container.
214+
181215
```
182216
docker-compose up
183217
docker-compose run phpunit vendor/bin/phpunit tests/Test/ --group Performance

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
"phpstan/phpstan-phpunit": "^1.1.0",
2626
"phpstan/phpstan-strict-rules": "^1.1.0",
2727
"phpunit/phpunit": "^9.4",
28+
"predis/predis": "^2.3",
2829
"squizlabs/php_codesniffer": "^3.6",
2930
"symfony/polyfill-apcu": "^1.6"
3031
},
3132
"suggest": {
3233
"ext-redis": "Required if using Redis.",
34+
"predis/predis": "Required if using Predis.",
3335
"ext-apc": "Required if using APCu.",
3436
"ext-pdo": "Required if using PDO.",
3537
"promphp/prometheus_push_gateway_php": "An easy client for using Prometheus PushGateway.",

examples/flush_adapter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
define('REDIS_HOST', $_SERVER['REDIS_HOST'] ?? '127.0.0.1');
1111

1212
$adapter = new Prometheus\Storage\Redis(['host' => REDIS_HOST]);
13+
} elseif ($adapterName === 'predis') {
14+
$adapter = new Prometheus\Storage\Predis(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
1315
} elseif ($adapterName === 'apc') {
1416
$adapter = new Prometheus\Storage\APC();
1517
} elseif ($adapterName === 'apcng') {

examples/metrics.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
if ($adapter === 'redis') {
1212
Redis::setDefaultOptions(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
1313
$adapter = new Prometheus\Storage\Redis();
14+
} elseif ($adapter === 'predis') {
15+
$adapter = new Prometheus\Storage\Predis(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
1416
} elseif ($adapter === 'apc') {
1517
$adapter = new Prometheus\Storage\APC();
1618
} elseif ($adapter === 'apcng') {

examples/some_counter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
if ($adapter === 'redis') {
1111
Redis::setDefaultOptions(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
1212
$adapter = new Prometheus\Storage\Redis();
13+
} elseif ($adapter === 'predis') {
14+
$adapter = new Prometheus\Storage\Predis(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
1315
} elseif ($adapter === 'apc') {
1416
$adapter = new Prometheus\Storage\APC();
1517
} elseif ($adapter === 'apcng') {

examples/some_gauge.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
use Prometheus\CollectorRegistry;
66
use Prometheus\Storage\Redis;
77

8-
98
error_log('c=' . $_GET['c']);
109

1110
$adapter = $_GET['adapter'];
1211

1312
if ($adapter === 'redis') {
1413
Redis::setDefaultOptions(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
1514
$adapter = new Prometheus\Storage\Redis();
15+
} elseif ($adapter === 'predis') {
16+
$adapter = new Prometheus\Storage\Predis(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
1617
} elseif ($adapter === 'apc') {
1718
$adapter = new Prometheus\Storage\APC();
1819
} elseif ($adapter === 'apcng') {

examples/some_histogram.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
if ($adapter === 'redis') {
1313
Redis::setDefaultOptions(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
1414
$adapter = new Prometheus\Storage\Redis();
15+
} elseif ($adapter === 'predis') {
16+
$adapter = new Prometheus\Storage\Predis(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
1517
} elseif ($adapter === 'apc') {
1618
$adapter = new Prometheus\Storage\APC();
1719
} elseif ($adapter === 'apcng') {

examples/some_summary.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
if ($adapter === 'redis') {
1313
Redis::setDefaultOptions(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
1414
$adapter = new Prometheus\Storage\Redis();
15+
} elseif ($adapter === 'predis') {
16+
$adapter = new Prometheus\Storage\Predis(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
1517
} elseif ($adapter === 'apc') {
1618
$adapter = new Prometheus\Storage\APC();
1719
} elseif ($adapter === 'apcng') {

0 commit comments

Comments
 (0)