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