Skip to content

Commit 13a3c2c

Browse files
joedixonStyleCIBot
andauthored
Send a notification on slow query (#918)
* Send a notification on slow query * Apply fixes from StyleCI * Refactor to method * Apply fixes from StyleCI Co-authored-by: StyleCI Bot <[email protected]>
1 parent 5d6b951 commit 13a3c2c

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}

app/Providers/AppServiceProvider.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
use App\Models\Reply;
77
use App\Models\Thread;
88
use App\Models\User;
9+
use App\Notifications\SlowQueryLogged;
10+
use Illuminate\Database\Connection;
911
use 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;
1017
use Illuminate\Support\ServiceProvider;
1118
use 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
}

0 commit comments

Comments
 (0)