Bladeren bron

permite cambiar el hosts de statsd

Luciano Andrade 7 jaren geleden
bovenliggende
commit
b9c0a44786
4 gewijzigde bestanden met toevoegingen van 17 en 84 verwijderingen
  1. 2 1
      Resources/config/services.yml
  2. 0 3
      Resources/config/statsd.ini
  3. 0 76
      Services/Config.php
  4. 15 4
      Services/StatsD.php

+ 2 - 1
Resources/config/services.yml

@@ -1,3 +1,4 @@
 services:
     statsd:
-        class: StatsDBundle\Services\StatsD
+        class: StatsDBundle\Services\StatsD
+        arguments: ['%statsd%']

+ 0 - 3
Resources/config/statsd.ini

@@ -1,3 +0,0 @@
-[statsd]
-host = 127.0.0.1
-port = 8125

+ 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];
-    }
-
-}

+ 15 - 4
Services/StatsD.php

@@ -7,6 +7,17 @@ namespace StatsDBundle\Services;
  */
 class StatsD
 {
+    public function __construct($config = false){
+           $this->config = array();
+           if(false === $config){
+               $this->config["enabled"] = true;
+               $this->config["host"] = "127.0.0.1";
+               $this->config["port"] = 8125;
+           }else{
+               $this->confg = $config;
+               $this->config["enabled"] = true;
+           }
+    }
 
     /**
      * Sets one or more timing values
@@ -108,8 +119,8 @@ class StatsD
      */
     public static function send($data, $sampleRate = 1)
     {
-        $config = Config::getInstance();
-        if (!$config->isEnabled("statsd")) {
+        $config = $this->config;
+        if (!$config["enabled"]){
             return;
         }
 
@@ -136,8 +147,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;