Skip to content

Commit

Permalink
Add prometheus exporter and scrape metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
meezaan committed Jan 7, 2025
1 parent 6bd78f5 commit 771de55
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .k8s/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ spec:
app: alquran-cloud-api
template:
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "9090"
prometheus.io/path: "/metrics"
labels:
app: alquran-cloud-api
spec:
Expand Down Expand Up @@ -181,6 +185,8 @@ spec:
ports:
- containerPort: 8080
protocol: TCP
- containerPort: 9090
protocol: TCP
---
# HPA
apiVersion: autoscaling/v1
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM islamicnetwork/php:8.2-unit
FROM islamicnetwork/php:8.3-unit

# Copy files
COPY . /var/www/
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
This repository powers the AlQuran.cloud API on http://api.alquran.cloud.

# Technology Stack
* PHP 8.2
* PHP 8.3
* MySQL 8
* Memcached 1.6
* Kipchak API Development Kit (https://github.com/mam-luk/kipchak)
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
- alquran_api.env
ports:
- "80:8080"
- "9090:9090"
volumes:
- .:/var/www
privileged: true
Expand Down
29 changes: 27 additions & 2 deletions etc/unit/.unit.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
"listeners": {
"*:8080": {
"pass": "routes/main"
},
"*:9090": {
"pass": "routes/metrics"
}
},
"applications": {
"api.alquran.cloud": {
"api_alquran_cloud": {
"type": "php",
"user": "nobody",
"limits": {
Expand All @@ -25,9 +28,31 @@
"root": "/var/www/html",
"index": "index.php",
"script": "index.php"
},
"metrics": {
"type": "php",
"root": "/var/www/metrics",
"index": "metrics.php",
"script": "metrics.php",
"user": "root",
"environment": {
"control_socket": "/var/run/control.unit.sock"
}
}
},
"routes": {
"metrics": [
{
"match": {
"uri": [
"/metrics"
]
},
"action": {
"pass": "applications/metrics"
}
}
],
"main": [
{
"match": {
Expand Down Expand Up @@ -55,7 +80,7 @@
"uri": "*"
},
"action": {
"pass": "applications/api.alquran.cloud"
"pass": "applications/api_alquran_cloud"
}
}
]
Expand Down
27 changes: 27 additions & 0 deletions metrics/metrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

$sock = stream_socket_client("unix://".getenv("control_socket"), $errno, $errst);
fwrite($sock, "GET /status HTTP/1.0\r\n\r\n");
$resp = fread($sock, 4096);
fclose($sock);

list($headers, $body) = explode("\r\n\r\n", $resp, 2);
$json = json_decode($body);

$metrics = array();
array_push($metrics, "unit_connections_accepted_total ".$json->connections->accepted);
array_push($metrics, "unit_connections_active ".$json->connections->active);
array_push($metrics, "unit_connections_idle ".$json->connections->idle);
array_push($metrics, "unit_connections_closed_total ".$json->connections->closed);
array_push($metrics, "unit_requests_total ".$json->requests->total);

foreach($json->applications as $application => $data) {
array_push($metrics, "unit_application_".$application."_processes_running ".$data->processes->running);
array_push($metrics, "unit_application_".$application."_processes_starting ".$data->processes->starting);
array_push($metrics, "unit_application_".$application."_processes_idle ".$data->processes->idle);
array_push($metrics, "unit_application_".$application."_requests_active ".$data->requests->active);
}

header("Content-Type: text/plain");
echo join("\n", $metrics)."\n";
?>

0 comments on commit 771de55

Please sign in to comment.