-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.1.0 - 2024-12-10 ADD Total Number of Queries: track the total numbe…
…r of SQL queries executed during a single HTTP request, Artisan command, or CLI execution.
- Loading branch information
Showing
8 changed files
with
637 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Padosoft\QueryMonitor\Middleware; | ||
|
||
use Closure; | ||
use Padosoft\QueryMonitor\QueryCounter; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class TrackTotalQueriesMiddleware | ||
{ | ||
protected function logExcessiveQueries(int $count, int $max, array $context): void | ||
{ | ||
$message = "Exceeded maximum total queries: {$count} queries (max: {$max})."; | ||
$extra = [ | ||
'context' => 'request', | ||
'url' => $context['url'] ?? 'unknown', | ||
'method' => $context['method'] ?? 'unknown', | ||
]; | ||
|
||
Log::warning($message, $extra); | ||
} | ||
|
||
public function handle($request, Closure $next) | ||
{ | ||
$totalQueriesConfig = Config::get('querymonitor.total_queries', []); | ||
|
||
if (empty($totalQueriesConfig['attiva']) || $totalQueriesConfig['attiva'] === false) { | ||
return $next($request); | ||
} | ||
|
||
$traceRegEx = $totalQueriesConfig['traceRegEx'] ?? null; | ||
|
||
$url = $request->fullUrl(); | ||
|
||
// Se c'è una regex e la url non match, non tracciamo questa request | ||
if ($traceRegEx && !preg_match("/{$traceRegEx}/", $url)) { | ||
// Non attiviamo il tracking, quindi niente reset | ||
return $next($request); | ||
} | ||
|
||
QueryCounter::reset([ | ||
'type' => 'request', | ||
'url' => $url, | ||
'method' => $request->method(), | ||
]); | ||
|
||
return $next($request); | ||
} | ||
|
||
public function terminate($request, $response) | ||
{ | ||
$totalQueriesConfig = Config::get('querymonitor.total_queries', []); | ||
|
||
if (empty($totalQueriesConfig['attiva']) || $totalQueriesConfig['attiva'] === false) { | ||
// Il tracking non è attivo, non facciamo nulla | ||
return; | ||
} | ||
|
||
// Controlla se il tracking è stato attivato controllando se getCount > 0 o se QueryCounter::getContextInfo() non è vuoto | ||
$context = QueryCounter::getContextInfo(); | ||
if (empty($context)) { | ||
// Non è stato attivato nessun tracking per questa request | ||
return; | ||
} | ||
|
||
$count = QueryCounter::getCount(); | ||
$max = $totalQueriesConfig['maxTotalQueries'] ?? 500; | ||
|
||
if ($count <= $max) { | ||
// Non superiamo il limite, non facciamo nulla | ||
return; | ||
} | ||
|
||
$this->logExcessiveQueries($count, $max, $context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Padosoft\QueryMonitor; | ||
|
||
class QueryCounter | ||
{ | ||
protected static int $queryCount = 0; | ||
protected static array $contextInfo = []; | ||
|
||
/** | ||
* Resetta il contatore query e memorizza il contesto (es: request info, command info) | ||
*/ | ||
public static function reset(array $contextInfo = []): void | ||
{ | ||
self::$queryCount = 0; | ||
self::$contextInfo = $contextInfo; | ||
} | ||
|
||
/** | ||
* Incrementa il contatore delle query | ||
*/ | ||
public static function increment(): void | ||
{ | ||
self::$queryCount++; | ||
} | ||
|
||
/** | ||
* Restituisce il numero totale di query eseguite | ||
*/ | ||
public static function getCount(): int | ||
{ | ||
return self::$queryCount; | ||
} | ||
|
||
/** | ||
* Restituisce le info di contesto salvate (url, metodo, command, ecc.) | ||
*/ | ||
public static function getContextInfo(): array | ||
{ | ||
return self::$contextInfo; | ||
} | ||
} |
Oops, something went wrong.