summaryrefslogtreecommitdiff
path: root/vendor/liuggio/statsd-php-client/src
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/liuggio/statsd-php-client/src')
-rwxr-xr-x[-rw-r--r--]vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdData.php28
-rwxr-xr-x[-rw-r--r--]vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdDataInterface.php6
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Service/StatsdService.php196
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/StatsdClient.php8
4 files changed, 231 insertions, 7 deletions
diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdData.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdData.php
index 9c6203db..2f030859 100644..100755
--- a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdData.php
+++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdData.php
@@ -10,6 +10,7 @@ class StatsdData implements StatsdDataInterface
private $key;
private $value;
private $metric;
+ private $sampleRate = 1;
/**
* @param string $key
@@ -55,6 +56,22 @@ class StatsdData implements StatsdDataInterface
}
/**
+ * @param float $sampleRate
+ */
+ public function setSampleRate($sampleRate)
+ {
+ $this->sampleRate = $sampleRate;
+ }
+
+ /**
+ * @return float
+ */
+ public function getSampleRate()
+ {
+ return $this->sampleRate;
+ }
+
+ /**
* @param bool $withMetric
*
* @return string
@@ -62,10 +79,17 @@ class StatsdData implements StatsdDataInterface
public function getMessage($withMetric = true)
{
if (!$withMetric) {
- return sprintf('%s:%s', $this->getKey(), $this->getValue());
+ $result = sprintf('%s:%s', $this->getKey(), $this->getValue());
} else {
- return sprintf('%s:%s|%s', $this->getKey(), $this->getValue(), $this->getMetric());
+ $result = sprintf('%s:%s|%s', $this->getKey(), $this->getValue(), $this->getMetric());
}
+
+ $sampleRate = $this->getSampleRate();
+ if($sampleRate < 1){
+ $result.= "|@$sampleRate";
+ }
+
+ return $result;
}
/**
diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdDataInterface.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdDataInterface.php
index 846bc7cc..d7b07a80 100644..100755
--- a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdDataInterface.php
+++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdDataInterface.php
@@ -35,6 +35,12 @@ interface StatsdDataInterface
/**
* @abstract
+ * @return float
+ */
+ function getSampleRate();
+
+ /**
+ * @abstract
* @return string
*/
function __toString();
diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Service/StatsdService.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Service/StatsdService.php
new file mode 100644
index 00000000..061e3510
--- /dev/null
+++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Service/StatsdService.php
@@ -0,0 +1,196 @@
+<?php
+
+namespace Liuggio\StatsdClient\Service;
+
+use Liuggio\StatsdClient\Entity\StatsdDataInterface;
+use Liuggio\StatsdClient\Factory\StatsdDataFactory;
+use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
+use Liuggio\StatsdClient\StatsdClient;
+use Liuggio\StatsdClient\Entity\StatsdData;
+
+/**
+ * Simplifies access to StatsD client and factory, buffers all data.
+ */
+class StatsdService implements StatsdDataFactoryInterface
+{
+ /**
+ * @var \Liuggio\StatsdClient\StatsdClient
+ */
+ protected $client;
+
+ /**
+ * @var \Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface
+ */
+ protected $factory;
+
+ /**
+ * @var StatsdData[]
+ */
+ protected $buffer = array();
+
+ /**
+ * @var float
+ */
+ protected $samplingRate;
+
+ private $samplingFunction;
+
+ /**
+ * @param StatsdClient $client
+ * @param StatsdDataFactoryInterface $factory
+ * @param float $samplingRate see setSamplingRate
+ */
+ public function __construct(
+ StatsdClient $client,
+ StatsdDataFactoryInterface $factory = null,
+ $samplingRate = 1
+ ) {
+ $this->client = $client;
+ if (is_null($factory)) {
+ $factory = new StatsdDataFactory();
+ }
+ $this->factory = $factory;
+ $this->setSamplingRate($samplingRate);
+ }
+
+ /**
+ * Actually defines the sampling rate used by the service.
+ * If set to 0.1, the service will automatically discard 10%
+ * of the incoming metrics. It will also automatically flag these
+ * as sampled data to statsd.
+ *
+ * @param float $samplingRate
+ */
+ public function setSamplingRate($samplingRate)
+ {
+ if ($samplingRate <= 0.0 || 1.0 < $samplingRate) {
+ throw new \LogicException('Sampling rate shall be within ]0, 1]');
+ }
+ $this->samplingRate = $samplingRate;
+ $this->samplingFunction = function($min, $max){
+ return rand($min, $max);
+ };
+ }
+
+ /**
+ * @return float
+ */
+ public function getSamplingRate()
+ {
+ return $this->samplingRate;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function timing($key, $time)
+ {
+ $this->appendToBuffer(
+ $this->factory->timing($key, $time)
+ );
+
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function gauge($key, $value)
+ {
+ $this->appendToBuffer(
+ $this->factory->gauge($key, $value)
+ );
+
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function set($key, $value)
+ {
+ $this->appendToBuffer(
+ $this->factory->set($key, $value)
+ );
+
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function increment($key)
+ {
+ $this->appendToBuffer(
+ $this->factory->increment($key)
+ );
+
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function decrement($key)
+ {
+ $this->appendToBuffer(
+ $this->factory->decrement($key)
+ );
+
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function updateCount($key, $delta)
+ {
+ $this->appendToBuffer(
+ $this->factory->updateCount($key, $delta)
+ );
+
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function produceStatsdData($key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT)
+ {
+ throw new \BadFunctionCallException('produceStatsdData is not implemented');
+ }
+
+ /**
+ * @param callable $samplingFunction rand() function by default.
+ */
+ public function setSamplingFunction(\Closure $samplingFunction)
+ {
+ $this->samplingFunction = $samplingFunction;
+ }
+
+ 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);
+ }
+ }
+
+ /**
+ * Send all buffered data to statsd.
+ *
+ * @return $this
+ */
+ public function flush()
+ {
+ $this->client->send($this->buffer);
+ $this->buffer = array();
+
+ return $this;
+ }
+}
diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/StatsdClient.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/StatsdClient.php
index a1d232a5..c5c16fed 100644
--- a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/StatsdClient.php
+++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/StatsdClient.php
@@ -109,12 +109,10 @@ class StatsdClient implements StatsdClientInterface
*/
public function appendSampleRate($data, $sampleRate = 1)
{
- $sampledData = array();
if ($sampleRate < 1) {
- foreach ($data as $key => $message) {
- $sampledData[$key] = sprintf('%s|@%s', $message, $sampleRate);
- }
- $data = $sampledData;
+ array_walk($data, function(&$message, $key) use ($sampleRate) {
+ $message = sprintf('%s|@%s', $message, $sampleRate);
+ });
}
return $data;