Phetric: Persistent Metrics for PHP applications

This is part two of three in a series on how we monitor application metrics on AddThis.com. See part 1.

Last month we open sourced two internally developed pieces of our metrics stack. Drew Stephens wrote about the first, MetricCatcher, which enables non-java applications to utilize Coda Hale’s Metrics Package. The second piece, which we use on AddThis.com to monitor internal application metrics, is Phetric.

AddThis.com attracts millions of page views each month across multiple servers on a complex site to which we are constantly adding new features and enhancements. In order to monitor our metrics during this process, we gather metrics with Phetric and MetricCatcher, sending them on to Ganglia and Graphite for recording and viewing.

Using Phetric

To initialize Phetric, first you must include Sender.php and call Phetric_Sender::init() which takes the following four arguments (the final two of which are optional):

  1. The host where we want to send our metrics
  2. The port where MetricCatcher is listening
  3. A string you want prepend to all metric names
  4. A boolean for if you want to send each metric as it comes in rather then waiting for the end. (off by default)

All metrics are sent (as JSON) using UDP, which is a non-blocking operation; sending metrics won’t ever hang your application. Phetric hooks into php’s shutdown event to send the metrics after your application code has finished. If you are debugging or running extremely long running application code, setting the auto flush boolean to true will mean you send all the metrics as they come in.

Phetric supports everything offered by Coda Hale’s metric Package except for health check. Take a look at the Phetric Readme to see how to implement each metric.

Verifying your metrics

There are two options for verifying the metrics that you are sending as you develop. The first is to point Phetric at an install of MetricCatcher and tail its logs. If you aren’t using MetricCatcher, you can trivially see the metrics that Phetric emits using Netcat. I have this function in my .bashrc to make testing easy:

catcher(){
    while true;
    do
         nc -w 1 -l -u 1420;
    done;
}

In Conclusion

Grab Phetric on Github, it’s available under the MIT License. If you want to extend it please submit a pull request. Have ideas or questions? Open an issue on Github.

If you haven’t already, check out Drew Stephens previous post on MetricCatcher, where our Phetric metrics end up. Also, check back here soon for the final part of this series.