summaryrefslogtreecommitdiff
path: root/vendor/liuggio/statsd-php-client/src/Liuggio
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/liuggio/statsd-php-client/src/Liuggio')
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdData.php78
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdDataInterface.php41
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Exception/InvalidArgumentException.php7
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Factory/StatsdDataFactory.php130
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Factory/StatsdDataFactoryInterface.php99
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Formatter/StatsDFormatter.php101
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Handler/StatsDHandler.php120
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/EchoSender.php35
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SenderInterface.php32
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SocketSender.php88
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SysLogSender.php42
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/StatsdClient.php211
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/StatsdClientInterface.php23
13 files changed, 1007 insertions, 0 deletions
diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdData.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdData.php
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);
+}