|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the tarantool/client package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace App; |
|
15
|
|
|
|
|
16
|
|
|
use Monolog\Handler\HandlerInterface; |
|
17
|
|
|
use Monolog\Handler\HandlerWrapper; |
|
18
|
|
|
use Monolog\Logger; |
|
19
|
|
|
use Tarantool\Client\RequestTypes; |
|
20
|
|
|
|
|
21
|
|
|
final class SlowRequestHandler extends HandlerWrapper |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var int */ |
|
24
|
|
|
private $thresholdMs; |
|
25
|
|
|
|
|
26
|
|
|
/** @var int */ |
|
27
|
|
|
private $level; |
|
28
|
|
|
|
|
29
|
|
|
/** @var string */ |
|
30
|
|
|
private $levelName; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param positive-int $thresholdMs |
|
34
|
|
|
* @param Logger::DEBUG|Logger::INFO|Logger::NOTICE|Logger::WARNING|Logger::ERROR|Logger::CRITICAL|Logger::ALERT|Logger::EMERGENCY $level |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(HandlerInterface $handler, int $thresholdMs, int $level = Logger::WARNING) |
|
37
|
|
|
{ |
|
38
|
|
|
parent::__construct($handler); |
|
39
|
|
|
|
|
40
|
|
|
$this->thresholdMs = $thresholdMs; |
|
41
|
|
|
$this->level = $level; |
|
42
|
|
|
$this->levelName = Logger::getLevelName($this->level); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function isHandling(array $record) : bool |
|
46
|
|
|
{ |
|
47
|
|
|
// Handle all levels |
|
48
|
|
|
return true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function handle(array $record) : bool |
|
52
|
|
|
{ |
|
53
|
|
|
if (!isset($record['context']['duration_ms'], $record['context']['request'])) { |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($record['context']['duration_ms'] <= $this->thresholdMs) { |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$request = $record['context']['request']; |
|
62
|
|
|
|
|
63
|
|
|
return $this->handler->handle([ |
|
64
|
|
|
'level' => $this->level, |
|
65
|
|
|
'level_name' => $this->levelName, |
|
66
|
|
|
'message' => sprintf('Slow %s request detected (%d ms)', RequestTypes::getName($request->getType()), $record['context']['duration_ms']), |
|
67
|
|
|
'context' => ['request_body' => $request->getBody()], |
|
68
|
|
|
] + $record); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|