diff options
Diffstat (limited to 'vendor/liuggio')
24 files changed, 1903 insertions, 0 deletions
diff --git a/vendor/liuggio/statsd-php-client/LICENSE b/vendor/liuggio/statsd-php-client/LICENSE new file mode 100644 index 00000000..3af47475 --- /dev/null +++ b/vendor/liuggio/statsd-php-client/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) Giulio De Donato + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/vendor/liuggio/statsd-php-client/Readme.md b/vendor/liuggio/statsd-php-client/Readme.md new file mode 100644 index 00000000..81083e05 --- /dev/null +++ b/vendor/liuggio/statsd-php-client/Readme.md @@ -0,0 +1,149 @@ +## statsd-php-client + +Be careful, see the [Upgrading section](Readme.md#upgrade) for <= v1.0.4, there's a BC. + +[![Build Status](https://secure.travis-ci.org/liuggio/statsd-php-client.png)](http://travis-ci.org/liuggio/statsd-php-client) [![Latest Stable Version](https://poser.pugx.org/liuggio/statsd-php-client/v/stable.png)](https://packagist.org/packages/liuggio/statsd-php-client) [![Total Downloads](https://poser.pugx.org/liuggio/statsd-php-client/downloads.png)](https://packagist.org/packages/liuggio/statsd-php-client) + +`statsd-php-client` is an Open Source, and **Object Oriented** Client for **etsy/statsd** written in php + +- `StatsdDataFactory` creates the `Liuggio\StatsdClient\Entity\StatsdDataInterface` Objects + +- `Sender` just sends data over the network (there are many sender) + +- `StatsdClient` sends the created objects via the `Sender` to the server + +## Why use this library instead the [statsd/php-example](https://github.com/etsy/statsd/blob/master/examples/php-example.php)? + +- You are wise. + +- You could also use monolog to redirect data to statsd + +- This library is tested. + +- This library optimizes the messages to send, compressing multiple messages in individual UDP packets. + +- This library pays attention to the maximum length of the UDP. + +- This library is made by Objects not array, but it also accepts array. + +- You do want to debug the packets, and using `SysLogSender` the packets will be logged in your `syslog` log (on debian-like distro: `tail -f /var/log/syslog`) + + +## Example + +1. create the Sender + +2. create the Client + +3. create the Factory + +4. the Factory will help you to create data + +5. the Client will send the data + +### Standard Usage + +```php +use Liuggio\StatsdClient\StatsdClient, + Liuggio\StatsdClient\Factory\StatsdDataFactory, + Liuggio\StatsdClient\Sender\SocketSender; +// 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); +$factory = new StatsdDataFactory('\Liuggio\StatsdClient\Entity\StatsdData'); + +// 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 as array or directly as object +$client->send($data); +``` + +### Usage with Monolog + +```php +use Liuggio\StatsdClient\StatsdClient, + Liuggio\StatsdClient\Factory\StatsdDataFactory, + Liuggio\StatsdClient\Sender\SocketSender; +// use Liuggio\StatsdClient\Sender\SysLogSender; + +use Monolog\Logger; +use Liuggio\StatsdClient\Monolog\Handler\StatsDHandler; + +$sender = new SocketSender(/*'localhost', 8126, 'udp'*/); +// $sender = new SysLogSender(); // enabling this, the packet will not send over the socket +$client = new StatsdClient($sender); +$factory = new StatsdDataFactory(); + +$logger = new Logger('my_logger'); +$logger->pushHandler(new StatsDHandler($client, $factory, 'prefix', Logger::DEBUG)); + +$logger->addInfo('My logger is now ready'); +``` + +the output will be: `prefix.my_logger.INFO.My-logger:1|c" 36 Bytes` + + + + +## Short Theory + +### Easily Install StatSD and Graphite + +In order to try this application monitor you have to install etsy/statsd and Graphite + +see this blog post to install it with vagrant [Easy install statsd graphite](http://welcometothebundle.com/easily-install-statsd-and-graphite-with-vagrant/). + +#### [StatsD](https://github.com/etsy/statsd) + +StatsD is a simple daemon for easy stats aggregation + +#### [Graphite](http://graphite.wikidot.com/) + +Graphite is a Scalable Realtime Graphing + +#### The Client sends data with UDP (faster) + +https://www.google.com/search?q=tcp+vs+udp + +## Contribution + +Active contribution and patches are very welcome. +To keep things in shape we have quite a bunch of unit tests. If you're submitting pull requests please +make sure that they are still passing and if you add functionality please +take a look at the coverage as well it should be pretty high :) + +- First fork or clone the repository + +``` +git clone git://github.com/liuggio/statsd-php-client.git +cd statsd-php-client +``` + +- Install vendors: + +``` bash +composer.phar install +``` + +- This will give you proper results: + +``` bash +phpunit --coverage-html reports +``` + +## Upgrade + +BC from the v1.0.4 version, [see Sender and Client differences](https://github.com/liuggio/statsd-php-client/pull/5/files). + + +## TODO + +example with monolog diff --git a/vendor/liuggio/statsd-php-client/composer.json b/vendor/liuggio/statsd-php-client/composer.json new file mode 100644 index 00000000..705dbdf8 --- /dev/null +++ b/vendor/liuggio/statsd-php-client/composer.json @@ -0,0 +1,26 @@ +{ + "name": "liuggio/statsd-php-client", + "description": "Statsd (Object Oriented) client library for PHP", + "keywords": ["statsd", "monitoring", "etsy", "php"], + "homepage": "https://github.com/liuggio/statsd-php-client/", + "type": "library", + "license": "MIT", + "require": { + "php": ">=5.2" + }, + "authors": [ + { + "name": "Giulio De Donato", + "email": "liuggio@gmail.com" + } + ], + "autoload": { + "psr-0": {"Liuggio": "src/"} + }, + "require-dev": { + "monolog/monolog": ">=1.2.0" + }, + "suggest": { + "monolog/monolog": "Monolog, in order to do generate statistic from log >=1.2.0)" + } +} diff --git a/vendor/liuggio/statsd-php-client/phpunit.xml.dist b/vendor/liuggio/statsd-php-client/phpunit.xml.dist new file mode 100644 index 00000000..621efd75 --- /dev/null +++ b/vendor/liuggio/statsd-php-client/phpunit.xml.dist @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<phpunit bootstrap="tests/bootstrap.php" colors="true"> + <testsuites> + <testsuite name="Statsd-Service Test Suite"> + <directory>tests/Liuggio/StatsdClient</directory> + </testsuite> + </testsuites> + + <filter> + <whitelist> + <directory suffix=".php">src/Liuggio/</directory> + </whitelist> + </filter> +</phpunit>
\ No newline at end of file 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 new file mode 100644 index 00000000..9c6203db --- /dev/null +++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdData.php @@ -0,0 +1,78 @@ +<?php + +namespace Liuggio\StatsdClient\Entity; + +use Liuggio\StatsdClient\Entity\StatsdDataInterface; + +class StatsdData implements StatsdDataInterface +{ + + private $key; + private $value; + private $metric; + + /** + * @param string $key + */ + public function setKey($key) + { + $this->key = $key; + } + + /** + * @return string + */ + public function getKey() + { + return $this->key; + } + + /** + * @param int $value + */ + public function setValue($value) + { + $this->value = $value; + } + + /** + * @return int + */ + public function getValue() + { + return $this->value; + } + + + public function setMetric($metric) + { + $this->metric = $metric; + } + + public function getMetric() + { + return $this->metric; + } + + /** + * @param bool $withMetric + * + * @return string + */ + public function getMessage($withMetric = true) + { + if (!$withMetric) { + return sprintf('%s:%s', $this->getKey(), $this->getValue()); + } else { + return sprintf('%s:%s|%s', $this->getKey(), $this->getValue(), $this->getMetric()); + } + } + + /** + * @return string + */ + public function __toString() + { + return $this->getMessage(); + } +} 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 new file mode 100644 index 00000000..846bc7cc --- /dev/null +++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdDataInterface.php @@ -0,0 +1,41 @@ +<?php + +namespace Liuggio\StatsdClient\Entity; + +interface StatsdDataInterface +{ + CONST STATSD_METRIC_TIMING = 'ms'; + CONST STATSD_METRIC_GAUGE = 'g'; + CONST STATSD_METRIC_SET = 's'; + CONST STATSD_METRIC_COUNT = 'c'; + + /** + * @abstract + * @return string + */ + function getKey(); + + /** + * @abstract + * @return mixed + */ + function getValue(); + + /** + * @abstract + * @return string + */ + function getMetric(); + + /** + * @abstract + * @return string + */ + function getMessage(); + + /** + * @abstract + * @return string + */ + function __toString(); +} diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Exception/InvalidArgumentException.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Exception/InvalidArgumentException.php new file mode 100644 index 00000000..3f39d359 --- /dev/null +++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Exception/InvalidArgumentException.php @@ -0,0 +1,7 @@ +<?php + +namespace Liuggio\StatsdClient\Exception; + +class InvalidArgumentException extends \InvalidArgumentException +{ +} diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Factory/StatsdDataFactory.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Factory/StatsdDataFactory.php new file mode 100644 index 00000000..7ec1a19e --- /dev/null +++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Factory/StatsdDataFactory.php @@ -0,0 +1,130 @@ +<?php + +namespace Liuggio\StatsdClient\Factory; + +use Liuggio\StatsdClient\Entity\StatsdDataInterface; + +class StatsdDataFactory implements StatsdDataFactoryInterface +{ + /** + * @var StatsdDataInterface + */ + private $entityClass; + + public function __construct($entity_class = '\Liuggio\StatsdClient\Entity\StatsdData') + { + $this->setEntityClass($entity_class); + } + + /** + * {@inheritDoc} + **/ + public function timing($key, $time) + { + return $this->produceStatsdData($key, $time, StatsdDataInterface::STATSD_METRIC_TIMING); + } + + /** + * {@inheritDoc} + **/ + public function gauge($key, $value) + { + return $this->produceStatsdData($key, $value, StatsdDataInterface::STATSD_METRIC_GAUGE); + } + + /** + * {@inheritDoc} + **/ + public function set($key, $value) + { + return $this->produceStatsdData($key, $value, StatsdDataInterface::STATSD_METRIC_SET); + } + + /** + * {@inheritDoc} + **/ + public function increment($key) + { + return $this->produceStatsdData($key, 1, StatsdDataInterface::STATSD_METRIC_COUNT); + } + + /** + * {@inheritDoc} + **/ + public function decrement($key) + { + return $this->produceStatsdData($key, -1, StatsdDataInterface::STATSD_METRIC_COUNT); + } + + /** + * {@inheritDoc} + **/ + public function updateCount($key, $delta) + { + return $this->produceStatsdData($key, $delta, StatsdDataInterface::STATSD_METRIC_COUNT); + } + + /** + * {@inheritDoc} + **/ + public function produceStatsdData($key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT) + { + $statsdData = $this->produceStatsdDataEntity(); + + if (null !== $key) { + $statsdData->setKey($key); + } + + if (null !== $value) { + $statsdData->setValue($value); + } + + if (null !== $metric) { + $statsdData->setMetric($metric); + } + + return $statsdData; + } + + /** + * {@inheritDoc} + **/ + public function produceStatsdDataEntity() + { + $statsdData = $this->getEntityClass(); + + return new $statsdData(); + } + + /** + * {@inheritDoc} + **/ + public function setFailSilently($failSilently) + { + $this->failSilently = $failSilently; + } + + /** + * {@inheritDoc} + **/ + public function getFailSilently() + { + return $this->failSilently; + } + + /** + * {@inheritDoc} + **/ + public function setEntityClass($entityClass) + { + $this->entityClass = $entityClass; + } + + /** + * {@inheritDoc} + **/ + public function getEntityClass() + { + return $this->entityClass; + } +} diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Factory/StatsdDataFactoryInterface.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Factory/StatsdDataFactoryInterface.php new file mode 100644 index 00000000..4d58833b --- /dev/null +++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Factory/StatsdDataFactoryInterface.php @@ -0,0 +1,99 @@ +<?php + +namespace Liuggio\StatsdClient\Factory; + +use Liuggio\StatsdClient\Entity\StatsdDataInterface; + +Interface StatsdDataFactoryInterface +{ + + /** + * This function creates a 'timing' StatsdData. + * + * @abstract + * + * @param string|array $key The metric(s) to set. + * @param float $time The elapsed time (ms) to log + **/ + function timing($key, $time); + + /** + * This function creates a 'gauge' StatsdData. + * + * @abstract + * + * @param string|array $key The metric(s) to set. + * @param float $value The value for the stats. + **/ + function gauge($key, $value); + + /** + * This function creates a 'set' StatsdData object + * A "Set" is a count of unique events. + * This data type acts like a counter, but supports counting + * of unique occurrences of values between flushes. The backend + * receives the number of unique events that happened since + * the last flush. + * + * The reference use case involved tracking the number of active + * and logged in users by sending the current userId of a user + * with each request with a key of "uniques" (or similar). + * + * @abstract + * + * @param string|array $key The metric(s) to set. + * @param float $value The value for the stats. + * + * @return array + **/ + function set($key, $value); + + /** + * This function creates a 'increment' StatsdData object. + * + * @abstract + * + * @param string|array $key The metric(s) to increment. + * @param float|1 $sampleRate The rate (0-1) for sampling. + * + * @return array + **/ + function increment($key); + + /** + * This function creates a 'decrement' StatsdData object. + * + * @abstract + * + * @param string|array $key The metric(s) to decrement. + * @param float|1 $sampleRate The rate (0-1) for sampling. + * + * @return mixed + **/ + function decrement($key); + + /** + * This function creates a 'updateCount' StatsdData object. + * + * @abstract + * + * @param string|array $key The metric(s) to decrement. + * @param integer $delta The delta to add to the each metric + * + * @return mixed + **/ + function updateCount($key, $delta); + + /** + * Produce a StatsdDataInterface Object. + * + * @abstract + * + * @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 + **/ + function produceStatsdData($key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT); +} diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Formatter/StatsDFormatter.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Formatter/StatsDFormatter.php new file mode 100644 index 00000000..6311112a --- /dev/null +++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Formatter/StatsDFormatter.php @@ -0,0 +1,101 @@ +<?php + +namespace Liuggio\StatsdClient\Monolog\Formatter; + +use Monolog\Formatter\LineFormatter; + +/** + * Formats incoming records in order to be a perfect StatsD key. + * + * This is especially useful for logging to files + * + * @author Jordi Boggiano <j.boggiano@seld.be> + * @author Christophe Coevoet <stof@notk.org> + * @author Giulio De Donato <liuggio@gmail.com> + */ +class StatsDFormatter extends LineFormatter +{ + const SIMPLE_FORMAT = "%channel%.%level_name%.%short_message%"; + + protected $numberOfWords; + protected $logContext; + protected $logExtra; + + /** + * @param string $format The format of the message + * @param Boolean $logContext If true add multiple rows containing Context information + * @param Boolean $logExtra If true add multiple rows containing Extra information + * @param integer $numberOfWords The number of words to show. + */ + public function __construct($format = null, $logContext = true, $logExtra = true, $numberOfWords = 2) + { + $this->format = $format ? : static::SIMPLE_FORMAT; + $this->numberOfWords = $numberOfWords; + $this->logContext = $logContext; + $this->logExtra = $logExtra; + parent::__construct(); + } + + /** + * This function converts a long message into a string with the first N-words. + * eg. from: "Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener" + * to: "Notified event" + * + * @param string $message The message to shortify. + * + * @return string + */ + public function getFirstWords($message) + { + $glue = '-'; + $pieces = explode(' ', $message); + array_splice($pieces, $this->numberOfWords); + $shortMessage = preg_replace("/[^A-Za-z0-9?![:space:]]/", "-", implode($glue, $pieces)); + + return $shortMessage; + } + + /** + * {@inheritdoc} + */ + public function format(array $record) + { + $vars = $this->normalize($record); + + $firstRow = $this->format; + $output = array(); + + $vars['short_message'] = $this->getFirstWords($vars['message']); + foreach ($vars as $var => $val) { + $firstRow = str_replace('%' . $var . '%', $this->convertToString($val), $firstRow); + } + $output[] = $firstRow; + // creating more rows for context content + if ($this->logContext && isset($vars['context'])) { + foreach ($vars['context'] as $key => $parameter) { + $output[] = sprintf("%s.context.%s.%s", $firstRow, $key, $parameter); + } + } + // creating more rows for extra content + if ($this->logExtra && isset($vars['extra'])) { + foreach ($vars['extra'] as $key => $parameter) { + $output[] = sprintf("%s.extra.%s.%s", $firstRow, $key, $parameter); + } + } + + return $output; + } + + /** + * {@inheritdoc} + */ + public function formatBatch(array $records) + { + $output = array(); + foreach ($records as $record) { + $output = array_merge($output, $this->format($record)); + } + + return $output; + } +}
\ No newline at end of file diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Handler/StatsDHandler.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Handler/StatsDHandler.php new file mode 100644 index 00000000..86c6d399 --- /dev/null +++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Handler/StatsDHandler.php @@ -0,0 +1,120 @@ +<?php + +namespace Liuggio\StatsdClient\Monolog\Handler; + +use Monolog\Logger; +use Monolog\Handler\AbstractProcessingHandler; +use Monolog\Formatter\FormatterInterface; + +use Liuggio\StatsdClient\Monolog\Formatter\StatsDFormatter; +use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface; +use Liuggio\StatsdClient\Factory\StatsdDataFactory; +use Liuggio\StatsdClient\StatsdClientInterface; + +/** + * A processing handler for StatsD. + * + * @author Giulio De Donato <liuggio@gmail.com> + */ +class StatsDHandler extends AbstractProcessingHandler +{ + /** + * @var array + */ + protected $buffer = array(); + + /** + * @var string + */ + protected $prefix; + + /** + * @var statsDService + */ + protected $statsDService; + + /** + * @var statsDFactory + */ + protected $statsDFactory; + + /** + * @param StatsdClientInterface $statsDService The Service sends the packet + * @param StatsdDataFactoryInterface $statsDFactory The Factory creates the StatsDPacket + * @param string $prefix Statsd key prefix + * @param integer $level The minimum logging level at which this handler will be triggered + * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not + */ + public function __construct(StatsdClientInterface $statsDService, StatsdDataFactoryInterface $statsDFactory = null, $prefix, $level = Logger::DEBUG, $bubble = true) + { + parent::__construct($level, $bubble); + + $this->statsDService = $statsDService; + $this->statsDFactory = $statsDFactory ? $statsDFactory : new StatsdDataFactory(); + $this->prefix = $prefix; + } + + /** + * {@inheritdoc} + */ + public function close() + { + $this->statsDService->send($this->buffer); + } + + /** + * {@inheritdoc} + */ + protected function write(array $record) + { + $records = is_array($record['formatted']) ? $record['formatted'] : array($record['formatted']); + + foreach ($records as $record) { + if (!empty($record)) { + $this->buffer[] = $this->statsDFactory->increment(sprintf('%s.%s', $this->getPrefix(), $record)); + } + } + } + + /** + * Gets the default formatter. + * + * @return FormatterInterface + */ + protected function getDefaultFormatter() + { + return new StatsDFormatter(); + } + + /** + * @param string $prefix + */ + public function setPrefix($prefix) + { + $this->prefix = $prefix; + } + + /** + * @return string + */ + public function getPrefix() + { + return $this->prefix; + } + + /** + * @param StatsdClientInterface $statsDService + */ + public function setStatsDService(StatsdClientInterface $statsDService) + { + $this->statsDService = $statsDService; + } + + /** + * @param StatsdDataFactoryInterface $statsDFactory + */ + public function setStatsDFactory(StatsdDataFactoryInterface $statsDFactory) + { + $this->statsDFactory = $statsDFactory; + } +} diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/EchoSender.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/EchoSender.php new file mode 100644 index 00000000..1015ca94 --- /dev/null +++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/EchoSender.php @@ -0,0 +1,35 @@ +<?php + +namespace Liuggio\StatsdClient\Sender; + + +Class EchoSender implements SenderInterface +{ + /** + * {@inheritDoc} + */ + public function open() + { + echo "[open]"; + + return true; + } + + /** + * {@inheritDoc} + */ + function write($handle, $message, $length = null) + { + echo "[$message]"; + + return strlen($message); + } + + /** + * {@inheritDoc} + */ + function close($handle) + { + echo "[closed]"; + } +} diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SenderInterface.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SenderInterface.php new file mode 100644 index 00000000..8dc8fd3a --- /dev/null +++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SenderInterface.php @@ -0,0 +1,32 @@ +<?php + +namespace Liuggio\StatsdClient\Sender; + +Interface SenderInterface +{ + /** + * @abstract + * @return mixed + */ + function open(); + + /** + * @abstract + * + * @param $handle + * @param string $string + * @param null $length + * + * @return mixed + */ + function write($handle, $string, $length = null); + + /** + * @abstract + * + * @param $handle + * + * @return mixed + */ + function close($handle); +} diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SocketSender.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SocketSender.php new file mode 100644 index 00000000..c319f077 --- /dev/null +++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SocketSender.php @@ -0,0 +1,88 @@ +<?php + +namespace Liuggio\StatsdClient\Sender; + +use Liuggio\StatsdClient\Exception\InvalidArgumentException; + +Class SocketSender implements SenderInterface +{ + private $port; + private $host; + private $protocol; + + public function __construct($hostname = 'localhost', $port = 8126, $protocol = 'udp') + { + $this->host = $hostname; + $this->port = $port; + + switch ($protocol) { + case 'udp': + $this->protocol = SOL_UDP; + break; + case 'tcp': + $this->protocol = SOL_TCP; + break; + default: + throw new InvalidArgumentException(sprintf('Use udp or tcp as protocol given %s', $protocol)); + break; + } + + } + + /** + * {@inheritDoc} + */ + public function open() + { + $fp = socket_create(AF_INET, SOCK_DGRAM, $this->getProtocol()); + + return $fp; + } + + /** + * {@inheritDoc} + */ + public function write($handle, $message, $length = null) + { + return socket_sendto($handle, $message, strlen($message), 0, $this->getHost(), $this->getPort()); + } + + /** + * {@inheritDoc} + */ + public function close($handle) + { + socket_close($handle); + } + + + protected function setHost($host) + { + $this->host = $host; + } + + protected function getHost() + { + return $this->host; + } + + protected function setPort($port) + { + $this->port = $port; + } + + protected function getPort() + { + return $this->port; + } + + protected function setProtocol($protocol) + { + $this->protocol = $protocol; + } + + protected function getProtocol() + { + return $this->protocol; + } +} diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SysLogSender.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SysLogSender.php new file mode 100644 index 00000000..537ead39 --- /dev/null +++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SysLogSender.php @@ -0,0 +1,42 @@ +<?php + +namespace Liuggio\StatsdClient\Sender; + + +Class SysLogSender implements SenderInterface +{ + private $priority; + + public function __construct($priority = LOG_INFO) + { + $this->priority = $priority; + } + + /** + * {@inheritDoc} + */ + public function open() + { + syslog($this->priority, "statsd-client-open"); + + return true; + } + + /** + * {@inheritDoc} + */ + function write($handle, $message, $length = null) + { + syslog($this->priority, sprintf("statsd-client-write \"%s\" %d Bytes", $message, strlen($message))); + + return strlen($message); + } + + /** + * {@inheritDoc} + */ + function close($handle) + { + syslog($this->priority, "statsd-client-close"); + } +} diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/StatsdClient.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/StatsdClient.php new file mode 100644 index 00000000..a1d232a5 --- /dev/null +++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/StatsdClient.php @@ -0,0 +1,211 @@ +<?php + +namespace Liuggio\StatsdClient; + +use Liuggio\StatsdClient\Sender\SenderInterface; +use Liuggio\StatsdClient\Entity\StatsdDataInterface; +use Liuggio\StatsdClient\Exception\InvalidArgumentException; + +class StatsdClient implements StatsdClientInterface +{ + /** + * @var boolean + */ + private $failSilently; + + /** + * @var \Liuggio\StatsdClient\Sender\SenderInterface + */ + private $sender; + + /** + * @var boolean + */ + private $reducePacket; + + /** + * Constructor. + * + * @param \Liuggio\StatsdClient\Sender\SenderInterface $sender + * @param Boolean $reducePacket + * @param Boolean $fail_silently + */ + public function __construct(SenderInterface $sender, $reducePacket = true, $fail_silently = true) + { + $this->sender = $sender; + $this->reducePacket = $reducePacket; + $this->failSilently = $fail_silently; + } + + /** + * Throws an exc only if failSilently if getFailSilently is false. + * + * @param \Exception $exception + * + * @throws \Exception + */ + private function throwException(\Exception $exception) + { + if (!$this->getFailSilently()) { + throw $exception; + } + } + + /** + * This function reduces the number of packets,the reduced has the maximum dimension of self::MAX_UDP_SIZE_STR + * Reference: + * https://github.com/etsy/statsd/blob/master/README.md + * All metrics can also be batch send in a single UDP packet, separated by a newline character. + * + * @param array $reducedMetrics + * @param array $metric + * + * @return array + */ + private static function doReduce($reducedMetrics, $metric) + { + $metricLength = strlen($metric); + $lastReducedMetric = count($reducedMetrics) > 0 ? end($reducedMetrics) : null; + + if ($metricLength >= self::MAX_UDP_SIZE_STR + || null === $lastReducedMetric + || strlen($newMetric = $lastReducedMetric . "\n" . $metric) > self::MAX_UDP_SIZE_STR + ) { + $reducedMetrics[] = $metric; + } else { + array_pop($reducedMetrics); + $reducedMetrics[] = $newMetric; + } + + return $reducedMetrics; + } + + + /** + * this function reduce the amount of data that should be send + * + * @param mixed $arrayData + * + * @return mixed $arrayData + */ + public function reduceCount($arrayData) + { + if (is_array($arrayData)) { + $arrayData = array_reduce($arrayData, "self::doReduce", array()); + } + + return $arrayData; + } + + /** + * Reference: https://github.com/etsy/statsd/blob/master/README.md + * Sampling 0.1 + * Tells StatsD that this counter is being sent sampled every 1/10th of the time. + * + * @param mixed $data + * @param int $sampleRate + * + * @return mixed $data + */ + 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; + } + + return $data; + } + + /* + * Send the metrics over UDP + * + * {@inheritDoc} + */ + public function send($data, $sampleRate = 1) + { + // check format + if ($data instanceof StatsdDataInterface || is_string($data)) { + $data = array($data); + } + if (!is_array($data) || empty($data)) { + return; + } + // add sampling + if ($sampleRate < 1) { + $data = $this->appendSampleRate($data, $sampleRate); + } + // reduce number of packets + if ($this->getReducePacket()) { + $data = $this->reduceCount($data); + } + //failures in any of this should be silently ignored if .. + try { + $fp = $this->getSender()->open(); + if (!$fp) { + return; + } + $written = 0; + foreach ($data as $key => $message) { + $written += $this->getSender()->write($fp, $message); + } + $this->getSender()->close($fp); + } catch (\Exception $e) { + $this->throwException($e); + } + + return $written; + } + + /** + * @param boolean $failSilently + */ + public function setFailSilently($failSilently) + { + $this->failSilently = $failSilently; + } + + /** + * @return boolean + */ + public function getFailSilently() + { + return $this->failSilently; + } + + /** + * @param \Liuggio\StatsdClient\Sender\SenderInterface $sender + */ + public function setSender(SenderInterface $sender) + { + $this->sender = $sender; + } + + /** + * @return \Liuggio\StatsdClient\Sender\SenderInterface + */ + public function getSender() + { + return $this->sender; + } + + /** + * @param boolean $reducePacket + */ + public function setReducePacket($reducePacket) + { + $this->reducePacket = $reducePacket; + } + + /** + * @return boolean + */ + public function getReducePacket() + { + return $this->reducePacket; + } + +} diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/StatsdClientInterface.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/StatsdClientInterface.php new file mode 100644 index 00000000..e2c75cda --- /dev/null +++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/StatsdClientInterface.php @@ -0,0 +1,23 @@ +<?php + +namespace Liuggio\StatsdClient; + +use Liuggio\StatsdClient\Sender\SenderInterface; +use Liuggio\StatsdClient\Entity\StatsdDataInterface; +use Liuggio\StatsdClient\Exception\InvalidArgumentException; + +Interface StatsdClientInterface +{ + const MAX_UDP_SIZE_STR = 512; + + /* + * Send the metrics over UDP + * + * @abstract + * @param array|string|StatsdDataInterface $data message(s) to sent + * @param int $sampleRate Tells StatsD that this counter is being sent sampled every Xth of the time. + * + * @return integer the data sent in bytes + */ + function send($data, $sampleRate = 1); +} 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 new file mode 100644 index 00000000..fad9175c --- /dev/null +++ b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/Entity/StatsdDataTest.php @@ -0,0 +1,27 @@ +<?php + +namespace Liuggio\StatsdClient\Entity; + +use Liuggio\StatsdClient\Entity\StatsdData; + + +class StatsdDataTest extends \PHPUnit_Framework_TestCase +{ + 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'); + + } +} diff --git a/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/Monolog/Formatter/StatsDFormatterTest.php b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/Monolog/Formatter/StatsDFormatterTest.php new file mode 100644 index 00000000..daf899aa --- /dev/null +++ b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/Monolog/Formatter/StatsDFormatterTest.php @@ -0,0 +1,169 @@ +<?php + +namespace Liuggio\StatsdClient\Tests\Monolog\Formatter; + +use Monolog\Logger; +use Liuggio\StatsdClient\Monolog\Formatter\StatsDFormatter; + +/** + * @covers Liuggio\StatsdClient\Monolog\Formatter\StatsDFormatter + */ +class StatsDFormatterTest extends \PHPUnit_Framework_TestCase +{ + public function testBatchFormat() + { + $formatter = new StatsDFormatter(null, 2); + $message = $formatter->formatBatch(array( + array( + 'level_name' => 'CRITICAL', + 'channel' => 'test', + 'message' => 'bar', + 'context' => array(), + 'datetime' => new \DateTime, + 'extra' => array(), + ), + array( + 'level_name' => 'WARNING', + 'channel' => 'log', + 'message' => 'foo', + 'context' => array(), + 'datetime' => new \DateTime, + 'extra' => array(), + ), + )); + + $this->assertEquals(array('test.CRITICAL.bar', 'log.WARNING.foo'), $message); + } + + public function testDefFormatWithString() + { + $formatter = new StatsDFormatter(StatsDFormatter::SIMPLE_FORMAT); + $message = $formatter->format(array( + 'level_name' => 'WARNING', + 'channel' => 'log', + 'context' => array(), + 'message' => 'foo', + 'datetime' => new \DateTime, + 'extra' => array(), + )); + $this->assertEquals(array('log.WARNING.foo'), $message); + } + + public function testDefFormatWithArrayContext() + { + $formatter = new StatsDFormatter(); + $message = $formatter->format(array( + 'level_name' => 'ERROR', + 'channel' => 'meh', + 'message' => 'foo', + 'datetime' => new \DateTime, + 'extra' => array(), + 'context' => array( + 'foo' => 'bar', + 'baz' => 'qux', + ) + )); + + $assert = array('meh.ERROR.foo', + 'meh.ERROR.foo.context.foo.bar', + 'meh.ERROR.foo.context.baz.qux'); + + $this->assertEquals($assert, $message); + } + + public function testDefFormatWithArrayContextAndExtra() + { + $formatter = new StatsDFormatter(); + $message = $formatter->format(array( + 'level_name' => 'ERROR', + 'channel' => 'meh', + 'message' => 'foo', + 'datetime' => new \DateTime, + 'extra' => array('extra'=>'woow'), + 'context' => array( + 'foo' => 'bar', + 'baz' => 'qux', + ) + )); + + $assert = array('meh.ERROR.foo', + 'meh.ERROR.foo.context.foo.bar', + 'meh.ERROR.foo.context.baz.qux', + 'meh.ERROR.foo.extra.extra.woow'); + + $this->assertEquals($assert, $message); + + } + + public function testDefLongFormat() + { + $formatter = new StatsDFormatter(); + $message = $formatter->format(array( + 'level_name' => 'DEBUG', + 'channel' => 'doctrine', + 'message' => 'INSERT INTO viaggio_calendar (enable, viaggio_id, calendar_id) VALUES (?, ?, ?)', + 'datetime' => new \DateTime, + 'extra' => array(), + 'context' => array( + 'foo' => 'bar', + 'baz' => 'qux', + ) + )); + $this->assertEquals(array("doctrine.DEBUG.INSERT-INTO", + "doctrine.DEBUG.INSERT-INTO.context.foo.bar", + "doctrine.DEBUG.INSERT-INTO.context.baz.qux"), $message); + } + + public function testDefLongFormatWith3WordsNoContextAndNoExtra() + { + $formatter = new StatsDFormatter(null, false, false, 3); + $message = $formatter->format(array( + 'level_name' => 'DEBUG', + 'channel' => 'doctrine', + 'message' => 'INSERT INTO viaggio_calendar (enable, viaggio_id, calendar_id) VALUES (?, ?, ?)', + 'datetime' => new \DateTime, + 'extra' => array(), + 'context' => array( + 'foo' => 'bar', + 'baz' => 'qux', + ) + )); + $this->assertEquals(array("doctrine.DEBUG.INSERT-INTO-viaggio-calendar"), $message); + } + public function testDefRouteException() + { + $formatter = new StatsDFormatter(); + $message = $formatter->format(array( + 'level_name' => 'DEBUG', + 'channel' => 'doctrine', + 'message' => 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route found for "GET /ddd" (uncaught exception) at /xxxx/classes.php line 5062', + 'datetime' => new \DateTime, + 'extra' => array(), + )); + $this->assertEquals(array('doctrine.DEBUG.Symfony-Component-HttpKernel-Exception-NotFoundHttpException--No'), $message); + } + + public function testDefKernelException() + { + $formatter = new StatsDFormatter(); + $message = $formatter->format(array( + 'level_name' => 'DEBUG', + 'channel' => 'doctrine', + 'message' => 'Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelException"', + 'datetime' => new \DateTime, + 'extra' => array(), + 'context' => array( + 'foo' => 'bar', + 'baz' => 'qux', + ) + )); + + $assert = array('doctrine.DEBUG.Notified-event', + 'doctrine.DEBUG.Notified-event.context.foo.bar', + 'doctrine.DEBUG.Notified-event.context.baz.qux'); + + $this->assertEquals($assert, $message); + + + } +} diff --git a/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/Monolog/Handler/StatsDHandlerTest.php b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/Monolog/Handler/StatsDHandlerTest.php new file mode 100644 index 00000000..c1b8eb4b --- /dev/null +++ b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/Monolog/Handler/StatsDHandlerTest.php @@ -0,0 +1,89 @@ +<?php + +namespace Liuggio\StatsdClient\Tests\Monolog\Handler; + +use Monolog\Logger; +use Liuggio\StatsdClient\Monolog\Handler\StatsDHandler; + + +class StatsDHandlerTest extends \PHPUnit_Framework_TestCase +{ + /** + * @return array Record + */ + protected function getRecord($level = Logger::WARNING, $message = 'test', $context = array()) + { + return array( + 'message' => $message, + 'context' => $context, + 'level' => $level, + 'level_name' => Logger::getLevelName($level), + 'channel' => 'test', + 'datetime' => \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true))), + 'extra' => array(), + ); + } + + /** + * @return array + */ + protected function getMultipleRecords() + { + return array( + $this->getRecord(Logger::DEBUG, 'debug message 1'), + $this->getRecord(Logger::DEBUG, 'debug message 2'), + $this->getRecord(Logger::INFO, 'information'), + $this->getRecord(Logger::WARNING, 'warning'), + $this->getRecord(Logger::ERROR, 'error') + ); + } + + /** + * @return Monolog\Formatter\FormatterInterface + */ + protected function getIdentityFormatter() + { + $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface'); + $formatter->expects($this->any()) + ->method('format') + ->will($this->returnCallback(function($record) { return $record['message']; })); + + return $formatter; + } + + + protected function setup() + { + if (!interface_exists('Liuggio\StatsdClient\StatsdClientInterface')) { + $this->markTestSkipped('The "liuggio/statsd-php-client" package is not installed'); + } + } + + public function testHandle() + { + $client = $this->getMock('Liuggio\StatsdClient\StatsdClientInterface'); + $factory = $this->getMock('Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface'); + + $factory->expects($this->any()) + ->method('increment') + ->will($this->returnCallback(function ($input){ + return sprintf('%s|c|1', $input); + })); + + $prefixToAssert = 'prefix'; + $messageToAssert = 'test-msg'; + + $record = $this->getRecord(Logger::WARNING, $messageToAssert, array('data' => new \stdClass, 'foo' => 34)); + + $assert = array(sprintf('%s.test.WARNING.%s|c|1',$prefixToAssert, $messageToAssert), + sprintf('%s.test.WARNING.%s.context.data.[object] (stdClass: {})|c|1',$prefixToAssert, $messageToAssert), + sprintf('%s.test.WARNING.%s.context.foo.34|c|1',$prefixToAssert, $messageToAssert)); + + $client->expects($this->once()) + ->method('send') + ->with($assert); + + $handler = new StatsDHandler($client, $factory, $prefixToAssert); + $handler->handle($record); + } +}
\ No newline at end of file diff --git a/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/ReadmeTest.php b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/ReadmeTest.php new file mode 100644 index 00000000..4608a601 --- /dev/null +++ b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/ReadmeTest.php @@ -0,0 +1,72 @@ +<?php + +namespace Liuggio\StatsdClient; + +use Liuggio\StatsdClient\StatsdClient; +use Liuggio\StatsdClient\Factory\StatsdDataFactory; +//use Liuggio\StatsdClient\Sender\SocketSender; + + +class ReadmeTest extends \PHPUnit_Framework_TestCase +{ + public function testFullUsageWithObject() { + + $sender = $this->mockSender(); + // $sender = new Sender(); + + // StatsdClient(SenderInterface $sender, $host = 'udp://localhost', $port = 8126, $reducePacket = true, $fail_silently = true) + $client = new StatsdClient($sender); + $factory = new StatsdDataFactory('\Liuggio\StatsdClient\Entity\StatsdData'); + + // 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 as array or directly as object + $client->send($data); + } + + + + public function testFullUsageArray() { + + $sender = $this->mockSender(); + // $sender = new Sender(); + + // StatsdClient(SenderInterface $sender, $host = 'localhost', $port = 8126, $protocol='udp', $reducePacket = true, $fail_silently = true) + $client = new StatsdClient($sender, $host = 'localhost', $port = 8126, 'udp', $reducePacket = true, $fail_silently = true); + + $data[] ="increment:1|c"; + $data[] ="set:value|s"; + $data[] ="gauge:value|g"; + $data[] = "timing:10|ms"; + $data[] = "decrement:-1|c"; + $data[] ="key:1|c"; + + // send the data as array or directly as object + $client->send($data); + } + + + private function mockSender() { + $sender = $this->getMock('\Liuggio\StatsdClient\Sender\SenderInterface', array('open', 'write', 'close')); + $sender->expects($this->once()) + ->method('open') + ->will($this->returnValue(true)); + + $sender->expects($this->any()) //If you set the reduce = true into the StatsdClient the write will be called once + ->method('write') + ->will($this->returnCallBack(function($fp, $message) { + // echo PHP_EOL . "- " . $message; + })); + + $sender->expects($this->once()) + ->method('close') + ->will($this->returnValue(true)); + + return $sender; + } +}
\ No newline at end of file diff --git a/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/StatsdClientTest.php b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/StatsdClientTest.php new file mode 100644 index 00000000..865f1b2d --- /dev/null +++ b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/StatsdClientTest.php @@ -0,0 +1,228 @@ +<?php + +namespace Liuggio\StatsdClient; + +use Liuggio\StatsdClient\StatsdClient; +use Liuggio\StatsdClient\Entity\StatsdData; + +class StatsdClientTest extends \PHPUnit_Framework_TestCase +{ + + public function mockSenderWithAssertionOnWrite($messageToAssert=null) { + + $mock = $this->getMockBuilder('\Liuggio\StatsdClient\Sender\SocketSender') ->disableOriginalConstructor() ->getMock(); + + $phpUnit = $this; + $mock->expects($this->any()) + ->method('open') + ->will($this->returnValue(true)); + // if the input is an array expects a call foreach item + if (is_array($messageToAssert)) { + $index = 0; + foreach ($messageToAssert as $oneMessage) { + $index++; + $mock->expects($this->at($index)) + ->method('write') + ->will($this->returnCallBack(function($fp, $message) use ($phpUnit, $oneMessage) { + $phpUnit->assertEquals($message, $oneMessage); + })); + } + } else if (null !== $messageToAssert){ + // if the input is a string expects only once + $mock->expects($this->once()) + ->method('write') + ->will($this->returnCallBack(function($fp, $message) use ($phpUnit, $messageToAssert) { + $phpUnit->assertEquals($message, $messageToAssert); + })); + } + return $mock; + } + + public function mockStatsdClientWithAssertionOnWrite($messageToAssert) { + + $mockSender = $this->mockSenderWithAssertionOnWrite($messageToAssert); + + return new StatsdClient($mockSender, false, false); + } + + public function mockFactory() { + + $mock = $this->getMock('\Liuggio\StatsdClient\Factory\StatsdDataFactory', array('timing')); + + $statsData = new StatsdData(); + $statsData->setKey('key'); + $statsData->setValue('1'); + $statsData->setMetric('ms'); + + $phpUnit = $this; + $mock->expects($this->any()) + ->method('timing') + ->will($this->returnValue($statsData)); + + return $mock; + } + + public static function provider() + { + /** + * First + */ + $statsData0 = new StatsdData(); + $statsData0->setKey('keyTiming'); + $statsData0->setValue('1'); + $statsData0->setMetric('ms'); + /** + * Second + */ + $stats1 = array(); + $statsData1 = new StatsdData(); + $statsData1->setKey('keyTiming'); + $statsData1->setValue('1'); + $statsData1->setMetric('ms'); + $stats1[] = $statsData1; + + $statsData1 = new StatsdData(); + $statsData1->setKey('keyIncrement'); + $statsData1->setValue('1'); + $statsData1->setMetric('c'); + $stats1[] = $statsData1; + + return array( + array($statsData0, "keyTiming:1|ms"), + array($stats1, array("keyTiming:1|ms", "keyIncrement:1|c")), + ); + } + public static function providerSend() + { + return array( + array(array('gauge:value|g'), 'gauge:value|g'), + array(array("keyTiming:1|ms", "keyIncrement:1|c"), array("keyTiming:1|ms", "keyIncrement:1|c")), + ); + } + + /** + * @dataProvider provider + */ + public function testPrepareAndSend($statsdInput, $assertion) { + + $statsdMock = $this->mockStatsdClientWithAssertionOnWrite($assertion); + $statsdMock->send($statsdInput); + } + + /** + * @dataProvider providerSend + */ + public function testSend($array, $assertion) { + + $statsdMock = $this->mockStatsdClientWithAssertionOnWrite($assertion); + $statsdMock->send($array); + } + + 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); + + } + + public function testReduceWithString() + { + $statsd = $this->mockStatsdClientWithAssertionOnWrite(null); + + $msg = 'A3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789:'; + $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c'; + $array0[] = $msg; + + $msg = 'B3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789:'; + $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c'; + $array0[] = $msg; + $reduced = $statsd->reduceCount($array0); + $combined = $array0[0] . PHP_EOL . $array0[1]; + $this->assertEquals($combined, $reduced[0]); + } + + + public function testReduceWithMaxUdpPacketSplitInTwoPacket() + { + $statsd = $this->mockStatsdClientWithAssertionOnWrite(null); + + $msg = 'A3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789'; //1 + $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 '; //2 + $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 '; //3 + $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 '; //4 + $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c'; //500 + $array0[] = $msg; + + $msg = 'Bkey:'; + $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c'; + $array0[] = $msg; + + $reduced = $statsd->reduceCount($array0); + + $this->assertEquals($array0[0], $reduced[0]); + $this->assertEquals($array0[1], $reduced[1]); + } + + + + public function testMultiplePacketsWithReducing() + { + + $msg = 'A23456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789'; + $array0[] = $msg; + $array0[] = $msg; + $array0[] = $msg; + $array0[] = $msg; + $array0[] = $msg; + $array0[] = $msg; + $array0[] = $msg; + $array0[] = $msg; + + $total = count($array0) * strlen($msg); + + $reducedPacketsAssertion = (int) ceil($total / StatsdClientInterface::MAX_UDP_SIZE_STR); + + + $mockSender = $this->mockSenderWithAssertionOnWrite(); + $statsd = new StatsdClient($mockSender, true, false); + $reduced = $statsd->reduceCount($array0); + + $this->assertEquals($reducedPacketsAssertion, count($reduced)); + } + + public function testSampleRate() + { + $senderMock = $this->getMock('Liuggio\StatsdClient\Sender\SenderInterface'); + $senderMock + ->expects($this->once()) + ->method('open') + ->will($this->returnValue(true)) + ; + $senderMock + ->expects($this->once()) + ->method('write') + ->with($this->anything(), 'foo|@0.2') + ; + $client = new StatsdClient($senderMock, false, false); + + $client->send( + 'foo', + 0.2 + ); + } +} diff --git a/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/StatsdDataFactoryTest.php b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/StatsdDataFactoryTest.php new file mode 100644 index 00000000..144f629c --- /dev/null +++ b/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/StatsdDataFactoryTest.php @@ -0,0 +1,97 @@ +<?php + +namespace Liuggio\StatsdClient\Factory; + +use Liuggio\StatsdClient\Factory\StatsdDataFactory; + +class StatsDataFactoryTest extends \PHPUnit_Framework_TestCase +{ + private $statsDataFactory; + + public function setUp() + { + $this->statsDataFactory = new StatsdDataFactory('\Liuggio\StatsdClient\Entity\StatsdData'); + } + + public function testProduceStatsdData() + { + $key = 'key'; + $value='val'; + + $obj = $this->statsDataFactory->produceStatsdData($key, $value); + $this->assertEquals($key, $obj->getKey()); + $this->assertEquals($value, $obj->getValue()); + } + + public function testTiming() + { + $key = 'key'; + $value = microtime(); + $valueFloat = (string) floatval($value); + + $obj = $this->statsDataFactory->timing($key, $value); + $this->assertEquals($key, $obj->getKey()); + $this->assertContains($valueFloat, $obj->getValue()); + $this->assertContains('ms', $obj->getMetric()); + } + + public function testProduceStatsdDataDecrement() + { + $key = 'key'; + $value = -1; + $stringValue = intval($value); + + $obj = $this->statsDataFactory->produceStatsdData($key, $value); + $this->assertEquals($key, $obj->getKey()); + $this->assertEquals($stringValue, $obj->getValue()); + $this->assertEquals('c', $obj->getMetric()); + } + + public function testGauge() + { + $key = 'key'; + $value = 1000; + $stringValue = (string) intval($value); + + $obj = $this->statsDataFactory->gauge($key, $value); + $this->assertEquals($key, $obj->getKey()); + $this->assertEquals($stringValue, $obj->getValue()); + $this->assertEquals('g', $obj->getMetric()); + } + + public function testDecrement() + { + $key = 'key'; + $value = -1; + $stringValue = intval($value); + + $obj = $this->statsDataFactory->decrement($key); + $this->assertEquals($key, $obj->getKey()); + $this->assertEquals($stringValue, $obj->getValue()); + $this->assertEquals('c', $obj->getMetric()); + } + + public function testcreateStatsdDataIncrement() + { + $key = 'key'; + $value = 1; + $stringValue = intval($value); + + $obj = $this->statsDataFactory->increment($key); + $this->assertEquals($key, $obj->getKey()); + $this->assertEquals($stringValue, $obj->getValue()); + $this->assertEquals('c', $obj->getMetric()); + } + + public function testCreateStatsdDataUpdateCount() + { + $key = 'key'; + $value = 10; + $stringValue = intval($value); + + $obj = $this->statsDataFactory->updateCount($key, 10); + $this->assertEquals($key, $obj->getKey()); + $this->assertEquals($stringValue, $obj->getValue()); + $this->assertEquals('c', $obj->getMetric()); + } +} diff --git a/vendor/liuggio/statsd-php-client/tests/bootstrap.php b/vendor/liuggio/statsd-php-client/tests/bootstrap.php new file mode 100644 index 00000000..0594a467 --- /dev/null +++ b/vendor/liuggio/statsd-php-client/tests/bootstrap.php @@ -0,0 +1,5 @@ +<?php + + +$loader = require_once __DIR__ . "/../vendor/autoload.php"; +$loader->add('Liuggio\\', __DIR__);
\ No newline at end of file |