From 771de550a59e826a903feadf4dd72be9bdef8a36 Mon Sep 17 00:00:00 2001 From: Meezaan-ud-Din A Wal-Ikram Date: Tue, 7 Jan 2025 14:18:28 +0400 Subject: [PATCH] Add prometheus exporter and scrape metrics --- .k8s/manifest.yml | 6 ++++++ Dockerfile | 2 +- README.md | 2 +- docker-compose.yml | 1 + etc/unit/.unit.conf.json | 29 +++++++++++++++++++++++++++-- metrics/metrics.php | 27 +++++++++++++++++++++++++++ 6 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 metrics/metrics.php diff --git a/.k8s/manifest.yml b/.k8s/manifest.yml index 5b4007e..85ad497 100644 --- a/.k8s/manifest.yml +++ b/.k8s/manifest.yml @@ -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: @@ -181,6 +185,8 @@ spec: ports: - containerPort: 8080 protocol: TCP + - containerPort: 9090 + protocol: TCP --- # HPA apiVersion: autoscaling/v1 diff --git a/Dockerfile b/Dockerfile index b86d4ad..a2613c8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM islamicnetwork/php:8.2-unit +FROM islamicnetwork/php:8.3-unit # Copy files COPY . /var/www/ diff --git a/README.md b/README.md index 173de8d..f7a29f4 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/docker-compose.yml b/docker-compose.yml index 24c1fba..7febe22 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,6 +13,7 @@ services: - alquran_api.env ports: - "80:8080" + - "9090:9090" volumes: - .:/var/www privileged: true diff --git a/etc/unit/.unit.conf.json b/etc/unit/.unit.conf.json index 2e18040..ddca675 100644 --- a/etc/unit/.unit.conf.json +++ b/etc/unit/.unit.conf.json @@ -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": { @@ -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": { @@ -55,7 +80,7 @@ "uri": "*" }, "action": { - "pass": "applications/api.alquran.cloud" + "pass": "applications/api_alquran_cloud" } } ] diff --git a/metrics/metrics.php b/metrics/metrics.php new file mode 100644 index 0000000..937f4fe --- /dev/null +++ b/metrics/metrics.php @@ -0,0 +1,27 @@ +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"; +?> \ No newline at end of file