PHProm: A Prometheus Datastore for PHP Applications
Introduction: If you’re familiar with PHP and Prometheus, you’re probably aware that PHP cannot store metrics on its own without a little help. The most common work-around for this is to introduce a separate datastore, like Redis, to house metric data. This solution comes with its own set of limitations and issues; thus, PHProm is born.
What? PHProm is a light-weight datastore for Prometheus metrics. It’s written in Go, has either a gRPC or REST interface, and comes with a PHP client library (and a “bundle” if you’re down with Symfony).
Why? There are already tools that exist; so, why do we need PHProm? You don’t. It was a pet-project I’d started because of some issues with existing tools and I wanted to make things better, and, let’s face it, I’m an engineer, I like to build things.
How? As mentioned: PHProm is very light-weight. It just makes use of the existing Prometheus package for Go and wraps a gRPC/REST interface over it. If you want to know more, I invite you check out the source code(s):
* server
* client
Examples! There are examples provided in READMEs for each repository, and even an entire, fully functional, end-to-end example here.
I won’t go too far in-depth; but, here is a small PHP snippet to give you a taste:
// connect to the server using grpc...
$phprom = new PHProm('127.0.0.1:3333', PHProm::GRPC_API);// ...or rest...
$phprom = new PHProm('127.0.0.1:3333', PHProm::REST_API);// create a simple counter...
$counter = (new Counter($phprom))
->setNamespace('example')
->setName('test_counter')
->setDescription('this is a test')
->setLabels(['foo']);// record/increment the counter...
$counter->record(1.2345, ['foo' => 'bar']);// fetch and return the metrics...
print($phprom->get());
As well, here is a short video of what you can expect (from the end-to-end example here):
Conclusion…
There are a handful of PHP Prometheus libraries that exist, and they can plug into Redis, APC, Memcache, etc. There is even a push-gateway that allows you to push metrics directly to Prometheus, instead of relying on a scraper. PHProm is my own answer to issues and limitations I encountered with existing solutions, and I thought I’d share it with the world.
Thanks for reading. I hope you found it interesting. Feedback and contributions welcomed!