halilcosdu/laravel-slower
Detect and log slow Laravel database queries, then get AI-powered suggestions for indexes and query improvements. Configurable thresholds, can run with or without AI, and supports Laravel 10–13 on PHP 8.2+.
Enhanced Observability with Fingerprints:
The introduction of query fingerprints (parameterized SQL normalization) transforms raw query logging into a grouped, deduped view, reducing noise and enabling trend analysis. This aligns with modern observability stacks (e.g., OpenTelemetry) where semantic grouping of traces/metrics is critical. The Grouped toggle in the dashboard mirrors tools like Honeycomb or Datadog’s service maps, making it easier to triage performance clusters.
Origin Context for Root-Cause Analysis:
Capturing HTTP routes, queue jobs, or CLI origins + code backtraces (opt-in) bridges the gap between slow queries and their business context. This is a game-changer for debugging in microservices or monoliths with shared databases, where a slow User::where(...) might originate from CheckoutController@create or a SendWelcomeEmail job.
Production-Grade Safeguards:
capture.sample_rate): Mitigates storage bloat in high-volume apps (e.g., 1% sampling for 10K QPS → ~100 slow queries/day).AI-Augmented Workflow Improvements:
ai_payload config ensures only parameterized SQL + schema + origin are sent to the LLM, eliminating raw SQL/binding leaks. This is a critical fix for security-sensitive apps (e.g., financial systems).SlowQueryCaptured/SlowQueryFirstSeen events enable custom alerting (e.g., Slack pager for new slow queries) without modifying core logic.Queued Analysis for Scalability:
Backgrounding slower:analyze via Laravel Queues decouples analysis from request processing, reducing latency spikes during peak hours. This is essential for high-traffic apps (e.g., e-commerce during Black Friday).
Backward Compatibility:
New Dependency Risks:
slower_analysis_jobs table growth.Storage Overhead:
slower:fingerprint is idempotent and chunked, but may take hours for large slow_log tables (e.g., 1M+ rows). Schedule during off-peak hours.AI Payload Redaction:
PayloadRedactor now throws instead of leaking data. This requires upfront validation of the redactor’s shouldRedact() logic.| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Fingerprint Normalization Errors | Medium | False splits (query variants treated as identical) are documented as a trade-off. Use slower:fingerprint --dry-run to validate grouping logic. |
| Queued Analysis Failures | Medium | Jobs drop cleanly if records are pruned. Monitor failed_jobs table for retries. |
| Backtrace Privacy | High | Disable with capture.backtrace: false; audit DEBUG_BACKTRACE_IGNORE_ARGS for PII. |
| Storage Bloat | Low | Use capture.sample_rate and schedule slower:clean aggressively. |
| PHP 8.4 Upgrade Costs | Medium | Test in staging first; leverage Laravel’s upgrade guides. |
| Event Listener Failures | Low | Throwing listeners are non-blocking; circuit breaker ensures query logging continues. |
Observability Strategy:
api-gateway, order-service)? This requires customizing the OriginResolver.Privacy and Compliance:
capture.backtrace: true acceptable for your stack? If not, how will you map slow queries to business flows without code context?Operational Trade-offs:
slower:analyze run synchronously (default) or queued? Queued analysis reduces latency but adds queue infrastructure.capture.sample_rate) for production? Default (1.0) may be too high for high-volume endpoints.Integration with CI/CD:
SlowQueryFirstSeen events block deploys if critical queries exceed thresholds? Example:
event(new SlowQueryFirstSeen($query))
->then(fn() => throw new \RuntimeException("Critical query detected!"));
Cost vs. Insight:
ai_payload exclude certain schemas (e.g., analytics.*) to reduce token usage?Laravel Ecosystem:
SlowQueryCaptured/SlowQueryFirstSeen fit Laravel’s event ecosystem (e.g., Illuminate\Events\Dispatcher).debugbar and telescope.Database Compatibility:
EXPLAIN syntax adjustments.slow_query_fingerprints table requires indexes on fingerprint and connection for grouped queries. Test with your DB’s collation (e.g., utf8mb4_unicode_ci for MySQL).Third-Party Tools:
ai_payload redactor ensures compatibility with Laravel’s OpenAI SDK (v4+).slow_query_count_by_fingerprint) for Grafana dashboards.Pre-Migration Audit:
slow_log table size. For >100K rows, backfill fingerprints in chunks:
php artisan slower:fingerprint --chunk=1000
PayloadRedactor config:
'ai_payload' => [
'redactor' => \HalilCosdu\Slower\Redactors\DefaultRedactor::class,
'should_redact' => fn($query) => !str_starts_with($query->schema, 'public'),
],
Installation:
How can I help you explore Laravel packages today?