PHP is one of the most popular open source programming languages on the internet, used for web development platforms such as Magento, WordPress, or Drupal. In addition to all PHP bases, PHP-FPM is the most popular alternative implementation of PHP FastCGI. It has additional features which are really useful for high-traffic websites. In this article, you’ll learn how to monitor PHP-FPM with Prometheus. You’ll write Prometheus queries to monitor server status, children processes state, average requests processing, use of CPU and memory per request, etc. Also, troubleshoot all the situations you might have to face in the future.
First stepsHere are the components you’ll need for this PHP-FPM monitoring tool using Prometheus::
- Kubernetes cluster, which you surely already have working.
- Your PHP-FPM service deployed into that cluster.
- Prometheus, the de-facto standard for Kubernetes monitoring.
- PHP-FPM exporter running as a sidecar.
How to install PHP-FPM exporterTo monitor PHP-FPM with Prometheus, you’ll need to install an exporter as a sidecar, which scrapes the PHP-FPM metrics through the exporter’s endpoint. Here, you have a
deployment.yamlexample to deploy the exporter with the PHP-FPM service. Take a look at these two options:
- Listen endpoint for exporter
- Endpoint path for exporter
apiVersion: v1 kind: ConfigMap metadata: labels: app: php-fpm name: php-fpm-config namespace: php-fpm data: www.conf: | [www] listen=127.0.0.1:9000 pm.status_path=/statusFinally, to let Prometheus scrape the PHP-FPM exporter, you have to check the environment variables:
Top metrics for monitoring PHP-FPM with PrometheusFirst, let’s assure you configure the exporter properly. Type
phpfpm_upin the Prometheus console. Maybe it’s a very simple metric, but a very important one to be alerted if everything is on fire.
sum(phpfpm_up)This way, you will count all PHP-FPM applications that the exporters can correctly connect to. In this case, it’s a scenario with three replicas up. Next, you can go a step further and check other golden metrics like traffic or saturation.
How to monitor PHP-FPM traffic with PrometheusLet’s create a dashboard where you can see real time traffic in all your applications. You can use the following metric and show current request traffic per child in every process. This way, you will see any imbalance with loads in your application.
sum by (child) (rate(phpfpm_process_requests [1h]))In this case, you used one-hour intervals as a normalizer for requests. Interval value changes in every environment. Environments with high loads will need smaller intervals to be able to see changes in a short time period. Environments with low loads will work perfectly with long intervals so it will be easy to see any spike in the amount of requests. With this metric, you can see traffic consumed by children in every application.
How to monitor PHP-FPM saturation with PrometheusTo measure saturation, you can use the two following metrics.
phpfpm_listen_queue_lengthThe first gives you information about the actual state of processes in the queue for your application, while the second one provides you with the actual limit for that queue. When you combine those, you will easily avoid possible bottlenecks in your application. This way, you will be aware of saturation in your applications.
How to monitor PHP-FPM latency PrometheusLatency metrics show slow requests caused by possible bottlenecks in your service. For that reason, you track the time process of your requests and warn when they reach some specific value. You can use the following metric with accommodations and acquire useful information.
sum (rate (phpfpm_slow_requests[5m])The
phpfpm_slow_requestsmetric counts the number of requests that reached a predefined timeout value. So, by making a five minute interval, you can check if latency rise is happening in your services.