prometheus_php

A PHP instrumentation library for Prometheus compatible with PHP 5.5 5.6, 7.0 and 7.1.

View the Project on GitHub

A Prometheus client library for PHP

Build Status Code Climate Test Coverage Latest Stable Version

This library aims to be a lightweight utility for instrumenting a PHP application using Prometheus. It is heavily inspired by the golang client.

Features

Missing features

Getting Started

Add this library to your project.

composer require bexiocom/prometheus_php:dev-master

Usage

Simple counter with no labels attached:

<?php

use Bexio\PrometheusPHP\Metric\Counter;
use Bexio\PrometheusPHP\Storage\InMemory;

$storage = new InMemory();
$counter = Counter::createFromValues('simple_counter', 'Just a simple counting');
$counter->inc();
$storage->persist($counter);

More enhanced counter with labels:

<?php

use Bexio\PrometheusPHP\Metric\CounterCollection;
use Bexio\PrometheusPHP\Storage\InMemory;

$storage = new InMemory();
$collection = CounterCollection::createFromValues('labeled_counter', 'Counting with labels', ['type']);
$blueCounter = $collection->withLabels(['type' => 'blue']);
$blueCounter->inc();
$redCounter = $collection->withLabels(['type' => 'red']);
$redCounter->add(42);
$storage->persist($collection);

Expose the metrics:

<?php

use Bexio\PrometheusPHP\Metric\Counter;
use Bexio\PrometheusPHP\Metric\CounterCollection;
use Bexio\PrometheusPHP\Output\TextRenderer;
use Bexio\PrometheusPHP\Storage\InMemory;
use GuzzleHttp\Stream\BufferStream;

$buffer = new BufferStream();
$renderer = TextRenderer::createFromStream($buffer);
$storage = new InMemory([
    'simple_counter' => [
        InMemory::DEFAULT_VALUE_INDEX => 1
    ],
    'labeled_counter' => [
        '{"type":"blue"}' => 1,
        '{"type":"red"}' => 42,
    ],
]);
$counter = Counter::createFromValues('simple_counter', 'Just a simple counting');
$renderer->render($counter, $storage->collectSamples($counter));
$collection = CounterCollection::createFromValues('labeled_counter', 'Counting with labels', ['type']);
$renderer->render($collection, $storage->collectSamples($collection));

header(sprintf('Content-Type: %s', TextRenderer::MIME_TYPE));
echo $buffer->getContents();

Use Redis as storage backend:

NOTE: When using the Redis storage, it is highly suggested to add ext-redis as requirement to your project.

<?php

use Bexio\PrometheusPHP\Storage\Redis;

$redis = new \Redis();
$redis->connect('localhost');
// Optional: tune your redis connection.
$redis->setOption(\Redis::OPT_READ_TIMEOUT, 10);
$storage = new Redis($redis, 'PROMETHEUS:');

Use Redis but open connection lacily:

<?php

use Bexio\PrometheusPHP\Storage\Redis;

$redis = new \Redis();
$storage = new Redis(new \Redis(), 'PROMETHEUS:', function(\Redis $redis) {
  if (!$redis->connect('localhost')) {
      throw new \Exception('Failed to connect to Redis server');
  }
  $redis->setOption(\Redis::OPT_READ_TIMEOUT, 10);
});

How to contribute

First off, thank you for considering contributing to this PHP Prometheus client library. It’s people like you that make it useful for more than a handful of people.

Pull requests are happily accepted, given they follow the following simple rules: