123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace StatsDBundle\Services;
- /**
- * Sends statistics to the stats daemon over UDP
- */
- class StatsD
- {
- public function __construct($config = false){
- $this->config = array();
- $this->config["enabled"] = true;
- $this->config["host"] = "127.0.0.1";
- $this->config["port"] = 8125;
- if(is_array($config)){
- $this->config = $config;
- $this->config["enabled"] = true;
- }
- }
- /**
- * Sets one or more timing values
- *
- * @param string|array $stats The metric(s) to set.
- * @param float $time The elapsed time (ms) to log
- */
- public function timing($stats, $time)
- {
- return $this->updateStats($stats, $time, 1, 'ms');
- }
- /**
- * Sets one or more gauges to a value
- *
- * @param string|array $stats The metric(s) to set.
- * @param float $value The value for the stats.
- */
- public function gauge($stats, $value)
- {
- return $this->updateStats($stats, $value, 1, 'g');
- }
- /**
- * A "Set" is a count of unique events.
- * This data type acts like a counter, but supports counting
- * of unique occurences of values between flushes. The backend
- * receives the number of unique events that happened since
- * the last flush.
- *
- * The reference use case involved tracking the number of active
- * and logged in users by sending the current userId of a user
- * with each request with a key of "uniques" (or similar).
- *
- * @param string|array $stats The metric(s) to set.
- * @param float $value The value for the stats.
- */
- public function set($stats, $value)
- {
- return $this->updateStats($stats, $value, 1, 's');
- }
- /**
- * Increments one or more stats counters
- *
- * @param string|array $stats The metric(s) to increment.
- * @param float|1 $sampleRate the rate (0-1) for sampling.
- *
- * @return boolean
- */
- public function increment($stats, $sampleRate = 1)
- {
- return $this->updateStats($stats, 1, $sampleRate, 'c');
- }
- /**
- * Decrements one or more stats counters.
- *
- * @param string|array $stats The metric(s) to decrement.
- * @param float|1 $sampleRate the rate (0-1) for sampling.
- *
- * @return boolean
- */
- public function decrement($stats, $sampleRate = 1)
- {
- $this->updateStats($stats, -1, $sampleRate, 'c');
- }
- /**
- * Updates one or more stats.
- *
- * @param string|array $stats The metric(s) to update. Should be either a string or array of metrics.
- * @param int|1 $delta The amount to increment/decrement each metric by.
- * @param float|1 $sampleRate the rate (0-1) for sampling.
- * @param string|c $metric The metric type ("c" for count, "ms" for timing, "g" for gauge, "s" for set)
- *
- * @return boolean
- */
- public function updateStats($stats, $delta = 1, $sampleRate = 1, $metric = 'c')
- {
- if (!is_array($stats)) {
- $stats = array($stats);
- }
- $data = array();
- foreach ($stats as $stat) {
- $data[$stat] = "$delta|$metric";
- }
- $this->send($data, $sampleRate);
- }
- /**
- * Squirt the metrics over UDP
- *
- * @param array $data
- * @param int $sampleRate
- *
- * @return void
- */
- public function send($data, $sampleRate = 1)
- {
- $config = $this->config;
- if (!$config["enabled"]){
- return;
- }
- // sampling
- $sampledData = array();
- if ($sampleRate < 1) {
- foreach ($data as $stat => $value) {
- if ((mt_rand() / mt_getrandmax()) <= $sampleRate) {
- $sampledData[$stat] = "$value|@$sampleRate";
- }
- }
- } else {
- $sampledData = $data;
- }
-
- $sampledData = $data;
- if (empty($sampledData)) {
- return;
- }
- // Wrap this in a try/catch - failures in any of this should be silently ignored
- $count = 1;
- // print_r("Total: ".count($sampledData).PHP_EOL);
- try {
- $host = $config["host"];
- $port = $config["port"];
- $fp = fsockopen("udp://$host", $port, $errno, $errstr);
- if (!$fp) {
- return;
- }
- $count_send = 0;
- foreach ($sampledData as $stat => $value) {
- // print_r("$count - $stat:$value".PHP_EOL);
- $count++;
- fwrite($fp, "$stat:$value");
- $count_send++;
- if($count_send > 250) {
- sleep(2);
- $count_send = 0;
- }
- }
- fclose($fp);
- } catch (Exception $e) {
- // print_r($e->getMessage());
-
- }
- }
- }
|