Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Prometheus Laravel Package

spatie/laravel-prometheus

Export Laravel app metrics to Prometheus via a /prometheus endpoint. Register custom gauges/counters with simple callbacks, use built-in queue and Horizon metrics, and optionally secure the endpoint. Ideal for scraping by Prometheus and charting in Grafana.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Metrics-Centric Alignment: The package excels in observability-first architectures, particularly for Laravel applications where real-time monitoring (e.g., request latency, error rates, queue processing) is critical. It integrates seamlessly with Prometheus (a CNCF-standard monitoring tool), making it ideal for microservices, serverless, or containerized deployments where metrics-driven scaling is a priority.
  • Laravel Native: Leverages Laravel’s service container, events, and middleware for minimal intrusion. Metrics collection is non-blocking (via Prometheus client library) and configurable (e.g., custom counters, gauges, histograms).
  • Extensibility: Supports custom metrics via decorators or event listeners, enabling domain-specific instrumentation (e.g., business KPIs, third-party API calls).

Integration Feasibility

  • Low Friction: Requires only composer installation and Prometheus server setup (no database dependencies). Configuration is YAML-based (config/prometheus.php), with zero runtime overhead by default.
  • Prometheus Ecosystem Synergy: Works with Grafana (for dashboards), Alertmanager (for alerts), and Thanos (for long-term storage). Compatible with k8s (via prometheus-operator) or bare-metal setups.
  • Laravel Ecosystem Compatibility:
    • Queue Workers: Tracks job processing time (via prometheus:queue middleware).
    • HTTP Requests: Auto-instruments routes (via prometheus:http middleware).
    • Database: Optional integration with spatie/laravel-activitylog for query metrics.

Technical Risk

  • Prometheus Server Dependency: Requires external Prometheus instance (not included). Misconfiguration (e.g., scrape intervals, labels) may lead to metric cardinality explosions or high cardinality pitfalls.
  • Laravel Version Lock: Tested against Laravel 10+; backporting to older versions may introduce deprecation risks (e.g., Illuminate\Support\Facades\Route changes).
  • Custom Metrics Complexity: Over-instrumentation can bloat Prometheus storage. Requires discipline in metric naming (e.g., laravel_http_request_duration_seconds) and retention policies.
  • Performance Impact: While minimal, high-frequency metrics (e.g., per-request counters) may add microsecond latency in edge cases. Benchmark with prometheus:disable to validate.

Key Questions

  1. Observability Goals:
    • Are we monitoring SLOs (e.g., 99th percentile latency), SLIs (e.g., error budgets), or debugging (e.g., slow queries)?
    • Do we need custom business metrics (e.g., checkout funnel steps)?
  2. Prometheus Infrastructure:
    • Is Prometheus self-hosted (k8s, VM) or managed (e.g., AWS Managed Prometheus)?
    • What’s the scrape interval (default: 15s) and retention period?
  3. Alerting Strategy:
    • Will we use Alertmanager for critical thresholds (e.g., http_requests_total > 1000)?
  4. Cost Implications:
    • High-cardinality metrics (e.g., user_id labels) may inflate Prometheus storage costs.
  5. CI/CD Integration:
    • Should metrics be validated in staging (e.g., via prometheus-blackbox-exporter)?

Integration Approach

Stack Fit

  • Laravel-Centric: Optimized for Laravel’s event-driven and middleware-based architecture. Works alongside:
    • Sentry (for errors) → Prometheus (for metrics).
    • Laravel Telescope (for debugging) → Prometheus (for production metrics).
  • Cloud-Native: Ideal for:
    • Kubernetes (via prometheus-operator + ServiceMonitor CRDs).
    • Serverless (AWS Lambda + Prometheus remote write).
    • Hybrid (on-prem Laravel + cloud Prometheus).
  • Polyglot Compatibility: Can coexist with other monitoring tools (e.g., Datadog, New Relic) via Prometheus exporters.

Migration Path

  1. Phase 1: Core Metrics (Low Risk)

    • Install package: composer require spatie/laravel-prometheus.
    • Configure config/prometheus.php with default metrics (HTTP, queues, DB).
    • Deploy Prometheus server (e.g., prom/prometheus Docker image).
    • Validate metrics in Grafana using laravel-prometheus dashboard.
  2. Phase 2: Custom Instrumentation (Medium Risk)

    • Add custom counters (e.g., increment('api.calls.success')).
    • Instrument critical paths (e.g., payment processing) with histograms.
    • Test with load testing (e.g., Artillery) to ensure no performance regression.
  3. Phase 3: Alerting & Scaling (High Impact)

    • Set up Alertmanager rules (e.g., error_rate > 0.1).
    • Configure Prometheus remote write (e.g., to Thanos or Cortex) for long-term storage.
    • Optimize metric labels to avoid cardinality explosions.

Compatibility

  • Laravel Versions: Officially supports 10.x; 9.x may require minor tweaks (e.g., Route::middleware() syntax).
  • PHP Versions: Requires PHP 8.1+ (due to Prometheus client dependencies).
  • Prometheus Client: Uses prometheus/client_php, which is stable but may need updates for new Prometheus features (e.g., histogram buckets).
  • Database: No direct DB dependency, but query metrics require spatie/laravel-query-logger or manual instrumentation.

Sequencing

Step Task Dependencies Owner
1 Install package + configure config/prometheus.php Laravel app Backend
2 Deploy Prometheus server (e.g., Docker/K8s) Network access DevOps/SRE
3 Validate core metrics (HTTP, queues) Prometheus endpoint QA
4 Add custom metrics (e.g., business KPIs) Domain knowledge Product/Backend
5 Integrate Grafana dashboards Prometheus data source Observability
6 Set up Alertmanager rules SLOs/SLIs SRE
7 Optimize retention/storage Cost constraints DevOps

Operational Impact

Maintenance

  • Package Updates: Low effort—MIT-licensed with semver compliance. Update via composer update.
  • Configuration Drift: Centralized in config/prometheus.php; Gitops-friendly.
  • Deprecation Risk: Minimal; relies on stable Laravel/PHP APIs. Monitor Spatie’s changelog for breaking changes.

Support

  • Troubleshooting:
    • Missing Metrics: Check Prometheus scrape targets (http://laravel-app/metrics).
    • High Latency: Profile with XHProf to isolate instrumentation overhead.
    • Label Errors: Validate metric names against Prometheus naming conventions.
  • Community: 266 stars, active GitHub issues, and Spatie’s support culture (responsive to PRs).
  • Documentation: Comprehensive README + usage examples (e.g., custom metrics, queue tracking).

Scaling

  • Horizontal Scaling: Prometheus pulls metrics (no single point of failure). Use Prometheus Federation for multi-region setups.
  • Metric Volume: Default metrics are lightweight (~100 metrics). Custom metrics may require:
    • Sampling (e.g., 1% of requests).
    • Aggregation (e.g., sum by (route)).
  • Cost Optimization:
    • Use Prometheus relabeling to reduce cardinality.
    • Archive raw data to Thanos or VictoriaMetrics.

Failure Modes

Failure Scenario Impact Mitigation
Prometheus server down Metrics unavailable Deploy high-availability Prometheus (HA setup).
Network partition (Laravel → Prometheus) Missing scrape data Set scrape timeout (e.g., 10s) and retry logic.
Over-instrumentation High cardinality → storage costs Enforce metric naming standards (e.g., no dynamic labels).
Laravel app crash No new metrics Use Prometheus’ scrape_timeout to avoid flapping.
Custom metric misconfiguration
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport