Observability is a key property of modern systems that allows understanding what is happening inside an application by collecting signals such as metrics, logs, and traces. Unlike simple monitoring (which only detects symptoms), observability lets us diagnose root causes.
In distributed environments, having observability tools is essential for detecting bottlenecks, performance drops, hidden errors, and unexpected behaviors. One of the most popular and accessible solutions is Prometheus, an open-source metrics collector originally created by SoundCloud, which allows querying data in real-time using its own query language: PromQL.
In this article, I will show you how to implement a complete observability flow in PHP, using only Prometheus.
π― What will you learn?
- What observability is and why it matters
- How to generate custom metrics using PHP
- How to configure Prometheus to collect them
- How to visualize metrics directly in Prometheus dashboard
π§° Tools used
Tool | Purpose |
---|---|
PHP (CLI) | Metric simulation and exposure |
Prometheus | Metric collection and querying |
Windows | Local development environment |
π§ͺ Step 1: Generate metrics from PHP
We create two PHP files: one to simulate metrics and another to expose them for Prometheus.
π generator.php
<?php
$counter = 0;
while (true) {
$start = microtime(true);
sleep(rand(1, 3));
$counter++;
$duration = microtime(true) - $start;
file_put_contents("metrics.txt",
"# HELP uploads_total Total uploads
" .
"# TYPE uploads_total counter
" .
"uploads_total {$counter}
" .
"# HELP upload_processing_seconds Processing time
" .
"# TYPE upload_processing_seconds gauge
" .
"upload_processing_seconds {$duration}
"
);
sleep(1);
}
π metrics.php
<?php
header('Content-Type: text/plain');
readfile("metrics.txt");
βΆοΈ Step 2: Run the scripts
- Open a terminal and run the generator:
php generator.php
- In another terminal, start the PHP server:
php -S localhost:8000 metrics.php
- Verify everything works by visiting: π http://localhost:8000
βοΈ Step 3: Configure Prometheus
- Download Prometheus from: https://prometheus.io/download
- Extract the ZIP and create
prometheus.yml
:
global:
scrape_interval: 5s
scrape_configs:
- job_name: 'php_metrics'
static_configs:
- targets: ['localhost:8000']
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
βΆοΈ Step 4: Run Prometheus
In PowerShell or CMD, run:
.\prometheus.exe --config.file=prometheus.yml
Open: π http://localhost:9090
π Step 5: Query metrics in Prometheus
- In the dashboard, go to βStatus β Targetsβ and check if it's UP
- In the βGraphβ tab, run:
uploads_total
then:
upload_processing_seconds
Press Execute to see the PHP-generated metrics.
π 1. upload_processing_seconds
upload_processing_seconds{instance="localhost:8000", job="php_metrics"} 3.0004351139069
Description:
This metric shows the time it took to process a file upload in PHP. In this example, it took about 3 seconds.
Labels:
-
instance="localhost:8000"
: Source endpoint (metrics.php
on port 8000) -
job="php_metrics"
: Job name defined inprometheus.yml
π¦ 2. uploads_total
uploads_total{instance="localhost:8000", job="php_metrics"} 1
Description:
This metric is a counter of how many times the upload process ran. In this case, it only ran once.
Labels:
-
instance="localhost:8000"
: Metric source endpoint. -
job="php_metrics"
: Registered job for that metric.
π¦ GitHub Repository
https://github.com/daniellupaca/php_Prometheus-/tree/main
π§ Conclusion
Prometheus allows you to instrument any modern app without much complexity. By integrating real-time metrics from PHP, you get an accessible solution for academic projects, performance testing, or production systems without needing external tools like Grafana.
This approach can easily be extended to Node.js, Python, Java, etc.
β€οΈ Was this useful?
If you liked this article, share it or comment your experience using Prometheus and PHP.
Top comments (0)