diff options
Diffstat (limited to 'vendor/liuggio')
11 files changed, 348 insertions, 18 deletions
diff --git a/vendor/liuggio/statsd-php-client/CHANGELOG.md b/vendor/liuggio/statsd-php-client/CHANGELOG.md new file mode 100644 index 00000000..e7ee9a44 --- /dev/null +++ b/vendor/liuggio/statsd-php-client/CHANGELOG.md @@ -0,0 +1,17 @@ +CHANGELOG for 1.x.x +=================== + +This changelog references the relevant changes (bug and security fixes) done +in 1.x.x minor. + +To get the diff for a specific change, go to https://github.com/liuggio/statsd-php-client/commit/XXX where XXX is the change hash +To get the diff between two versions, go to https://github.com/liuggio/statsd-php-client/compare/v1.0.12...v1.0.13 + +### 1.0.14, 1.0.16 + * fix minor casing issue + * php 5.3.2 instead of php5.2 + +### 1.0.13 + * add changelog + * feature StatsdService abstracting Client/Factory usage + * feature Deal with sampling in StatsdService diff --git a/vendor/liuggio/statsd-php-client/Readme.md b/vendor/liuggio/statsd-php-client/README.md index 81083e05..e7c6f67a 100644 --- a/vendor/liuggio/statsd-php-client/Readme.md +++ b/vendor/liuggio/statsd-php-client/README.md @@ -46,24 +46,27 @@ Be careful, see the [Upgrading section](Readme.md#upgrade) for <= v1.0.4, there' ```php use Liuggio\StatsdClient\StatsdClient, Liuggio\StatsdClient\Factory\StatsdDataFactory, - Liuggio\StatsdClient\Sender\SocketSender; + Liuggio\StatsdClient\Sender\SocketSender, + Liuggio\StatsdClient\Service\StatsdService; // use Liuggio\StatsdClient\Sender\SysLogSender; $sender = new SocketSender(/*'localhost', 8126, 'udp'*/); // $sender = new SysLogSender(); // enabling this, the packet will not send over the socket -$client = new StatsdClient($sender); +$client = new StatsdClient($sender); $factory = new StatsdDataFactory('\Liuggio\StatsdClient\Entity\StatsdData'); +$service = new StatsdService($client, $factory); + +// create the metrics with the service +$service->timing('usageTime', 100); +$service->increment('visitor'); +$service->decrement('click'); +$service->gauge('gaugor', 333); +$service->set('uniques', 765); -// create the data with the factory -$data[] = $factory->timing('usageTime', 100); -$data[] = $factory->increment('visitor'); -$data[] = $factory->decrement('click'); -$data[] = $factory->gauge('gaugor', 333); -$data[] = $factory->set('uniques', 765); +// send the data to statsd +$service->flush(); -// send the data as array or directly as object -$client->send($data); ``` ### Usage with Monolog diff --git a/vendor/liuggio/statsd-php-client/composer.json b/vendor/liuggio/statsd-php-client/composer.json index 705dbdf8..b47e0fd6 100644 --- a/vendor/liuggio/statsd-php-client/composer.json +++ b/vendor/liuggio/statsd-php-client/composer.json @@ -6,7 +6,7 @@ "type": "library", "license": "MIT", "require": { - "php": ">=5.2" + "php": ">=5.3.2" }, "authors": [ { diff --git a/vendor/liuggio/statsd-php-client/phpunit.xml.dist b/vendor/liuggio/statsd-php-client/phpunit.xml index 621efd75..621efd75 100644 --- a/vendor/liuggio/statsd-php-client/phpunit.xml.dist +++ b/vendor/liuggio/statsd-php-client/phpunit.xml 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; diff --git a/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/Entity/StatsdDataTest.php b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/Entity/StatsdDataTest.php index fad9175c..fad9175c 100644..100755 --- a/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/Entity/StatsdDataTest.php +++ b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/Entity/StatsdDataTest.php diff --git a/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/Service/StatsdServiceTest.php b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/Service/StatsdServiceTest.php new file mode 100644 index 00000000..dcf2710d --- /dev/null +++ b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/Service/StatsdServiceTest.php @@ -0,0 +1,86 @@ +<?php + +namespace Liuggio\StatsdClient\Entity; + +use Liuggio\StatsdClient\Service\StatsdService; + +class StatsdServiceTest extends \PHPUnit_Framework_TestCase +{ + private $clientMock; + private $factoryMock; + + public function setUp() + { + $this->clientMock = $this->getMockBuilder('Liuggio\StatsdClient\StatsdClient') + ->disableOriginalConstructor() + ->getMock(); + $this->factoryMock = $this->getMockBuilder('Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface') + ->disableOriginalConstructor() + ->getMock(); + } + + public function testConstructorWithoutFactory() + { + $dut = new StatsdService($this->clientMock); + $dut->timing('foo.bar', 123); + } + + public function testFactoryImplementation() + { + $data = new StatsdData(); + + // Configure the client mock. + $this->factoryMock->expects($this->once())->method('timing')->willReturn($data); + $this->factoryMock->expects($this->once())->method('gauge')->willReturn($data); + $this->factoryMock->expects($this->once())->method('set')->willReturn($data); + $this->factoryMock->expects($this->once())->method('increment')->willReturn($data); + $this->factoryMock->expects($this->once())->method('decrement')->willReturn($data); + $this->factoryMock->expects($this->once())->method('updateCount')->willReturn($data); + + // Actual test + $dut = new StatsdService($this->clientMock, $this->factoryMock); + $dut->timing('foo.bar', 123); + $dut->gauge('foo.bar', 123); + $dut->set('foo.bar', 123); + $dut->increment('foo.bar'); + $dut->decrement('foo.bar'); + $dut->updateCount('foo.bar', 123); + } + + public function testFlush() + { + $data = new StatsdData(); + $this->factoryMock->expects($this->once())->method('timing')->willReturn($data); + $this->clientMock->expects($this->once())->method('send') + ->with($this->equalTo(array($data))); + + // Actual test + $dut = new StatsdService($this->clientMock, $this->factoryMock); + $dut->timing('foobar', 123); + $dut->flush(); + } + + public function testSampling() + { + $tries = false; + $closure = function($a, $b) use (&$tries) { + $tries = !$tries; + return $tries ? 1 : 0; + }; + + $data = new StatsdData(); + $this->factoryMock->expects($this->exactly(2))->method('timing')->willReturn($data); + $this->clientMock->expects($this->once())->method('send') + ->with($this->equalTo(array($data))); + + // Actual test + $dut = new StatsdService($this->clientMock, $this->factoryMock); + $dut->setSamplingRate(0.1); + $dut->setSamplingFunction($closure); + $dut->timing('foo', 123); + $dut->timing('bar', 123); + $dut->flush(); + + $this->assertSame(0.1, $data->getSampleRate()); + } +} diff --git a/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/StatsdDataFactoryTest.php b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/StatsdDataFactoryTest.php index 144f629c..144f629c 100644..100755 --- a/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/StatsdDataFactoryTest.php +++ b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/StatsdDataFactoryTest.php |