-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathStatsdDataCollector.php
More file actions
127 lines (114 loc) · 3.27 KB
/
Copy pathStatsdDataCollector.php
File metadata and controls
127 lines (114 loc) · 3.27 KB
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
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
namespace M6Web\Bundle\StatsdBundle\DataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Contracts\EventDispatcher\Event;
/**
* Handle datacollector for statsd
*/
class StatsdDataCollector extends DataCollector
{
/** @var array */
private $statsdClients;
/**
* Construct the data collector
*/
public function __construct()
{
$this->reset();
}
/**
* Reset the data collector to initial state
*/
public function reset(): void
{
$this->statsdClients = [];
$this->data = [
'clients' => [],
'operations' => 0,
];
}
/**
* Kernel event
*
* @param Event $event The received event
*/
public function onKernelResponse($event): void
{
if ($event instanceof KernelEvent && HttpKernelInterface::MASTER_REQUEST == $event->getRequestType()) {
foreach ($this->statsdClients as $clientName => $client) {
$clientInfo = [
'name' => $clientName,
'operations' => [],
];
foreach ($client->getToSend() as $operation) {
if ($operation) {
$this->data['operations']++;
$message = $operation['message'];
$clientInfo['operations'][] = [
'server' => $operation['server'],
'node' => $message->getNode(),
'value' => $message->getValue(),
'sample' => $message->getSampleRate(),
'unit' => $message->getUnit(),
];
}
}
$this->data['clients'][] = $clientInfo;
}
}
}
/**
* Add a statsd client to monitor
*
* @param string $clientAlias The client alias
* @param object $statsdClient A statsd client instance
*/
public function addStatsdClient($clientAlias, $statsdClient): void
{
$this->statsdClients[$clientAlias] = $statsdClient;
}
/**
* Collect the data
*
* @param Request $request The request object
* @param Response $response The response object
* @param \Throwable|null $exception An exception
*
* @return void
*/
public function collect(Request $request, Response $response, \Throwable $exception = null)
{
}
/**
* Return the list of statsd operations
*
* @return array operations list
*/
public function getClients(): array
{
return $this->data['clients'];
}
/**
* Return the number of statsd operations
*
* @return int the number of operations
*/
public function getOperations()
{
return $this->data['operations'];
}
/**
* Return the name of the collector
*
* @return string data collector name
*/
public function getName()
{
return 'statsd';
}
}