Файловый менеджер - Редактировать - /var/www/html/statsd-php-client.zip
Ðазад
PK ! �F��� � README.mdnu �Iw�� ## statsd-php-client [](http://travis-ci.org/liuggio/statsd-php-client) [](https://packagist.org/packages/liuggio/statsd-php-client) [](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 ### Install with composer ```bash composer require liuggio/statsd-php-client ``` ### Simple Usage ```php $statsd = new StatsdService(); $service->timing('usageTime', 100); $service->increment('visitor'); $service->decrement('click'); $service->gauge('gaugor', 333); $service->set('uniques', 765); $service->flush(); ``` ## 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`) ### Advanced Instantiation ```php use Liuggio\StatsdClient\StatsdClient, Liuggio\StatsdClient\Factory\StatsdDataFactory, 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); $factory = new StatsdDataFactory('\Liuggio\StatsdClient\Entity\StatsdData'); $service = new StatsdService($client, $factory); // create the metrics with the service $service->timing('usageTime', 100); //... // send the data to statsd $service->flush(); ``` ### 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 ``` ## Core developers: This project is actively mantained by David Moreau AKA @dav-m85 and @liuggio PK ! �$��w w ? src/Liuggio/StatsdClient/Exception/InvalidArgumentException.phpnu �Iw�� <?php namespace Liuggio\StatsdClient\Exception; class InvalidArgumentException extends \InvalidArgumentException { } PK ! �9�� � 0 src/Liuggio/StatsdClient/Sender/SocketSender.phpnu �Iw�� <?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; } } PK ! �=h(� � 3 src/Liuggio/StatsdClient/Sender/SenderInterface.phpnu �Iw�� <?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); } PK ! ��Z� � 0 src/Liuggio/StatsdClient/Sender/SysLogSender.phpnu �Iw�� <?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"); } } PK ! ͒�3� � . src/Liuggio/StatsdClient/Sender/EchoSender.phpnu �Iw�� <?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]"; } } PK ! =�� � ) src/Liuggio/StatsdClient/StatsdClient.phpnu �Iw�� <?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) { if ($sampleRate < 1) { array_walk($data, function(&$message, $key) use ($sampleRate) { $message = sprintf('%s|@%s', $message, $sampleRate); }); } 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; } } PK ! ��pi� � . src/Liuggio/StatsdClient/Entity/StatsdData.phpnu ��̗� <?php namespace Liuggio\StatsdClient\Entity; use Liuggio\StatsdClient\Entity\StatsdDataInterface; class StatsdData implements StatsdDataInterface { private $key; private $value; private $metric; private $sampleRate = 1; /** * @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 float $sampleRate */ public function setSampleRate($sampleRate) { $this->sampleRate = $sampleRate; } /** * @return float */ public function getSampleRate() { return $this->sampleRate; } /** * @param bool $withMetric * * @return string */ public function getMessage($withMetric = true) { if (!$withMetric) { $result = sprintf('%s:%s', $this->getKey(), $this->getValue()); } else { $result = sprintf('%s:%s|%s', $this->getKey(), $this->getValue(), $this->getMetric()); } $sampleRate = $this->getSampleRate(); if($sampleRate < 1){ $result.= "|@$sampleRate"; } return $result; } /** * @return string */ public function __toString() { return $this->getMessage(); } } PK ! ���� � 7 src/Liuggio/StatsdClient/Entity/StatsdDataInterface.phpnu ��̗� <?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 float */ function getSampleRate(); /** * @abstract * @return string */ function __toString(); } PK ! �<�� � ? src/Liuggio/StatsdClient/Factory/StatsdDataFactoryInterface.phpnu �Iw�� <?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); } PK ! @0�מ � 6 src/Liuggio/StatsdClient/Factory/StatsdDataFactory.phpnu �Iw�� <?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; } } PK ! f>t�7 7 >