Monitoring and logging are crucial aspects of any sound or robust infrastructure or system. Monitoring is the process of continuously collecting, analyzing and visualizing data from a system, application, or infrastructure to ensure that it is functioning correctly and efficiently. This helps to mitigate any issues before they impact users or business operations.

Importance of Monitoring in Software Applications
Monitoring helps to maintain the health, performance, and security of an application. Some of the reasons why monitoring is essential for any application include:

  • Ensures your application is always available and reliable. It prevents downtime by detecting failures early.
  • Identifies slow database queries, API calls, or resource bottlenecks.
  • Enhances security and compliance by monitoring logs for potential security threats.
  • Helps in debugging and troubleshooting.
  • Enables scalability and capacity planning.

What is Prometheus
Prometheus is an open-source systems monitoring and alerting toolkit that collects and stores its metrics as time series data i.e. it stores metrics information with the timestamp at which it was recorded, alongside optional key-value pairs called labels.

Metrics are numerical measurements, and time series refers to the recording of changes over time. Metrics help you to understand why your application works the way it does. For example, if your application is running slow, you will need some information. If your application has a high number of requests, it can make your system slow. And the only way to know is to have a request count metric. With this, you can determine the cause and proactively increase the number of servers to handle the load.

Prometheus provides:

  • Real-time metrics collection
  • Custom alerts for anomalies
  • Detailed dashboards with Grafana
  • Easy integration with laravel and other systems.

How Prometheus Works
Prometheus operates on a pull-based model where:

  1. Your laravel application exposes metrics. This is done by creating an endpoint (e.g. /metrics) in your laravel application to expose application-specific metrics.
  2. Prometheus periodically fetches data from the /metrics endpoint.
  3. This data is stored in prometheus and can be queried using PromQL.
  4. Finally, you can choose to use Grafana to visualize your data or just view in prometheus.

Setting Up Prometheus in Your Laravel Application

*Step 1: * Install Prometheus Exporter
To expose your application's metrics, you need a Prometheus client library for PHP. And we will be using promphp/prometheus_client_php in this lesson.

Run:

composer require promphp/prometheus_client_php

Step 2: Configure Storage for Prometheus
We will be using Redis because it is highly recommended for production.

Install Redis on your system

sudo apt-get update
sudo apt-get install redis

On your command prompt, start redis:

redis-server

Verify Redis is running:

redis-cli ping

If it returns PONG, redis is active.

Install Redis and Predis in Laravel

composer require predis/predis

Step 3: ** Define Metrics in Laravel
Create a new **MetricsController
to expose Prometheus metrics

use Prometheus\CollectorRegistry;
use Prometheus\RenderTextFormat;
use Prometheus\Storage\Redis;
use Illuminate\Routing\Controller;

class MetricsController extends Controller
{
    public function metrics()
    {
        // Configure Redis storage
        $adapter = new Redis([
            'host' => '127.0.0.1',
            'port' => 6379
        ]);
        $registry = new CollectorRegistry($adapter);

        // Define a counter metric for HTTP requests
        $counter = $registry->getOrRegisterCounter(
            'app', 'http_requests_total', 'Total number of HTTP requests', ['method', 'route']
        );
        $counter->incBy(1, [request()->method(), request()->path()]);

        // Render Prometheus metrics in the required format
        $renderer = new RenderTextFormat();
        return response($renderer->render($registry->getMetricFamilySamples()))
            ->header('Content-Type', RenderTextFormat::MIME_TYPE);
    }
}

*Step 4: * Create a Route to Expose Metrics
Add the following route in routes/api.php
Route::get('/metrics', [MetricsController::class, 'metrics']);

Step 5: Install and Configure Prometheus

  1. Download Prometheus For Linux:
wget https://github.com/prometheus/prometheus/releases/download/v2.50.0/prometheus-2.50.0.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
cd prometheus-*

For Windows:

  1. Configure Prometheus to scrape Laravel metrics. Edit the prometheus.yml file inside your prometheus folder:
global:
  scrape_interval: 5s  # How often Prometheus scrapes data

scrape_configs:
  - job_name: 'laravel_app'
    metrics_path: '/metrics'
       static_configs:
      - targets: ['localhost:8000'] # Change to your Laravel app’s hostname and port
  1. Run Promtheus
  2. cd into your Prometheus folder you installed above. Then run:
./prometheus --config.file=prometheus.yml

Step 6: Verify Prometheus is Scraping Laravel

  • Visit Promtheus UI: http://localhost:9090
  • Query Metrics: Enter app_http_requests_total to see the recorded requests.

*Step 7: * Visualize Metrics in Grafana (Optional)

  1. Install Grafana:
wget https://dl.grafana.com/oss/release/grafana-10.0.1.linux-amd64.tar.gz
tar -zxvf grafana-10.0.1.linux-amd64.tar.gz
cd grafana-10.0.1
./bin/grafana-server
  1. Open Grafana UI (Http://localhost:3000) and add Prometheus as a data source (http://localhost:9090).
  2. Create dashboards to visualize your Laravel application metrics.

Conclusion
By following these steps, you now have:

  • Laravel exposing metrics via /metrics
  • Redis storing Prometheus data
  • (Optional) Grafana visualizing application performance.

Thank you for reading. In my next article, I will dive deep into how to add more custom metrics like histograms, counters, gauge etc. If you love this article, don't forget to follow and also save for future use.