Skip to content

Monitoring PHP Applications with Prometheus

Professional

You can instrument your PHP application with promphp/prometheus_client_php and let the Glouton agent scrape its /metrics endpoint.

Terminal window
composer require promphp/prometheus_client_php

The snippet below defines a counter and exposes metrics at http://localhost:8080/metrics (assuming this script is served by your web server):

<?php
require __DIR__ . '/vendor/autoload.php';
use Prometheus\CollectorRegistry;
use Prometheus\RenderTextFormat;
use Prometheus\Storage\APC;
$registry = new CollectorRegistry(new APC());
$counter = $registry->getOrRegisterCounter(
'myapp',
'requests_total',
'Total number of processed requests.'
);
$counter->inc();
header('Content-Type: ' . RenderTextFormat::MIME_TYPE);
echo (new RenderTextFormat())->render($registry->getMetricFamilySamples());

Because PHP is stateless between requests, use a shared storage backend such as APC, Redis or APCng to persist counters across workers.

Configure Glouton to scrape your application by adding the following to /etc/glouton/conf.d/50-prometheus-metrics.conf:

metric:
prometheus:
targets:
- url: "http://localhost:8080/metrics"
name: "my_php_application"
allow_metrics:
- "myapp_requests_total"

Restart the agent to apply the configuration. See Prometheus Monitoring for the full list of options, including Docker label auto-discovery and Kubernetes annotations.