rylxes/laravel-observability
Composer 安装命令:
composer require rylxes/laravel-observability
包简介
Production-grade observability and APM plugin for Laravel applications with OpenTelemetry, Prometheus, and AI-powered anomaly detection
关键字:
README 文档
README
Full Documentation — Complete usage guide, configuration reference, and API docs.
🔍 Production-Grade Observability & APM for Laravel Applications
A lightweight, database-agnostic alternative to full APM tools, optimized specifically for Laravel apps. Features request tracing, performance profiling, slow query detection, OpenTelemetry/Prometheus integration, AI-based anomaly detection, and smart alerting.
✨ Features
Core Capabilities
- 📊 Request Tracing - Capture HTTP requests with route, controller, duration, memory usage, headers
- 🗄️ Database Query Monitoring - Track all queries, detect N+1 problems, identify slow queries
- ⚡ Performance Profiling - Per-route metrics, P50/P95/P99 latency, bottleneck identification
- 🐌 Slow Query Detector - Automatic detection with stack traces and optimization recommendations
- 🤖 AI Anomaly Detection - Statistical analysis (Z-score) to detect unusual patterns
- 📈 OpenTelemetry Export - OTLP protocol support for distributed tracing
- 📊 Prometheus Metrics -
/metricsendpoint for Prometheus scraping - 🔔 Smart Alerting - Slack/Telegram notifications with throttling and deduplication
- 🎨 Built-In Dashboard UI + API - Authenticated web dashboard at
/admin/observabilityplus API endpoints for custom implementations - 🗃️ Database Agnostic - Works with MySQL, PostgreSQL, SQLite, SQL Server
📦 Installation
1. Install via Composer
composer require rylxes/laravel-observability
2. Run Installation Command
php artisan observability:install
This will:
- Publish configuration file to
config/observability.php - Run migrations (creates observability tables)
- Display middleware setup instructions
3. Register Middleware
Add to app/Http/Kernel.php (Laravel 10) or bootstrap/app.php (Laravel 11):
Laravel 10:
protected $middleware = [ // ... other middleware \Rylxes\Observability\Middleware\RequestTracingMiddleware::class, ];
Laravel 11:
->withMiddleware(function (Middleware $middleware) { $middleware->append(\Rylxes\Observability\Middleware\RequestTracingMiddleware::class); })
4. Configure Environment
Add to .env:
OBSERVABILITY_ENABLED=true OBSERVABILITY_TRACING_ENABLED=true OBSERVABILITY_SLOW_QUERY_THRESHOLD=1000 # Optional: Slack Notifications OBSERVABILITY_SLACK_ENABLED=true OBSERVABILITY_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/YOUR/WEBHOOK/URL # Optional: Telegram Notifications OBSERVABILITY_TELEGRAM_ENABLED=true OBSERVABILITY_TELEGRAM_BOT_TOKEN=your_bot_token OBSERVABILITY_TELEGRAM_CHAT_ID=your_chat_id # Optional: Prometheus OBSERVABILITY_PROMETHEUS_ENABLED=true
🚀 Usage
Using the Facade
use Rylxes\Observability\Facades\Observability; // Get performance analysis $analysis = Observability::analyze(days: 7); // Detect slow queries $slowQueries = Observability::slowQueries()->analyze(); // Detect anomalies $anomalies = Observability::anomalies()->detectAnomalies('response_time'); // Export Prometheus metrics $metrics = Observability::exportMetrics();
Artisan Commands
Analyze Performance:
php artisan observability:analyze --days=7 --notify
Prune Old Data:
php artisan observability:prune --force
Web Dashboard (Authenticated)
By default, the package includes a web dashboard at:
/admin/observability
It uses your app's configured auth guards (observability.dashboard.guards), so it works with your existing login/session setup while still exposing the API for internal tools.
API Endpoints
All endpoints are prefixed with /api/observability:
| Endpoint | Description |
|---|---|
GET /metrics |
Prometheus metrics (text format) |
GET /dashboard |
Performance dashboard data (JSON, supports ?days=1..30) |
GET /traces |
Recent request traces |
GET /traces/{traceId} |
Single trace detail |
GET /alerts |
Recent alerts |
POST /alerts/{id}/resolve |
Resolve an alert |
GET /health |
Health check |
Example:
curl http://yourapp.test/api/observability/metrics
curl http://yourapp.test/api/observability/dashboard | jq
⚙️ Configuration
Database Connection
By default, uses your app's default database connection. To use a separate database:
OBSERVABILITY_DB_CONNECTION=observability_db
Then configure the connection in config/database.php.
Request Tracing
'tracing' => [ 'enabled' => true, 'capture_headers' => true, 'capture_payload' => false, // Be careful with sensitive data 'excluded_routes' => ['telescope.*', 'horizon.*'], 'sample_rate' => 1.0, // 0.0 to 1.0 (1.0 = trace all) ],
Slow Query Detection
'queries' => [ 'enabled' => true, 'log_all' => false, // Only log slow queries 'slow_threshold_ms' => 1000, 'detect_duplicates' => true, // N+1 detection ],
Anomaly Detection (AI)
'anomaly_detection' => [ 'enabled' => true, 'z_score_threshold' => 3.0, // Standard deviations 'min_data_points' => 100, 'baseline_window_days' => 7, ],
Notifications
'notifications' => [ 'slack' => ['enabled' => true, 'webhook_url' => env('...')], 'telegram' => ['enabled' => true, 'bot_token' => env('...')], 'throttle' => [ 'enabled' => true, 'window_minutes' => 15, 'max_alerts_per_window' => 1, ], ],
Data Retention
'retention' => [ 'traces_days' => 7, 'queries_days' => 7, 'metrics_days' => 30, 'alerts_days' => 30, ],
Dashboard UI
'dashboard' => [ 'enabled' => true, 'route_prefix' => 'admin/observability', 'middleware' => ['web'], 'guards' => ['web', 'sanctum'], 'refresh_interval_seconds' => 30, ],
📊 Integrations
Prometheus
Enable in .env:
OBSERVABILITY_PROMETHEUS_ENABLED=true OBSERVABILITY_PROMETHEUS_STORAGE=redis # or 'memory', 'apc'
Configure Prometheus to scrape:
scrape_configs: - job_name: 'laravel-app' static_configs: - targets: ['yourapp.test'] metrics_path: '/api/observability/metrics'
OpenTelemetry
OBSERVABILITY_OTEL_ENABLED=true OBSERVABILITY_OTEL_ENDPOINT=http://localhost:4318
Grafana Dashboard
Import the included Grafana dashboard template:
# Coming soon - dashboard JSON in /docs folder
🤖 AI Anomaly Detection
The plugin uses statistical analysis (Z-score method) to detect anomalies:
- Baseline Calculation: Analyzes last 7 days (configurable)
- Statistical Analysis: Calculates mean and standard deviation
- Anomaly Detection: Flags values > 3σ from baseline
- Auto-Alerting: Creates alerts for critical anomalies
Monitored Metrics:
- Response time
- Memory usage
- Error rate
- Query execution time
Example:
$result = Observability::anomalies()->detectAnomalies('response_time'); if ($result['status'] === 'success') { foreach ($result['anomalies'] as $anomaly) { echo "Anomaly: {$anomaly['metric_name']} - "; echo "Value: {$anomaly['value']} (Baseline: {$anomaly['baseline']})"; echo "Deviation: {$anomaly['deviation_percent']}%"; } }
📈 Performance Insights
Dashboard Data Structure
{
"overall_metrics": {
"total_requests": 12500,
"avg_response_time_ms": 145.3,
"p95_response_time_ms": 450,
"p99_response_time_ms": 890,
"avg_memory_mb": 28.5,
"error_rate": 1.2
},
"route_performance": [
{
"route": "api.users.index",
"requests": 3400,
"avg_duration_ms": 89,
"error_rate": 0.5
}
],
"bottlenecks": [
{
"type": "slow_routes",
"severity": "warning",
"data": ["api.reports.generate"]
}
]
}
🔒 Security Considerations
- Sensitive Data: Disable
capture_payloadandcapture_headersin production - Authentication: All API endpoints require authentication by default
- Data Sanitization: Passwords, tokens automatically redacted
- Rate Limiting: Consider adding rate limits to metrics endpoints
🧪 Testing
Run tests:
composer test
Local Development Loop (Deploy Once)
cd .. composer create-project laravel/laravel observability-sandbox cd observability-sandbox composer config repositories.observability '{"type":"path","url":"../Observability Plugin","options":{"symlink":true}}' composer require rylxes/laravel-observability:@dev php artisan observability:install php artisan migrate
This lets you test package changes locally through /admin/observability, then ship one release after validation.
With coverage:
composer test-coverage
📊 Database Schema
The plugin creates 4 tables (with configurable prefix):
| Table | Purpose |
|---|---|
observability_traces |
HTTP request traces |
observability_queries |
Database query logs |
observability_metrics |
Aggregated performance metrics |
observability_alerts |
Generated alerts |
All tables use your app's database connection (MySQL, PostgreSQL, SQLite, SQL Server).
🤝 Contributing
Contributions welcome! Please see CONTRIBUTING.md for details.
📝 License
MIT License. See LICENSE file.
🙏 Credits
Built with ❤️ for the Laravel community by Sherriff Agboola.
- OpenTelemetry PHP: https://github.com/open-telemetry/opentelemetry-php
- Prometheus PHP: https://github.com/PromPHP/prometheus_client_php
📞 Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Email: rylxes@gmail.com
⭐ If you find this useful, please star the repository!
rylxes/laravel-observability 适用场景与选型建议
rylxes/laravel-observability 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「performance」 「monitoring」 「profiling」 「laravel」 「apm」 「prometheus」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rylxes/laravel-observability 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rylxes/laravel-observability 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rylxes/laravel-observability 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Quick profiling of your code for Laravel
g4 application profiler package
CSS/Javascript Minificator, Compressor and Concatenator for TYPO3 - highly configurable frontend asset optimization for CSS/JS merging, minification and compression with optional body parsing, async/defer loading, inline output, data-ignore exclusions, SRI integrity validation/calculation, external
A Symfony bundle for chameleon-system/sanitycheck
WordPress mu-plugin to remove jQuery Migrate from the list of jQuery dependencies and to allow jQuery to enqueue before </body> instead of in the <head>.
Create link to static resources with cache-breaking segment based on md5 of the file
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 42
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-11