瀏覽代碼

Merge branch 'master' of ssh://gogs.infra.flowdat.com:222/VendorSoftwareFlowdat3/StatsDBundle

Maximiliano Schvindt 7 年之前
父節點
當前提交
dcb4862615
共有 4 個文件被更改,包括 26 次插入95 次删除
  1. 2 1
      Resources/config/services.yml
  2. 0 4
      Resources/config/statsd.ini
  3. 0 76
      Services/Config.php
  4. 24 14
      Services/StatsD.php

+ 2 - 1
Resources/config/services.yml

@@ -1,6 +1,7 @@
 services:
     statsd:
         class: StatsDBundle\Services\StatsD
+        arguments: ['%statsd%']
     endpoint.mysql:
         class: StatsDBundle\Services\EndpointMysql
-        arguments: ["@service_container"]
+        arguments: ["@service_container"]

+ 0 - 4
Resources/config/statsd.ini

@@ -1,4 +0,0 @@
-[statsd]
-host = 200.50.168.118
-port = 8125
-[endpoint.mysql]

+ 0 - 76
Services/Config.php

@@ -1,76 +0,0 @@
-<?php
-
-namespace StatsDBundle\Services;
-
-class Config
-{
-    
-    const INI_FILE =  '../Resources/config/statsd.ini';
-
-    /**
-     * @var Config
-     */
-    private static $_instance;
-
-    /**
-     * @var array
-     */
-    private $_data;
-
-
-    /**
-     * @param string $ini_file
-     */
-    private function __construct($ini_file = self::INI_FILE)
-    {
-        if ($ini_file == self::INI_FILE) {
-            $ini_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . $ini_file;
-        }
-        $this->_data = parse_ini_file($ini_file, true);
-    }
-
-    /**
-     * @return Config
-     */
-    public static function getInstance()
-    {
-        if (!self::$_instance) {
-            self::$_instance = new self();
-        }
-
-        return self::$_instance;
-    }
-
-    /**
-     * @param string $section
-     * 
-     * @return boolean
-     */
-    public function isEnabled($section)
-    {
-        return isset($this->_data[$section]);
-    }
-
-    /**
-     * @param string $name
-     * 
-     * @return string
-     */
-    public function getConfig($name)
-    {
-        $name_array = explode('.', $name, 2);
-
-        if (count($name_array) < 2) {
-            return;
-        }
-
-        list($section, $param) = $name_array;
-
-        if (!isset($this->_data[$section][$param])) {
-            return;
-        }
-
-        return $this->_data[$section][$param];
-    }
-
-}

+ 24 - 14
Services/StatsD.php

@@ -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;