You are here

class StatsD in StatsD 7.2

Same name and namespace in other branches
  1. 6 includes/statsd.inc \StatsD
  2. 7 includes/statsd.inc \StatsD

Sends statistics to the stats daemon over UDP.

Properties have public visibility rather than protected to reduce clutter in statsd.admin.inc where they are used for configuration.

Hierarchy

Expanded class hierarchy of StatsD

3 string references to 'StatsD'
statsd.install in ./statsd.install
StatsDTest::getInfo in lib/Drupal/statsd/Tests/StatsDTest.php
Describe the test suite.
statsd_menu in ./statsd.module
Implementation of hook_menu()

File

includes/statsd.inc, line 10

View source
class StatsD {
  public $enabled = FALSE;
  public $sampleRate = 1;
  public $host = '127.0.0.1';
  public $port = 8125;
  public $prefix = '';
  public $suffix = '';

  /**
   * Constructor. Copy valid arguments to public properties, ignore others.
   *
   * @param array $arguments
   *   A name-value hash.
   */
  public function __construct($arguments) {
    $properties = $this
      ->getDefaultProperties();
    foreach ($arguments as $name => $value) {
      if (isset($properties[$name])) {
        $this->{$name} = $value;
      }
    }
  }

  /**
   * Return the default class properties.
   *
   * @return array
   *   A hash of name-default_value property descriptions.
   */
  public static function getDefaultProperties() {
    $statsd = new ReflectionClass(__CLASS__);

    // @todo: Find out if this can be done easier with get_object_vars()
    $properties = array();
    $properties['enabled'] = $statsd->enabled;
    $properties['sampleRate'] = $statsd->sampleRate;
    $properties['host'] = $statsd->host;
    $properties['port'] = $statsd->port;
    $properties['prefix'] = $statsd->prefix;
    $properties['suffix'] = $statsd->suffix;
    return $properties;
  }

  /**
   * Log timing information
   *
   * @param $stat
   *   A string of the metric to log timing info for.
   * @param $time
   *   The ellapsed time (ms) to log
   * @param $sample_rate
   *   A float between 0 and 1 representing the sampling rate.
   *
   */
  public function timing($stat, $time, $sample_rate = NULL) {
    $this
      ->send(array(
      $stat => "{$time}|ms",
    ), $sample_rate);
  }

  /**
   * Sends a gauge, an arbitrary value to statsd
   *
   * @param $stat
   *   The metric to send.
   * @param $value
   *   The value to send for this metric.
   * @param $sample_rate
   *   A float between 0 and 1 representing the sampling rate.
   *
   */
  public function gauge($stat, $value, $sample_rate = NULL) {
    $this
      ->send(array(
      $stat => "{$value}|g",
    ), $sample_rate);
  }

  /**
   * Increments one or more stats counters
   *
   * @param $stats
   *   A string or an array of string representing the metric(s) to increment.
   * @param $sample_rate
   *   A float between 0 and 1 representing the sampling rate.
   * @return boolean
   *
   */
  public function increment($stats, $sample_rate = NULL) {
    $this
      ->updateStats($stats, 1, $sample_rate);
  }

  /**
   * Decrements one or more stats counters.
   *
   * @param $stats
   *   A string or an array of string representing the metric(s) to decrement.
   * @param $sample_rate
   *   A float between 0 and 1 representing the sampling rate.
   * @return boolean
   *
   */
  public function decrement($stats, $sample_rate = NULL) {
    $this
      ->updateStats($stats, -1, $sample_rate);
  }

  /**
   * Updates one or more stats counters by arbitrary amounts.
   *
   * @param $stats
   *   A string or an array of string representing the metric(s) to increment or decrement.
   * @param $delta
   *   The amount to increment/decrement each metric by.
   * @param $sample_rate
   *   A float between 0 and 1 representing the sampling rate.
   * @return boolean
   *
   */
  public function updateStats($stats, $delta = 1, $sample_rate = NULL) {
    $data = array();
    if (!is_array($stats)) {
      $stats = array(
        $stats,
      );
    }
    foreach ($stats as $stat) {
      $data[$stat] = "{$delta}|c";
    }
    $this
      ->send($data, $sample_rate);
  }

  /*
   * Squirt the metrics over UDP
   *
   */
  public function send($data, $sample_rate = NULL) {
    if (!$this->enabled) {
      return;
    }
    $sample_rate = $sample_rate ? $sample_rate : $this->sampleRate;
    $sampled_data = array();
    $data = $this
      ->prefixData($data);
    if ($sample_rate < 1) {
      foreach ($data as $stat => $value) {
        if (mt_rand() / mt_getrandmax() <= $sample_rate) {
          $sampled_data[$stat] = "{$value}|@{$sample_rate}";
        }
      }
    }
    else {
      $sampled_data = $data;
    }
    if (empty($sampled_data)) {
      return;
    }
    $fp = stream_socket_client("udp://{$this->host}:{$this->port}", $errno, $errstr);
    if ($fp) {
      stream_set_blocking($fp, 0);
      foreach ($sampled_data as $stat => $value) {
        fwrite($fp, "{$stat}:{$value}");
      }
      fclose($fp);
    }
  }

  /**
   * Create the data strings that will be passed into statsd
   *
   * @param $data
   *   An array of key value pairs to prefix.
   *
   */
  protected function prefixData($data) {
    $prefix = $this->prefix ? $this->prefix . '.' : '';
    $suffix = $this->suffix ? '.' . $this->suffix : '';
    $return = array();
    foreach ($data as $key => $value) {
      $name = sprintf('%s' . $key . '%s', $prefix, $suffix);
      $return[$name] = $value;
    }
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StatsD::$enabled public property
StatsD::$host public property
StatsD::$port public property
StatsD::$prefix public property
StatsD::$sampleRate public property
StatsD::$suffix public property
StatsD::decrement public function Decrements one or more stats counters.
StatsD::gauge public function Sends a gauge, an arbitrary value to statsd
StatsD::getDefaultProperties public static function Return the default class properties.
StatsD::increment public function Increments one or more stats counters
StatsD::prefixData protected function Create the data strings that will be passed into statsd
StatsD::send public function
StatsD::timing public function Log timing information
StatsD::updateStats public function Updates one or more stats counters by arbitrary amounts.
StatsD::__construct public function Constructor. Copy valid arguments to public properties, ignore others.