1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EightPoints\Bundle\GuzzleBundle\DataCollector; |
4
|
|
|
|
5
|
|
|
use EightPoints\Bundle\GuzzleBundle\Log\LogGroup; |
6
|
|
|
use EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface; |
7
|
|
|
use EightPoints\Bundle\GuzzleBundle\Log\LogMessage; |
8
|
|
|
use Psr\Log\LogLevel; |
9
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Collecting http data for Symfony profiler |
15
|
|
|
*/ |
16
|
|
|
class HttpDataCollector extends DataCollector |
17
|
|
|
{ |
18
|
|
|
use DataCollectorSymfonyCompatibilityTrait; |
19
|
|
|
|
20
|
|
|
/** @var \EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface */ |
21
|
|
|
protected $logger; |
22
|
|
|
|
23
|
|
|
/** @var float */ |
24
|
|
|
private $slowResponseTime; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param \EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface $logger |
28
|
|
|
* @param float|int $slowResponseTime Time in seconds |
29
|
|
|
*/ |
30
|
|
|
public function __construct(LoggerInterface $logger, float $slowResponseTime) |
31
|
|
|
{ |
32
|
|
|
$this->logger = $logger; |
33
|
|
|
$this->slowResponseTime = $slowResponseTime; |
34
|
|
|
|
35
|
|
|
$this->reset(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
protected function doCollect(Request $request, Response $response, \Throwable $exception = null) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$messages = $this->logger->getMessages(); |
44
|
|
|
|
45
|
|
|
if ($this->slowResponseTime > 0) { |
46
|
|
|
foreach ($messages as $message) { |
47
|
|
|
if (!$message instanceof LogMessage) { |
48
|
|
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($message->getTransferTime() >= $this->slowResponseTime) { |
52
|
|
|
$this->data['hasSlowResponse'] = true; |
53
|
|
|
break; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$requestId = $request->getUri(); |
59
|
|
|
|
60
|
|
|
// clear log to have only messages related to Symfony request context |
61
|
|
|
$this->logger->clear(); |
62
|
|
|
|
63
|
|
|
$logGroup = $this->getLogGroup($requestId); |
64
|
|
|
$logGroup->setRequestName($request->getPathInfo()); |
65
|
|
|
$logGroup->addMessages($messages); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function getName() : string |
72
|
|
|
{ |
73
|
|
|
return 'eight_points_guzzle'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Resets this data collector to its initial state. |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function reset() : void |
82
|
|
|
{ |
83
|
|
|
$this->data = [ |
84
|
|
|
'logs' => [], |
85
|
|
|
'callCount' => 0, |
86
|
|
|
'totalTime' => 0, |
87
|
|
|
'hasSlowResponse' => false, |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returning log entries |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function getLogs() : array |
97
|
|
|
{ |
98
|
|
|
return array_key_exists('logs', $this->data) ? $this->data['logs'] : []; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get all messages |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function getMessages() : array |
107
|
|
|
{ |
108
|
|
|
$messages = []; |
109
|
|
|
|
110
|
|
|
foreach ($this->getLogs() as $log) { |
111
|
|
|
foreach ($log->getMessages() as $message) { |
112
|
|
|
$messages[] = $message; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $messages; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Return amount of http calls |
121
|
|
|
* |
122
|
|
|
* @return integer |
123
|
|
|
*/ |
124
|
|
|
public function getCallCount() : int |
125
|
|
|
{ |
126
|
|
|
return count($this->getMessages()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get Error Count |
131
|
|
|
* |
132
|
|
|
* @return integer |
133
|
|
|
*/ |
134
|
|
|
public function getErrorCount() : int |
135
|
|
|
{ |
136
|
|
|
return count($this->getErrorsByType(LogLevel::ERROR)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $type |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
public function getErrorsByType(string $type) : array |
145
|
|
|
{ |
146
|
|
|
return array_filter( |
147
|
|
|
$this->getMessages(), |
148
|
|
|
function (LogMessage $message) use ($type) { |
149
|
|
|
return $message->getLevel() === $type; |
150
|
|
|
} |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get total time of all requests |
156
|
|
|
* |
157
|
|
|
* @return float |
158
|
|
|
*/ |
159
|
|
|
public function getTotalTime() : float |
160
|
|
|
{ |
161
|
|
|
return $this->data['totalTime']; |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Check if there were any slow responses |
166
|
|
|
* |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
public function hasSlowResponses() : bool |
170
|
|
|
{ |
171
|
|
|
return $this->data['hasSlowResponse']; |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param float $time |
176
|
|
|
* |
177
|
|
|
* @return void |
178
|
|
|
*/ |
179
|
|
|
public function addTotalTime(float $time) : void |
180
|
|
|
{ |
181
|
|
|
$this->data['totalTime'] += $time; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Returns (new) LogGroup based on given id |
186
|
|
|
* |
187
|
|
|
* @param string $id |
188
|
|
|
* |
189
|
|
|
* @return \EightPoints\Bundle\GuzzleBundle\Log\LogGroup |
190
|
|
|
*/ |
191
|
|
|
protected function getLogGroup(string $id) : LogGroup |
192
|
|
|
{ |
193
|
|
|
if (!isset($this->data['logs'][$id])) { |
194
|
|
|
$this->data['logs'][$id] = new LogGroup(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $this->data['logs'][$id]; |
|
|
|
|
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.