Skip to content

Commit

Permalink
Merge pull request endclothing#36 from krukru/InMemory-impl
Browse files Browse the repository at this point in the history
In memory impl
  • Loading branch information
bracki authored Apr 4, 2017
2 parents 1d8c990 + 629961d commit 30fd249
Show file tree
Hide file tree
Showing 12 changed files with 354 additions and 59 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ If using Redis, we recommend to run a local Redis instance next to your PHP work
## How does it work?

Usually PHP worker processes don't share any state.
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.
You can pick from three adapters.
Redis, APC or an in memory adapter.
While the first needs a separate binary running, the second just needs 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.

## Usage

Expand Down Expand Up @@ -73,6 +73,17 @@ Change the Redis options (the example shows the defaults):
);
```

Using the InMemory storage:
```php
$registry = new CollectorRegistry(new InMemory());

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

$renderer = new RenderTextFormat();
$result = $renderer->render($registry->getMetricFamilySamples());
```

Also look at the [examples](examples).

## Development
Expand Down
11 changes: 6 additions & 5 deletions examples/flush_adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

$adapter = $_GET['adapter'];

if ($adapter == 'redis') {
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') {
} elseif ($adapter === 'apc') {
$apcAdapter = new Prometheus\Storage\APC();
$apcAdapter->flushAPC();
}
} elseif ($adapter === 'in-memory') {
$inMemoryAdapter = new Prometheus\Storage\InMemory();
$inMemoryAdapter->flushMemory();
}
7 changes: 4 additions & 3 deletions examples/metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

$adapter = $_GET['adapter'];

if ($adapter == 'redis') {
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') {
} elseif ($adapter === 'apc') {
$adapter = new Prometheus\Storage\APC();
} elseif ($adapter === 'in-memory') {
$adapter = new Prometheus\Storage\InMemory();
}
$registry = new CollectorRegistry($adapter);
$renderer = new RenderTextFormat();
Expand Down
13 changes: 12 additions & 1 deletion examples/pushgateway.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

use Prometheus\Storage\Redis;
use Prometheus\CollectorRegistry;

$adapter = new Prometheus\Storage\APC();
$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();
} elseif ($adapter === 'apc') {
$adapter = new Prometheus\Storage\APC();
} elseif ($adapter === 'in-memory') {
$adapter = new Prometheus\Storage\InMemory();
}

$registry = new CollectorRegistry($adapter);

$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
Expand Down
9 changes: 4 additions & 5 deletions examples/some_counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
use Prometheus\CollectorRegistry;
use Prometheus\Storage\Redis;

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

$adapter = $_GET['adapter'];

if ($adapter == 'redis') {
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') {
} elseif ($adapter === 'apc') {
$adapter = new Prometheus\Storage\APC();
} elseif ($adapter === 'in-memory') {
$adapter = new Prometheus\Storage\InMemory();
}
$registry = new CollectorRegistry($adapter);

Expand Down
7 changes: 4 additions & 3 deletions examples/some_gauge.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

$adapter = $_GET['adapter'];

if ($adapter == 'redis') {
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') {
} elseif ($adapter === 'apc') {
$adapter = new Prometheus\Storage\APC();
} elseif ($adapter === 'in-memory') {
$adapter = new Prometheus\Storage\InMemory();
}
$registry = new CollectorRegistry($adapter);

Expand Down
7 changes: 4 additions & 3 deletions examples/some_histogram.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@

$adapter = $_GET['adapter'];

if ($adapter == 'redis') {
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') {
} elseif ($adapter === 'apc') {
$adapter = new Prometheus\Storage\APC();
} elseif ($adapter === 'in-memory') {
$adapter = new Prometheus\Storage\InMemory();
}
$registry = new CollectorRegistry($adapter);

Expand Down
Loading

0 comments on commit 30fd249

Please sign in to comment.