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