Serverless, ReactPHP, and Expanding Frontiers, May 2019

(singke) #1
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


  1. <?php



  2. $stream = new Subject(); // The stream using for the metrics



  3. $kernel->execute(function () use ($bunny, $stream, $logger) {

  4. $logger->debug('Connecting');

  5. try {

  6. $bunny = yield $bunny->connect();

  7. } catch (ClientException $ce) {

  8. // Already connected

  9. }



  10. /* @var Channel $channel /

  11. $channel = yield $bunny->channel();



  12. $stream->subscribe(

  13. function (Metric $metric) use ($channel) {

  14. $channel->publish(

  15. 'PREFIX.APP_TYPE.APP_NAME.'
    20.. $metric->getKey(). ' '. $metric->getValue()
    21.. ' '. $metric->getTime(),

  16. [], '', 'graphite');

  17. }

  18. );

  19. });


https://phpa.me/podcast-ep-



  • 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:

Free download pdf