From a1789ddde42033f1b05cc4929491214ee6e79383 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Thu, 17 Dec 2015 09:15:42 +0100 Subject: Update to MediaWiki 1.26.0 --- .../src/Liuggio/StatsdClient/Entity/StatsdData.php | 28 ++- .../StatsdClient/Entity/StatsdDataInterface.php | 6 + .../Liuggio/StatsdClient/Service/StatsdService.php | 196 +++++++++++++++++++++ .../src/Liuggio/StatsdClient/StatsdClient.php | 8 +- 4 files changed, 231 insertions(+), 7 deletions(-) create mode 100644 vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Service/StatsdService.php (limited to 'vendor/liuggio/statsd-php-client/src/Liuggio') 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 --- 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 @@ -54,6 +55,22 @@ class StatsdData implements StatsdDataInterface return $this->metric; } + /** + * @param float $sampleRate + */ + public function setSampleRate($sampleRate) + { + $this->sampleRate = $sampleRate; + } + + /** + * @return float + */ + public function getSampleRate() + { + return $this->sampleRate; + } + /** * @param bool $withMetric * @@ -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 --- a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdDataInterface.php +++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdDataInterface.php @@ -33,6 +33,12 @@ interface StatsdDataInterface */ function getMessage(); + /** + * @abstract + * @return float + */ + function getSampleRate(); + /** * @abstract * @return string 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 @@ +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; -- cgit v1.2.3-54-g00ecf