PHP-FPM and long-running workers
How the PHP SDK sends data without slowing responses - sending after fastcgi_finish_request, short timeouts, APCu-shared circuit breaker, and time-based sending in queue workers and long-running servers.
PHP-FPM
Under PHP-FPM every request starts with an empty process state. The SDK is designed so that this does not cost your users anything:
- Sending happens after the response. The SDK calls
fastcgi_finish_request()(orlitespeed_finish_request()) so the visitor has the response, and then sends one batch. It calls it only when there is something to send, so the visitor’s response never waits for inspector.rest. - Short timeouts. The connect timeout is 200 ms (
connect_timeout_ms) and one send takes at most 3 seconds (timeout_ms). Batches are compressed (zstd withext-zstd, else gzip). - Bounded memory. Records, bodies read from streams and batches are sized against the free
memory_limit, so a download of several hundred MB cannot cause a “memory exhausted” error. Bodies that do not fit are cut (truncated) or omitted. - Nothing blocks the request. Records are built from references while your code runs. With
capture_bodyset to'never'the SDK does not even read the body streams. - Errors are swallowed. Whatever goes wrong inside the SDK is never raised in your code.
When ingest is unavailable
The SDK follows the same rules as the Node.js SDK; see Behavior under load and quota. In PHP-FPM a request cannot wait, so a failed batch is not kept: its records are counted as dropped and the next batch reports the number.
- After
failure_thresholdfailed sends in a row the circuit opens forcircuit_open_ms. No process of the pool tries to send during that time, and request-bound processes stop recording until it closes. The skipped requests are counted as dropped. Your own requests are not slowed down. - When the monthly request limit is reached (
429,quota_exceeded), the SDK stops recording (nothing is kept or serialized) and probes with an empty batch afterRetry-After(60 seconds by default). With APCu the pause is shared by the whole pool; recording resumes when a probe succeeds. - When the organization is suspended (
403,organization_suspended), the SDK pauses and probes in the same way. Recording resumes when the suspension is lifted. - On
401the SDK disables itself.
Without APCu each PHP process starts from a clean state. After a failure the next request tries again (still after its response and bounded by the connect timeout). After quota_exceeded or organization_suspended every request is recorded and sent once before the pause is learned again.
Long-running workers
Queue workers, Laravel Octane, Swoole, RoadRunner and similar servers handle many jobs in one process. There is no “end of request” to send on, so the SDK also sends by time (flush_interval_ms) and by size (batch_bytes) from inside its own calls. A failed batch is kept and sent again with the same batch_id and exponential back-off, within max_queue_bytes. long_running is true by default for the cli SAPI; set it yourself if the SDK does not detect your server.
$inspector = new Inspector([
'server_key' => getenv('INSPECTOR_REST_KEY'),
'long_running' => true,
]);
// On worker shutdown:
$inspector->close();Call $inspector->flush() after a job when you want its requests to show up immediately, and $inspector->close() when the worker stops. PHP does not run shutdown functions on SIGTERM unless you handle the signal, so call close() from your own signal handler. In a loop that waits a long time between requests, call $inspector->tick() to send when a time or size threshold is due.
Nothing is sent while a request is running. A request that is still running when a batch goes out (flush(), close() or a time-based send) and is older than in_progress_after_ms is reported as “in progress”; the finished record replaces it.
Short scripts and CLI
In a CLI script or cron job the SDK sends when the script ends (auto_flush). To be sure, call $inspector->close() as the last line.