You are here

public function StatsD::send in StatsD 7.2

Same name and namespace in other branches
  1. 6 includes/statsd.inc \StatsD::send()
  2. 7 includes/statsd.inc \StatsD::send()
3 calls to StatsD::send()
StatsD::gauge in includes/statsd.inc
Sends a gauge, an arbitrary value 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 142

Class

StatsD
Sends statistics to the stats daemon over UDP.

Code

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);
  }
}