You are here

public static function StatsD::send in StatsD 7

Same name and namespace in other branches
  1. 6 includes/statsd.inc \StatsD::send()
  2. 7.2 includes/statsd.inc \StatsD::send()

Squirt the metrics over UDP.

Parameters

array $data: The data to send.

float $sample_rate: A float between 0 and 1 representing the sample rate.

4 calls to StatsD::send()
StatsD::gauge in includes/statsd.inc
Sends a gauge, an arbitrary value to statsd.
StatsD::set in includes/statsd.inc
Sends one or more set values to statsd.
StatsD::timing in includes/statsd.inc
Log timing information.
StatsD::updateStats in includes/statsd.inc
Updates one or more stats counters by arbitrary amounts.

File

includes/statsd.inc, line 136

Class

StatsD
Sends statistics to the stats daemon over UDP

Code

public static function send($data, $sample_rate = NULL) {
  if (!variable_get('statsd_enabled', FALSE)) {
    return;
  }
  $sample_rate = $sample_rate ? $sample_rate : variable_get('statsd_sample_rate', 1);
  $sampled_data = array();
  $data = self::prefixData(self::sanitizeDataNames($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;
  }
  $host = variable_get('statsd_host', '127.0.0.1');
  $port = variable_get('statsd_port', 8125);
  $timeout = (double) variable_get('statsd_timeout', ini_get('default_socket_timeout'));
  $fp = stream_socket_client("udp://{$host}:{$port}", $errno, $errstr, $timeout);
  if ($fp) {
    stream_set_blocking($fp, 0);
    foreach ($sampled_data as $stat => $value) {
      fwrite($fp, "{$stat}:{$value}");
    }
    fclose($fp);
  }
}