public function samplingDataProvider()
 {
     $unsampled = new StatsdData();
     $unsampled->setKey('foo');
     $unsampled->setValue(1);
     $sampled = new StatsdData();
     $sampled->setKey('foo');
     $sampled->setValue(1);
     $sampled->setSampleRate('0.1');
     return array(array($unsampled, 1, 0, $unsampled), array($sampled, 1, 0, null), array($sampled, 1, 4, $sampled), array($unsampled, 0.1, 4, $sampled), array($sampled, 0.5, 0, null), array($sampled, 0.5, 4, $sampled));
 }
 public function samplingDataProvider()
 {
     $unsampled = new StatsdData();
     $unsampled->setKey('foo');
     $unsampled->setValue(1);
     $sampled = new StatsdData();
     $sampled->setKey('foo');
     $sampled->setValue(1);
     $sampled->setSampleRate('0.1');
     return [[$unsampled, 1, 0, $unsampled], [$sampled, 1, 0, null], [$sampled, 1, 4, $sampled], [$unsampled, 0.1, 4, $sampled], [$sampled, 0.5, 0, null], [$sampled, 0.5, 4, $sampled]];
 }
 public function testGetMessage()
 {
     $statsdData = new StatsdData();
     $statsdData->setKey('key');
     $statsdData->setValue('value');
     $statsdData->setMetric('c');
     $this->assertEquals($statsdData->getMessage(), 'key:value|c');
     $statsdData = new StatsdData();
     $statsdData->setKey('key');
     $statsdData->setValue(-1);
     $statsdData->setMetric('c');
     $this->assertEquals($statsdData->getMessage(), 'key:-1|c');
 }
 public function testSetSamplingRates()
 {
     $matching = new StatsdData();
     $matching->setKey('foo.bar');
     $matching->setValue(1);
     $nonMatching = new StatsdData();
     $nonMatching->setKey('oof.bar');
     $nonMatching->setValue(1);
     $sender = $this->getMock('Liuggio\\StatsdClient\\Sender\\SenderInterface');
     $sender->expects($this->any())->method('open')->will($this->returnValue(true));
     $sender->expects($this->once())->method('write')->with($this->anything(), $this->equalTo($nonMatching));
     $client = new SamplingStatsdClient($sender);
     $client->setSamplingRates(['foo.*' => 0.2]);
     mt_srand(0);
     // next random is 0.44
     $client->send($matching);
     mt_srand(0);
     $client->send($nonMatching);
 }
 private function createStatData(Stat $stat, $template, array $parameters)
 {
     $nullReplacement = $this->nullReplacement;
     $parameters = array_merge($parameters, $stat->getParameters());
     $key = preg_replace_callback('/{(?P<name>[^}]+)}/', function ($matches) use($parameters, $nullReplacement) {
         $name = $matches['name'];
         if (!array_key_exists($name, $parameters)) {
             throw new \RuntimeException(sprintf('Unknown template parameter "%s", only %s available.', $name, join(', ', array_keys($parameters))));
         }
         $parameter = $parameters[$name];
         if (null === $parameter) {
             $parameter = $nullReplacement;
         }
         return $parameter;
     }, $template);
     $statData = new StatsdData();
     $statData->setMetric($stat->getType());
     $statData->setValue($stat->getValue());
     $statData->setKey($key);
     return $statData;
 }
 private function appendToBuffer(StatsdData $data)
 {
     if ($this->samplingRate < 1) {
         $data->setSampleRate($this->samplingRate);
         $result = call_user_func($this->samplingFunction, 0, floor(1 / $this->samplingRate));
         if ($result == 0) {
             array_push($this->buffer, $data);
         }
     } else {
         array_push($this->buffer, $data);
     }
 }
 public function testReduceCount()
 {
     $statsd = $this->mockStatsdClientWithAssertionOnWrite(null);
     $entity0 = new StatsdData();
     $entity0->setKey('key1');
     $entity0->setValue('1');
     $entity0->setMetric('c');
     $array0[] = $entity0;
     $entity0 = new StatsdData();
     $entity0->setKey('key2');
     $entity0->setValue('2');
     $entity0->setMetric('ms');
     $array0[] = $entity0;
     $reducedMessage = array('key1:1|c' . PHP_EOL . 'key2:2|ms');
     $this->assertEquals($statsd->reduceCount($array0), $reducedMessage);
 }
 /**
  * Produce a StatsdDataInterface Object.
  *
  * @param string $key The key of the metric
  * @param int $value The amount to increment/decrement each metric by.
  * @param string $metric The metric type
  *                      ("c" for count, "ms" for timing, "g" for gauge, "s" for set)
  *
  * @return StatsdDataInterface
  **/
 public function produceStatsdData($key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT)
 {
     $data = new StatsdData();
     $data->setKey($key);
     $data->setValue($value);
     $data->setMetric($metric);
     return $data;
 }