Skip to content
This repository was archived by the owner on Oct 22, 2019. It is now read-only.

Commit 43fc3bd

Browse files
authored
Merge pull request #25 from Jimdo/apc_storage
Use APC as a storage adapter
2 parents 2359025 + c15f673 commit 43fc3bd

25 files changed

Lines changed: 936 additions & 298 deletions

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
language: php
22
php:
33
- 5.6
4-
- 5.3
54

65
services:
76
- redis-server
87

98
before_script:
9+
- echo yes | pecl install apcu-4.0.11
10+
- echo "apc.enabled = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
11+
- echo "apc.enable_cli = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
1012
- echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
1113
- composer self-update
1214
- composer install --no-interaction --prefer-source --dev
1315
- phpenv rehash
16+
- php -i
1417

1518
script:
1619
- vendor/bin/phpunit --verbose --colors

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,15 @@
44
[![Code Climate](https://codeclimate.com/github/Jimdo/prometheus_client_php.png)](https://codeclimate.com/github/Jimdo/prometheus_client_php)
55

66

7-
This library uses Redis to do the client side aggregation.
8-
We recommend to run a local Redis instance next to your PHP workers.
7+
This library uses Redis or APCu to do the client side aggregation.
8+
If using Redis, we recommend to run a local Redis instance next to your PHP workers.
99

10-
## Why Redis?
10+
## How does it work?
1111

1212
Usually PHP worker processes don't share any state.
13-
14-
We decided to use Redis because:
15-
* It is easy to deploy as a sidecar to the PHP worker processes (see [docker-compose.yml](docker-compose.yml)).
16-
* It provides us with easy to use concurrency mechanisms we need for the metric aggregation (e.g. `incrByFloat`).
17-
18-
We think this could be implemented with APCu as well and we might do so in the future.
19-
Of course we would also appreciate a pull-request.
20-
13+
You can pick from two adapters.
14+
One uses Redis the other APC.
15+
While the former needs a separate binary running, the latter just needs the [APC](https://pecl.php.net/package/APCU) extension to be installed.
2116

2217
## Usage
2318

@@ -73,8 +68,9 @@ Also look at the [examples](examples).
7368

7469
### Dependencies
7570

76-
* PHP 5.3/5.6 (at least these versions are tested at the moment)
71+
* PHP 5.6
7772
* PHP Redis extension
73+
* PHP APCu extension
7874
* [Composer](https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx)
7975
* Redis
8076

@@ -98,5 +94,10 @@ Just start the nginx, fpm & Redis setup with docker-compose:
9894
```
9995
composer require guzzlehttp/guzzle=~6.0
10096
docker-compose up
101-
vendor/bin/phpunit tests/Test/BlackBoxTest.php
97+
```
98+
Pick the adapter you want to test.
99+
100+
```
101+
ADAPTER=redis vendor/bin/phpunit tests/Test/BlackBoxTest.php
102+
ADAPTER=apc vendor/bin/phpunit tests/Test/BlackBoxTest.php
102103
```

composer.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@
44
{
55
"name": "Joscha",
66
"email": "joscha@schnipseljagd.org"
7+
},
8+
{
9+
"name": "Jan Brauer",
10+
"email": "jan.brauer@jimdo.com"
711
}
812
],
913
"require": {
10-
"php": ">=5.3.3",
11-
"ext-redis": "*"
14+
"php": ">=5.6.3"
1215
},
1316
"require-dev": {
1417
"phpunit/phpunit": "4.1.0"
1518
},
1619
"suggest": {
17-
"guzzlehttp/guzzle": "~6.0 for running the blackbox tests."
20+
"guzzlehttp/guzzle": "~6.0 for running the blackbox tests.",
21+
"ext-redis": "Required if using Redis.",
22+
"ext-apc": "Required if using APCu."
1823
},
1924
"autoload": {
2025
"psr-0": {

examples/flush_adapter.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
require __DIR__ . '/../vendor/autoload.php';
3+
4+
$adapter = $_GET['adapter'];
5+
6+
if ($adapter == 'redis') {
7+
define('REDIS_HOST', isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1');
8+
9+
$redisAdapter = new Prometheus\Storage\Redis(array('host' => REDIS_HOST));
10+
$redisAdapter->flushRedis();
11+
}
12+
13+
if ($adapter == 'apc') {
14+
$apcAdapter = new Prometheus\Storage\APC();
15+
$apcAdapter->flushAPC();
16+
}

examples/flush_redis.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

examples/metrics.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@
66
use Prometheus\RenderTextFormat;
77
use Prometheus\Storage\Redis;
88

9-
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
10-
$registry = CollectorRegistry::getDefault();
9+
$adapter = $_GET['adapter'];
10+
11+
if ($adapter == 'redis') {
12+
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
13+
$adapter = new Prometheus\Storage\Redis();
14+
}
15+
if ($adapter == 'apc') {
16+
$adapter = new Prometheus\Storage\APC();
17+
}
18+
$registry = new CollectorRegistry($adapter);
1119
$renderer = new RenderTextFormat();
1220
$result = $renderer->render($registry->getMetricFamilySamples());
1321

examples/some_counter.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77

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

10-
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
11-
$registry = CollectorRegistry::getDefault();
10+
$adapter = $_GET['adapter'];
11+
12+
if ($adapter == 'redis') {
13+
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
14+
$adapter = new Prometheus\Storage\Redis();
15+
}
16+
if ($adapter == 'apc') {
17+
$adapter = new Prometheus\Storage\APC();
18+
}
19+
$registry = new CollectorRegistry($adapter);
1220

1321
$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
1422
$counter->incBy($_GET['c'], ['blue']);

examples/some_gauge.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88

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

11-
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
12-
$registry = CollectorRegistry::getDefault();
11+
$adapter = $_GET['adapter'];
12+
13+
if ($adapter == 'redis') {
14+
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
15+
$adapter = new Prometheus\Storage\Redis();
16+
}
17+
if ($adapter == 'apc') {
18+
$adapter = new Prometheus\Storage\APC();
19+
}
20+
$registry = new CollectorRegistry($adapter);
1321

1422
$gauge = $registry->registerGauge('test', 'some_gauge', 'it sets', ['type']);
1523
$gauge->set($_GET['c'], ['blue']);

examples/some_histogram.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77

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

10-
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
11-
$registry = CollectorRegistry::getDefault();
10+
$adapter = $_GET['adapter'];
11+
12+
if ($adapter == 'redis') {
13+
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
14+
$adapter = new Prometheus\Storage\Redis();
15+
}
16+
if ($adapter == 'apc') {
17+
$adapter = new Prometheus\Storage\APC();
18+
}
19+
$registry = new CollectorRegistry($adapter);
1220

1321
$histogram = $registry->registerHistogram('test', 'some_histogram', 'it observes', ['type'], [0.1, 1, 2, 3.5, 4, 5, 6, 7, 8, 9]);
1422
$histogram->observe($_GET['c'], ['blue']);

php-fpm/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
FROM php:5.6-fpm
22

33
RUN pecl install redis-2.2.8 && docker-php-ext-enable redis
4+
RUN pecl install apcu-4.0.11 && docker-php-ext-enable apcu
45

56
COPY www.conf /usr/local/etc/php-fpm.d/

0 commit comments

Comments
 (0)