]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Handler/StatsDHandler.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / vendor / liuggio / statsd-php-client / src / Liuggio / StatsdClient / Monolog / Handler / StatsDHandler.php
1 <?php
2
3 namespace Liuggio\StatsdClient\Monolog\Handler;
4
5 use Monolog\Logger;
6 use Monolog\Handler\AbstractProcessingHandler;
7 use Monolog\Formatter\FormatterInterface;
8
9 use Liuggio\StatsdClient\Monolog\Formatter\StatsDFormatter;
10 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
11 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
12 use Liuggio\StatsdClient\StatsdClientInterface;
13
14 /**
15  * A processing handler for StatsD.
16  *
17  * @author Giulio De Donato <liuggio@gmail.com>
18  */
19 class StatsDHandler extends AbstractProcessingHandler
20 {
21     /**
22      * @var array
23      */
24     protected $buffer = array();
25
26     /**
27      * @var string
28      */
29     protected $prefix;
30
31     /**
32      * @var statsDService
33      */
34     protected $statsDService;
35
36     /**
37      * @var statsDFactory
38      */
39     protected $statsDFactory;
40
41     /**
42      * @param StatsdClientInterface $statsDService The Service sends the packet
43      * @param StatsdDataFactoryInterface $statsDFactory The Factory creates the StatsDPacket
44      * @param string $prefix Statsd key prefix
45      * @param integer $level The minimum logging level at which this handler will be triggered
46      * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
47      */
48     public function __construct(StatsdClientInterface $statsDService, StatsdDataFactoryInterface $statsDFactory = null, $prefix, $level = Logger::DEBUG, $bubble = true)
49     {
50         parent::__construct($level, $bubble);
51
52         $this->statsDService = $statsDService;
53         $this->statsDFactory = $statsDFactory ? $statsDFactory : new StatsdDataFactory();
54         $this->prefix = $prefix;
55     }
56
57     /**
58      * {@inheritdoc}
59      */
60     public function close()
61     {
62         $this->statsDService->send($this->buffer);
63     }
64
65     /**
66      * {@inheritdoc}
67      */
68     protected function write(array $record)
69     {
70         $records = is_array($record['formatted']) ? $record['formatted'] : array($record['formatted']);
71
72         foreach ($records as $record) {
73             if (!empty($record)) {
74                 $this->buffer[] = $this->statsDFactory->increment(sprintf('%s.%s', $this->getPrefix(), $record));
75             }
76         }
77     }
78
79     /**
80      * Gets the default formatter.
81      *
82      * @return FormatterInterface
83      */
84     protected function getDefaultFormatter()
85     {
86         return new StatsDFormatter();
87     }
88
89     /**
90      * @param string $prefix
91      */
92     public function setPrefix($prefix)
93     {
94         $this->prefix = $prefix;
95     }
96
97     /**
98      * @return string
99      */
100     public function getPrefix()
101     {
102         return $this->prefix;
103     }
104
105     /**
106      * @param StatsdClientInterface $statsDService
107      */
108     public function setStatsDService(StatsdClientInterface $statsDService)
109     {
110         $this->statsDService = $statsDService;
111     }
112
113     /**
114      * @param StatsdDataFactoryInterface $statsDFactory
115      */
116     public function setStatsDFactory(StatsdDataFactoryInterface $statsDFactory)
117     {
118         $this->statsDFactory = $statsDFactory;
119     }
120 }