|
@@ -7,6 +7,16 @@ namespace StatsDBundle\Services;
|
|
|
*/
|
|
|
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
|
|
@@ -14,9 +24,9 @@ class StatsD
|
|
|
* @param string|array $stats The metric(s) to set.
|
|
|
* @param float $time The elapsed time (ms) to log
|
|
|
*/
|
|
|
- public static function timing($stats, $time)
|
|
|
+ public function timing($stats, $time)
|
|
|
{
|
|
|
- StatsD::updateStats($stats, $time, 1, 'ms');
|
|
|
+ return $this->updateStats($stats, $time, 1, 'ms');
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -25,9 +35,9 @@ class StatsD
|
|
|
* @param string|array $stats The metric(s) to set.
|
|
|
* @param float $value The value for the stats.
|
|
|
*/
|
|
|
- public static function gauge($stats, $value)
|
|
|
+ public function gauge($stats, $value)
|
|
|
{
|
|
|
- StatsD::updateStats($stats, $value, 1, 'g');
|
|
|
+ return $this->updateStats($stats, $value, 1, 'g');
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -44,9 +54,9 @@ class StatsD
|
|
|
* @param string|array $stats The metric(s) to set.
|
|
|
* @param float $value The value for the stats.
|
|
|
*/
|
|
|
- public static function set($stats, $value)
|
|
|
+ public function set($stats, $value)
|
|
|
{
|
|
|
- StatsD::updateStats($stats, $value, 1, 's');
|
|
|
+ return $this->updateStats($stats, $value, 1, 's');
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -59,7 +69,7 @@ class StatsD
|
|
|
*/
|
|
|
public static function increment($stats, $sampleRate = 1)
|
|
|
{
|
|
|
- StatsD::updateStats($stats, 1, $sampleRate, 'c');
|
|
|
+ return $this->updateStats($stats, 1, $sampleRate, 'c');
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -72,7 +82,7 @@ class StatsD
|
|
|
*/
|
|
|
public static function decrement($stats, $sampleRate = 1)
|
|
|
{
|
|
|
- StatsD::updateStats($stats, -1, $sampleRate, 'c');
|
|
|
+ $this->updateStats($stats, -1, $sampleRate, 'c');
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -95,7 +105,7 @@ class StatsD
|
|
|
$data[$stat] = "$delta|$metric";
|
|
|
}
|
|
|
|
|
|
- StatsD::send($data, $sampleRate);
|
|
|
+ $this->send($data, $sampleRate);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -106,10 +116,10 @@ class StatsD
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
- public static function send($data, $sampleRate = 1)
|
|
|
+ public function send($data, $sampleRate = 1)
|
|
|
{
|
|
|
- $config = Config::getInstance();
|
|
|
- if (!$config->isEnabled("statsd")) {
|
|
|
+ $config = $this->config;
|
|
|
+ if (!$config["enabled"]){
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -136,8 +146,8 @@ class StatsD
|
|
|
$count = 1;
|
|
|
// print_r("Total: ".count($sampledData).PHP_EOL);
|
|
|
try {
|
|
|
- $host = $config->getConfig("statsd.host");
|
|
|
- $port = $config->getConfig("statsd.port");
|
|
|
+ $host = $config["host"];
|
|
|
+ $port = $config["port"];
|
|
|
$fp = fsockopen("udp://$host", $port, $errno, $errstr);
|
|
|
if (!$fp) {
|
|
|
return;
|