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

Commit

Permalink
Merge pull request #25 from Jimdo/apc_storage
Browse files Browse the repository at this point in the history
Use APC as a storage adapter
  • Loading branch information
bracki authored Sep 7, 2016
2 parents 2359025 + c15f673 commit 43fc3bd
Show file tree
Hide file tree
Showing 25 changed files with 936 additions and 298 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
language: php
php:
- 5.6
- 5.3

services:
- redis-server

before_script:
- echo yes | pecl install apcu-4.0.11
- echo "apc.enabled = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- echo "apc.enable_cli = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- composer self-update
- composer install --no-interaction --prefer-source --dev
- phpenv rehash
- php -i

script:
- vendor/bin/phpunit --verbose --colors
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@
[![Code Climate](https://codeclimate.com/github/Jimdo/prometheus_client_php.png)](https://codeclimate.com/github/Jimdo/prometheus_client_php)


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

## Why Redis?
## How does it work?

Usually PHP worker processes don't share any state.

We decided to use Redis because:
* It is easy to deploy as a sidecar to the PHP worker processes (see [docker-compose.yml](docker-compose.yml)).
* It provides us with easy to use concurrency mechanisms we need for the metric aggregation (e.g. `incrByFloat`).

We think this could be implemented with APCu as well and we might do so in the future.
Of course we would also appreciate a pull-request.

You can pick from two adapters.
One uses Redis the other APC.
While the former needs a separate binary running, the latter just needs the [APC](https://pecl.php.net/package/APCU) extension to be installed.

## Usage

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

### Dependencies

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

Expand All @@ -98,5 +94,10 @@ Just start the nginx, fpm & Redis setup with docker-compose:
```
composer require guzzlehttp/guzzle=~6.0
docker-compose up
vendor/bin/phpunit tests/Test/BlackBoxTest.php
```
Pick the adapter you want to test.

```
ADAPTER=redis vendor/bin/phpunit tests/Test/BlackBoxTest.php
ADAPTER=apc vendor/bin/phpunit tests/Test/BlackBoxTest.php
```
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
{
"name": "Joscha",
"email": "[email protected]"
},
{
"name": "Jan Brauer",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.3.3",
"ext-redis": "*"
"php": ">=5.6.3"
},
"require-dev": {
"phpunit/phpunit": "4.1.0"
},
"suggest": {
"guzzlehttp/guzzle": "~6.0 for running the blackbox tests."
"guzzlehttp/guzzle": "~6.0 for running the blackbox tests.",
"ext-redis": "Required if using Redis.",
"ext-apc": "Required if using APCu."
},
"autoload": {
"psr-0": {
Expand Down
16 changes: 16 additions & 0 deletions examples/flush_adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

$adapter = $_GET['adapter'];

if ($adapter == 'redis') {
define('REDIS_HOST', isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1');

$redisAdapter = new Prometheus\Storage\Redis(array('host' => REDIS_HOST));
$redisAdapter->flushRedis();
}

if ($adapter == 'apc') {
$apcAdapter = new Prometheus\Storage\APC();
$apcAdapter->flushAPC();
}
7 changes: 0 additions & 7 deletions examples/flush_redis.php

This file was deleted.

12 changes: 10 additions & 2 deletions examples/metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@
use Prometheus\RenderTextFormat;
use Prometheus\Storage\Redis;

Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
$registry = CollectorRegistry::getDefault();
$adapter = $_GET['adapter'];

if ($adapter == 'redis') {
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
$adapter = new Prometheus\Storage\Redis();
}
if ($adapter == 'apc') {
$adapter = new Prometheus\Storage\APC();
}
$registry = new CollectorRegistry($adapter);
$renderer = new RenderTextFormat();
$result = $renderer->render($registry->getMetricFamilySamples());

Expand Down
12 changes: 10 additions & 2 deletions examples/some_counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@

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

Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
$registry = CollectorRegistry::getDefault();
$adapter = $_GET['adapter'];

if ($adapter == 'redis') {
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
$adapter = new Prometheus\Storage\Redis();
}
if ($adapter == 'apc') {
$adapter = new Prometheus\Storage\APC();
}
$registry = new CollectorRegistry($adapter);

$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
$counter->incBy($_GET['c'], ['blue']);
Expand Down
12 changes: 10 additions & 2 deletions examples/some_gauge.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@

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

Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
$registry = CollectorRegistry::getDefault();
$adapter = $_GET['adapter'];

if ($adapter == 'redis') {
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
$adapter = new Prometheus\Storage\Redis();
}
if ($adapter == 'apc') {
$adapter = new Prometheus\Storage\APC();
}
$registry = new CollectorRegistry($adapter);

$gauge = $registry->registerGauge('test', 'some_gauge', 'it sets', ['type']);
$gauge->set($_GET['c'], ['blue']);
Expand Down
12 changes: 10 additions & 2 deletions examples/some_histogram.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@

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

Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
$registry = CollectorRegistry::getDefault();
$adapter = $_GET['adapter'];

if ($adapter == 'redis') {
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
$adapter = new Prometheus\Storage\Redis();
}
if ($adapter == 'apc') {
$adapter = new Prometheus\Storage\APC();
}
$registry = new CollectorRegistry($adapter);

$histogram = $registry->registerHistogram('test', 'some_histogram', 'it observes', ['type'], [0.1, 1, 2, 3.5, 4, 5, 6, 7, 8, 9]);
$histogram->observe($_GET['c'], ['blue']);
Expand Down
1 change: 1 addition & 0 deletions php-fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
FROM php:5.6-fpm

RUN pecl install redis-2.2.8 && docker-php-ext-enable redis
RUN pecl install apcu-4.0.11 && docker-php-ext-enable apcu

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

0 comments on commit 43fc3bd

Please sign in to comment.