File tree Expand file tree Collapse file tree 2 files changed +68
-0
lines changed Expand file tree Collapse file tree 2 files changed +68
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Notifications ;
4+
5+ use Illuminate \Bus \Queueable ;
6+ use Illuminate \Contracts \Queue \ShouldQueue ;
7+ use Illuminate \Notifications \Notification ;
8+ use NotificationChannels \Telegram \TelegramMessage ;
9+
10+ class SlowQueryLogged extends Notification implements ShouldQueue
11+ {
12+ use Queueable;
13+
14+ public function __construct (private string $ query , private float |null $ duration , private string $ url )
15+ {
16+ }
17+
18+ public function via ($ notifiable )
19+ {
20+ if (
21+ ! empty (config ('services.telegram-bot-api.token ' )) &&
22+ ! empty (config ('services.telegram-bot-api.channel ' ))
23+ ) {
24+ return ['telegram ' ];
25+ }
26+
27+ return [];
28+ }
29+
30+ public function toTelegram ($ notifiable )
31+ {
32+ return TelegramMessage::create ()
33+ ->to (config ('services.telegram-bot-api.channel ' ))
34+ ->content ($ this ->content ());
35+ }
36+
37+ private function content (): string
38+ {
39+ $ content = "*Slow query logged!* \n\n" ;
40+ $ content .= "``` {$ this ->query }``` \n\n" ;
41+ $ content .= "Duration: {$ this ->duration }ms \n" ;
42+ $ content .= "URL: {$ this ->url }" ;
43+
44+ return $ content ;
45+ }
46+ }
Original file line number Diff line number Diff line change 66use App \Models \Reply ;
77use App \Models \Thread ;
88use App \Models \User ;
9+ use App \Notifications \SlowQueryLogged ;
10+ use Illuminate \Database \Connection ;
911use Illuminate \Database \Eloquent \Relations \Relation ;
12+ use Illuminate \Database \Events \QueryExecuted ;
13+ use Illuminate \Notifications \AnonymousNotifiable ;
14+ use Illuminate \Support \Facades \DB ;
15+ use Illuminate \Support \Facades \Notification ;
16+ use Illuminate \Support \Facades \Request ;
1017use Illuminate \Support \ServiceProvider ;
1118use Laravel \Horizon \Horizon ;
1219
@@ -17,6 +24,7 @@ public function boot()
1724 $ this ->bootEloquentMorphs ();
1825 $ this ->bootMacros ();
1926 $ this ->bootHorizon ();
27+ $ this ->bootSlowQueryLogging ();
2028 }
2129
2230 private function bootEloquentMorphs ()
@@ -43,4 +51,18 @@ public function bootHorizon()
4351 return auth ()->check () && auth ()->user ()->isAdmin ();
4452 });
4553 }
54+
55+ private function bootSlowQueryLogging ()
56+ {
57+ DB ::whenQueryingForLongerThan (500 , function (Connection $ connection , QueryExecuted $ event ) {
58+ Notification::send (
59+ new AnonymousNotifiable ,
60+ new SlowQueryLogged (
61+ $ event ->sql ,
62+ $ event ->time ,
63+ Request::url (),
64+ ),
65+ );
66+ });
67+ }
4668}
You can’t perform that action at this time.
0 commit comments