http://www.phparch.com \ May 2019 \ 7
Deploying ReactPHP Applications
and open streams. Additionally, I also monitor HTTP server
requests per second, active child processes count and call
queue, thread count, average response time, active corou-
tine count from RecoilPHP^19 , API client requests per second,
currently active API client requests, currently active HTTP
server requests, and even more where there is a need for it.
Collecting
There are several ways to get all of that data from the
application. There are many tools for both push and pull.
Prometheus is a prominent pull tool. However, in this article,
we’ll be quickly going over using RabbitMQ and Graphite
(push).
To push messages onto a RabbitMQ queue, I’m using
BunnyPHP^20 which hooks in nicely with the event loop making
all communication with RabbitMQ non-blocking. To get
started we need a connection to Bunny and an open channel
before we can push metrics. The example shown in Listing 4
uses a ReactPHP kernel from the RecoilPHP project listening
on an RxPHP subject and then pushes all metrics as a single
line each to the graphite queue.
To get the metrics from RabbitMQ into Graphite I’m using
Telegraf^21 , which picks up the metrics and ship them to
Graphite.
19 RecoilPHP: https://github.com/recoilphp/react
20 BunnyPHP: https://packagist.org/packages/bunny/bunny
21 Telegraf: https://github.com/influxdata/telegraf
Listing 4
- <?php
- $stream = new Subject(); // The stream using for the metrics
- $kernel->execute(function () use ($bunny, $stream, $logger) {
- $logger->debug('Connecting');
- try {
- $bunny = yield $bunny->connect();
- } catch (ClientException $ce) {
- // Already connected
- }
- /* @var Channel $channel /
- $channel = yield $bunny->channel();
- $stream->subscribe(
- function (Metric $metric) use ($channel) {
- $channel->publish(
- 'PREFIX.APP_TYPE.APP_NAME.'
20.. $metric->getKey(). ' '. $metric->getValue()
21.. ' '. $metric->getTime(), - [], '', 'graphite');
- }
- );
- });
- New Features of CSS
- Caching application output
- Managing Support Issues
- An interview with Chris Hartjes
(@grmpyprogrammer) about testing,
podcasting, and more.
Listen to Episode 19: