Backend Development 10 min read

Building a PHP Extension for Automated Web API Monitoring at 58 Anjuke

This article describes the design, implementation, and deployment of a PHP extension that enables automated, low‑overhead monitoring of web API performance, detailing its flexible configuration, high resource efficiency, concurrency handling, and successful production rollout within the 58 rental business platform.

58 Tech
58 Tech
58 Tech
Building a PHP Extension for Automated Web API Monitoring at 58 Anjuke

The article introduces a PHP‑extension‑based solution for automating Web API monitoring, aiming to simplify API health checks for 58 Anjuke's PHP services.

Background : Rapid growth of rental business APIs led to a lack of unified monitoring tools, causing issues such as timeout detection, traffic analysis, and performance insight.

Design Principles :

Flexible customization: standardize collected data (cluster name, URI, HTTP code, latency, etc.) and allow configurable collection intervals and URI models.

Low cost integration: load the extension at PHP process start, requiring no code changes from developers.

High resource utilization: use shared memory and thread‑safe mechanisms to minimize CPU and memory impact.

Configuration example:

wrm.enable=1
wrm.tick=60
wrm.timeout=2000
wrm.collector_register_url=https://****.**.com/****/****
wrm.collector_port=****
wrm.cluster=***_***_***
wrm.total_url_num=500
wrm.enable_sync_uri=1
wrm.sync_uri_url=****.**.com/****/****

System Architecture :

Process startup: PHP Zend engine loads the dynamic extension, forks a monitoring process via the MINIT hook.

Log collection: RINIT gathers request data (URI, HTTP code, latency) into a trace structure; RSHUTDOWN records response metrics.

Data reporting: a timer‑driven thread swaps hash tables, compresses data, and asynchronously reports to the central service.

Key technical challenges addressed:

Preventing reporting traffic spikes by aggregating logs locally before periodic compressed uploads.

Designing an efficient hash‑based data structure using the bkrdhash algorithm to store per‑URI metrics within ~10 MB for 10 000 URIs.

Ensuring concurrent writes with futex kernel‑level locks.

Isolating collection and reporting via dual shared‑memory regions (A and B) for thread‑safe asynchronous reporting.

Low‑cost deployment: the .so extension works with PHP 7+ and can be loaded via php.ini or project‑level extension directories.

Results : Deployed across dozens of clusters, covering over a hundred PHP servers, collecting >1 million logs per minute, achieving near‑real‑time monitoring, reducing incident response from days to minutes, and providing detailed traffic analytics for capacity planning.

Authors : Chen Yitian and Hu Kun from 58 Rental Technology Department.

performanceBackend DevelopmentObservabilityPHPextensionAPI monitoring
58 Tech
Written by

58 Tech

Official tech channel of 58, a platform for tech innovation, sharing, and communication.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.