From f6d65e533c62f6deb21342d4901ece24497b433e Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Thu, 4 Jun 2015 07:31:04 +0200 Subject: Update to MediaWiki 1.25.1 --- .../src/Liuggio/StatsdClient/Entity/StatsdData.php | 78 ++++++++ .../StatsdClient/Entity/StatsdDataInterface.php | 41 ++++ .../Exception/InvalidArgumentException.php | 7 + .../StatsdClient/Factory/StatsdDataFactory.php | 130 +++++++++++++ .../Factory/StatsdDataFactoryInterface.php | 99 ++++++++++ .../Monolog/Formatter/StatsDFormatter.php | 101 ++++++++++ .../StatsdClient/Monolog/Handler/StatsDHandler.php | 120 ++++++++++++ .../src/Liuggio/StatsdClient/Sender/EchoSender.php | 35 ++++ .../StatsdClient/Sender/SenderInterface.php | 32 ++++ .../Liuggio/StatsdClient/Sender/SocketSender.php | 88 +++++++++ .../Liuggio/StatsdClient/Sender/SysLogSender.php | 42 ++++ .../src/Liuggio/StatsdClient/StatsdClient.php | 211 +++++++++++++++++++++ .../Liuggio/StatsdClient/StatsdClientInterface.php | 23 +++ 13 files changed, 1007 insertions(+) create mode 100644 vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdData.php create mode 100644 vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdDataInterface.php create mode 100644 vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Exception/InvalidArgumentException.php create mode 100644 vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Factory/StatsdDataFactory.php create mode 100644 vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Factory/StatsdDataFactoryInterface.php create mode 100644 vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Formatter/StatsDFormatter.php create mode 100644 vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Handler/StatsDHandler.php create mode 100644 vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/EchoSender.php create mode 100644 vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SenderInterface.php create mode 100644 vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SocketSender.php create mode 100644 vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SysLogSender.php create mode 100644 vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/StatsdClient.php create mode 100644 vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/StatsdClientInterface.php (limited to 'vendor/liuggio/statsd-php-client/src/Liuggio') diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdData.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Entity/StatsdData.php 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 @@ +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 @@ +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 @@ + + * @author Christophe Coevoet + * @author Giulio De Donato + */ +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 @@ + + */ +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 @@ +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 @@ +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 @@ +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 @@ +