|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Health monitoring for Yii2 applications |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/yii2-monitoring |
|
6
|
|
|
* @package yii2-monitoring |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\yii2\monitoring; |
|
12
|
|
|
|
|
13
|
|
|
use Yii; |
|
14
|
|
|
use yii\base\InvalidConfigException; |
|
15
|
|
|
use yii\helpers\VarDumper; |
|
16
|
|
|
use yii\log\Logger; |
|
17
|
|
|
|
|
18
|
|
|
class Module extends \yii\base\Module |
|
19
|
|
|
{ |
|
20
|
|
|
const FLAG_APPLICATION = 'app'; |
|
21
|
|
|
const FLAG_DOMAIN = 'domain'; |
|
22
|
|
|
|
|
23
|
|
|
public $error = []; |
|
24
|
|
|
|
|
25
|
|
|
public $feedback = []; |
|
26
|
|
|
|
|
27
|
|
|
public $targets; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string Flag that will be prepended to the message title |
|
31
|
|
|
* Special values that will be processed in a special way: |
|
32
|
|
|
* - `hiqdev\yii2\monitoring\Module::FLAG_APPLICATION` - will prefix `Yii::$app->id` |
|
33
|
|
|
* - `hiqdev\yii2\monitoring\Module::FLAG_DOMAIN` - will prefix `Yii::$app->request->getHostName()` |
|
34
|
|
|
*/ |
|
35
|
|
|
public $flag; |
|
36
|
|
|
|
|
37
|
|
|
public function getTarget($name) |
|
38
|
|
|
{ |
|
39
|
|
|
if (empty($this->targets[$name])) { |
|
40
|
|
|
throw new InvalidConfigException("no monitoring target $name defined"); |
|
41
|
|
|
} |
|
42
|
|
|
if (!is_object($this->targets[$name])) { |
|
43
|
|
|
$this->targets[$name] = Yii::createObject($this->targets[$name]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $this->targets[$name]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected $_flagText; |
|
50
|
|
|
|
|
51
|
|
|
public function getFlagText() |
|
52
|
|
|
{ |
|
53
|
|
|
if ($this->_flagText === null) { |
|
54
|
|
|
$this->_flagText = $this->detectFlagText($this->flag); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this->_flagText; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function detectFlagText($flag) |
|
61
|
|
|
{ |
|
62
|
|
|
switch ($flag) { |
|
63
|
|
|
case self::FLAG_APPLICATION: |
|
64
|
|
|
return Yii::$app->id; |
|
65
|
|
|
case self::FLAG_DOMAIN: |
|
66
|
|
|
return Yii::$app->request->getHostName(); |
|
67
|
|
|
default: |
|
68
|
|
|
return $flag; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function flagEmail($email) |
|
73
|
|
|
{ |
|
74
|
|
|
if (empty($this->flag)) { |
|
75
|
|
|
return $email; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
list($nick, $host) = explode('@', $email, 2); |
|
79
|
|
|
|
|
80
|
|
|
return $nick . '+' . $this->getFlagText() . '@' . $host; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function flagText($text, $delimiter = ' ') |
|
84
|
|
|
{ |
|
85
|
|
|
if (empty($this->flag)) { |
|
86
|
|
|
return $text; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return '[' . $this->getFlagText() . ']' . $delimiter . $text; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public static function getInstance() |
|
93
|
|
|
{ |
|
94
|
|
|
return Yii::$app->getModule('monitoring'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getDebugUrl($sessionTag = null) |
|
98
|
|
|
{ |
|
99
|
|
|
if (empty($sessionTag)) { |
|
100
|
|
|
$sessionTag = $this->getDebugTag(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return Yii::$app->getUrlManager()->createAbsoluteUrl([ |
|
104
|
|
|
'/debug/default/view', |
|
105
|
|
|
'panel' => 'log', |
|
106
|
|
|
'tag' => $sessionTag, |
|
107
|
|
|
]); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function getDebugTag() |
|
111
|
|
|
{ |
|
112
|
|
|
if (!Yii::$app->hasModule('debug')) { |
|
113
|
|
|
return null; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return Yii::$app->getModule('debug')->logTarget->tag; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function prepareMessageData($message) |
|
120
|
|
|
{ |
|
121
|
|
|
list($load, $level, $category, $timestamp) = $message; |
|
122
|
|
|
$dump = $load; |
|
123
|
|
|
$throwable = null; |
|
124
|
|
|
if (!is_string($load)) { |
|
125
|
|
|
// exceptions may not be serializable if in the call stack somewhere is a Closure |
|
126
|
|
|
if ($load instanceof \Throwable || $load instanceof \Exception) { |
|
127
|
|
|
$dump = (string) $load; |
|
128
|
|
|
$throwable = $load; |
|
129
|
|
|
} else { |
|
130
|
|
|
$dump = VarDumper::export($load); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
$traces = []; |
|
134
|
|
|
if (isset($message[4])) { |
|
135
|
|
|
foreach ($message[4] as $trace) { |
|
136
|
|
|
$traces[] = "in {$trace['file']}:{$trace['line']}"; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return [ |
|
141
|
|
|
'level' => Logger::getLevelName($level), |
|
142
|
|
|
'time' => date('c', $timestamp), |
|
143
|
|
|
'category' => $category, |
|
144
|
|
|
'traces' => $traces, |
|
145
|
|
|
'text' => $dump, |
|
146
|
|
|
'throwable' => $throwable, |
|
147
|
|
|
'debugUrl' => $this->getDebugUrl(), |
|
148
|
|
|
]; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function prepareSubject($message, $subject = null) |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->flagText($this->rawSubject($message, $subject)); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function rawSubject($message, $subject = null) |
|
157
|
|
|
{ |
|
158
|
|
|
if ($subject) { |
|
159
|
|
|
return $subject; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$load = reset($message); |
|
163
|
|
|
|
|
164
|
|
|
if (is_string($load)) { |
|
165
|
|
|
return $load; |
|
166
|
|
|
} elseif ($load instanceof \Throwable || $load instanceof \Exception) { |
|
167
|
|
|
return get_class($load) . ': ' . $load->getMessage(); |
|
168
|
|
|
} else { |
|
169
|
|
|
return VarDumper::export($load); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|