Skip to content

Commit 0ccb17c

Browse files
committed
Initial commit
0 parents  commit 0ccb17c

File tree

6 files changed

+168
-0
lines changed

6 files changed

+168
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Laravel Slow Query Detector
2+
3+
## Requirements
4+
* Laravel 5.5+
5+
6+
## Installation
7+
Install the package
8+
```bash
9+
composer require ilzrv/laravel-slow-query-detector
10+
```
11+
12+
Publish the configuration file
13+
```bash
14+
php artisan vendor:publish --provider="Ilzrv\LaravelSlowQueryDetector\ServiceProvider"
15+
```

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "ilzrv/laravel-slow-query-detector",
3+
"description": "Laravel Slow DB Query Detector",
4+
"require": {
5+
"laravel/framework": "^5.5|^6|^7"
6+
},
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Ilia Lazarev",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"autoload": {
15+
"psr-4": {
16+
"Ilzrv\\LaravelSlowQueryDetector\\": "src"
17+
}
18+
},
19+
"extra": {
20+
"laravel": {
21+
"providers": [
22+
"Ilzrv\\LaravelSlowQueryDetector\\ServiceProvider"
23+
]
24+
}
25+
}
26+
}

config/sqd.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
return [
4+
5+
'enabled' => (bool) env('SQD_ENABLED', true),
6+
7+
'code' => [
8+
'max_queries' => (int) env('SQD_CODE_MAX_QUERIES', 50),
9+
'max_time' => (int) env('SQD_CODE_MAX_TIME', 1000),
10+
],
11+
12+
'query' => [
13+
'with_bindings' => (bool) env('SQD_QUERY_BINDINGS', true),
14+
'max_time' => (int) env('SQD_QUERY_MAX_TIME', 50),
15+
],
16+
17+
];
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Ilzrv\LaravelSlowQueryDetector\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Support\Str;
7+
8+
class SlowQueryDetectorMiddleware
9+
{
10+
protected $queriesCount = 0;
11+
protected $heavyQueryCount = 0;
12+
protected $heaviestQueryTime = 0;
13+
protected $heaviestQuery = '';
14+
protected $executionTime = 0;
15+
16+
/**
17+
* Handle an incoming request.
18+
*
19+
* @param \Illuminate\Http\Request $request
20+
* @param \Closure $next
21+
* @return mixed
22+
*/
23+
public function handle($request, Closure $next)
24+
{
25+
$this->executionTime = -round(microtime(true) * 1000);
26+
27+
\DB::listen(function ($query) {
28+
$this->queriesCount++;
29+
if ($query->time > config('slow-query-detector.query.max_time')) {
30+
$this->heavyQueryCount++;
31+
if ($query->time > $this->heaviestQueryTime) {
32+
$this->heaviestQueryTime = $query->time;
33+
$this->heaviestQuery = $this->getQuery($query);
34+
}
35+
}
36+
});
37+
38+
$next = $next($request);
39+
40+
$this->executionTime += round(microtime(true) * 1000);
41+
42+
if ($this->needNotify()) {
43+
$this->notify($request);
44+
}
45+
46+
return $next;
47+
}
48+
49+
protected function needNotify()
50+
{
51+
return $this->queriesCount > config('slow-query-detector.code.max_queries')
52+
|| $this->executionTime > config('slow-query-detector.code.max_time')
53+
|| $this->heaviestQueryTime > config('slow-query-detector.query.max_time');
54+
}
55+
56+
protected function notify($request)
57+
{
58+
app('log')->critical(print_r([
59+
'Execution Time' => $this->executionTime,
60+
'Queries Count' => $this->queriesCount,
61+
'Heavy Queries Count' => $this->heavyQueryCount,
62+
'Full URL' => $request->fullUrl(),
63+
'Action' => $request->route()->getActionName(),
64+
'Heaviest Query' => [
65+
'Query' => $this->heaviestQuery,
66+
'Time' => $this->heaviestQueryTime,
67+
]
68+
], true));
69+
}
70+
71+
protected function getQuery($query)
72+
{
73+
return config('slow-query-detector.query.with_bindings')
74+
? Str::replaceArray('?', $query->bindings, $query->sql)
75+
: $query->sql;
76+
}
77+
}

src/ServiceProvider.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Ilzrv\LaravelSlowQueryDetector;
4+
5+
use Ilzrv\LaravelSlowQueryDetector\Http\Middleware\SlowQueryDetectorMiddleware;
6+
7+
class ServiceProvider extends \Illuminate\Support\ServiceProvider
8+
{
9+
/**
10+
* Register services.
11+
*
12+
* @return void
13+
*/
14+
public function register()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Bootstrap services.
21+
*
22+
* @return void
23+
*/
24+
public function boot()
25+
{
26+
$this->publishes([__DIR__ . '/../config/sqd.php' => config_path('sqd.php')]);
27+
28+
if (config('sqd.enabled')) {
29+
$this->app['router']->pushMiddlewareToGroup('web', SlowQueryDetectorMiddleware::class);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)