Пример #1
0
 public function testSend()
 {
     $configuration = new Configuration();
     $configuration->setHost('127.0.0.1')->setNamespace('base.name')->setPort(123456);
     $client = new Client($configuration);
     $client->addStats(array(array('namespace' => 'some.namespace', 'value' => 12, 'type' => 'ms'), array('namespace' => 'some.namespace2', 'value' => 1, 'type' => 'c')));
     $messages = $client->sendStats();
     $expected = array('base.name.some.namespace:12|ms', 'base.name.some.namespace2:1|c');
     $this->assertSame($messages, $expected);
 }
Пример #2
0
 /**
  * Transmit the metrics in `$data` over UDP
  *
  * @param string[]  $data
  * @param float     [$sampleRate=1]
  */
 public function send($data, $sampleRate = 1)
 {
     if ($sampleRate < 1) {
         $data = \StatsD\Client::getSampledData($data, $sampleRate);
     }
     if (empty($data)) {
         return;
     }
     try {
         $fp = fsockopen("udp://{$this->host}", $this->port);
         if (!$fp) {
             return;
         }
         foreach ($data as $stat => $value) {
             fwrite($fp, "{$stat}:{$value}");
         }
         fclose($fp);
     } catch (Exception $e) {
     }
 }