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