summaryrefslogtreecommitdiff
path: root/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Handler/StatsDHandler.php
blob: 86c6d399b324855ad4cc84003a2d4a33e8c51d6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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;
    }
}