Monitoring PHP Applications: From Basic Metrics to Advanced Profiling
Monitoring PHP applications is not just about "watching if the site is down." It is a comprehensive process of collecting, analyzing, and visualizing data about the operation of code, the server, and the database. For the educational portal pythonlib.ru (focused on PHP), we have gathered key tools and techniques that will help you keep your finger on the pulse of your project.
Why Monitor PHP?
- Detect bottlenecks: slow database queries, long loops, suboptimal algorithms.
- Prevent failures: fatal errors, memory limit exceedances, disk space shortages.
- Analyze user experience: page load times, requests per second (RPS), error rate.
- Security: suspicious request patterns, injection attacks.
Key PHP Monitoring Tools
1. Xdebug — The Classic Profiler
Xdebug is a PHP extension that provides stack traces, profiling, and debugging. For monitoring, use the profiling mode (profiler_enable_trigger).
Example php.ini configuration:
zend_extension=xdebug.soxdebug.mode=profilexdebug.output_dir=/tmp/xdebugxdebug.profiler_output_name=cachegrind.out.%pThe result is cachegrind.out files, which can be analyzed using KCachegrind or Webgrind.
2. Pinba — Real-Time Monitoring
Pinba (PHP Is Not A Bottleneck Anymore) is a lightweight metric collector that works as a PHP module and a C server. It sends data about execution time, memory, request count, and errors to MySQL.
Example usage in code:
// Start a timerpinba_timer_start('my_script');
// Your code$data = heavyCalculation();
// Stop the timer and send datapinba_timer_stop('my_script');
// Additional tagspinba_script_name_set('api/v1/users');pinba_hostname_set('web-01');The data automatically goes into Pinba tables, from where it can be visualized in Grafana.
3. Tideways — Commercial Profiler
Tideways (formerly XHProf) is a PHP extension that allows profiling production environments with minimal overhead. It collects:
- Execution time of each function.
- Number of calls.
- Memory consumption.
Installation via Composer:
composer require tideways/php-xhprof-extensionStarting profiling:
$profiler = new Tideways\\Profiler();$profiler->start();
// Your code$result = expensiveOperation();
$profiler->stop();$data = $profiler->getCallgraph(); // data for visualizationError Logging: The Foundation of Monitoring
Without quality logging, monitoring is useless. Use PSR-3 compatible libraries, such as Monolog.
Example of configuring Monolog to send critical errors to Telegram:
use Monolog\\Logger;use Monolog\\Handler\\TelegramBotHandler;
$logger = new Logger('app');$handler = new TelegramBotHandler( 'YOUR_BOT_TOKEN', 'YOUR_CHAT_ID', Logger::CRITICAL);$logger->pushHandler($handler);
try { // dangerous code} catch (\\Throwable $e) { $logger->critical('Error in payment module', [ 'exception' => $e, 'user_id' => $userId, 'ip' => $_SERVER['REMOTE_ADDR'] ]);}Metrics to Track
Here is a minimum set of metrics for any PHP application:
- Response Time — average, 95th and 99th percentiles.
- Number of requests