inspector.restDocs

PHP SDK

Install inspector-rest/sdk with Composer, create an Inspector with your ingest key and see the configuration options that match the Node.js SDK. Requires PHP 8.1 or newer.

inspector-rest/sdk records the HTTP requests your PHP application makes (request, response, bodies, retries, errors, timings) and sends them to your inspector.rest environment. It uses the same configuration names, label order and record format as the Node.js SDK.

Requirements

  • PHP 8.1 or newer with ext-curl and ext-json.
  • Guzzle 7 for the Guzzle middleware.
  • Optional: ext-apcu (shares state between PHP-FPM workers, see PHP-FPM), ext-zstd (zstd compression, preferred over gzip when present) and ext-zlib (gzip).

Install

Terminal
composer require inspector-rest/sdk

Quick start

PHP
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use InspectorRest\GuzzleMiddleware;
use InspectorRest\Inspector;

$inspector = new Inspector([
    'server_key' => getenv('INSPECTOR_REST_KEY'), // irk_<org-slug>_<secret>
    'service' => 'billing',
    'labels' => ['env' => 'production'],
]);

$stack = HandlerStack::create();
$stack->push(GuzzleMiddleware::create($inspector));
$client = new Client(['handler' => $stack]);

$client->get('https://api.example.com/items');

Create the Inspector once per process (a service container singleton is a good place) and reuse it for every Guzzle client you want to record.

The ingest key

Every environment has its own ingest key, shown once in the console. It looks like irk_<org-slug>_<secret>. The SDK derives the API address from it: https://<org-slug>.api.inspector.rest. Without a usable key the SDK does nothing and your requests are not affected. If ingest answers 401 (key revoked), the SDK disables itself.

Configuration

Configuration is an array passed to new Inspector([...]). The names are the same as in Node.js; see the Node.js configuration reference for the meaning of each option. The PHP defaults that differ are listed here.

OptionDefaultNotes
server_key–Ingest key.
service–Service name stored with every batch.
labels[]Labels added to every request. See Labels.
capture_body'always''always', 'on_error' or 'never'. With 'never' the body streams are not read at all.
max_body_bytesunlimitedCuts text bodies on a UTF-8 character boundary.
capture_binary / max_binary_bytesfalse / 10 MiBUpload binary bodies. See Binary bodies.
sample_rate1Fraction of requests recorded (0 to 1).
include_hosts, exclude_hosts, include_paths, exclude_paths–Lists of patterns with the * wildcard.
redact_headers, redact_fields–Lists of names whose values become [redacted].
redact_hook–callable(array): mixed, called with the record before it is serialized. Return false to drop it.
error_details–callable(Throwable): mixed. Adds the error’s own fields as details.
batch_bytes, flush_interval_ms1 MiB, 1000A batch is sent at this size or age.
max_queue_bytes16 MiB (16777216)Upper bound of the records held in memory; also limited to a quarter of the free memory_limit. The oldest records are dropped beyond it.
connect_timeout_ms200Connect timeout of one send.
timeout_ms3000Timeout of one send.
close_timeout_ms5000close() and flush() stop waiting after this long.
in_progress_after_ms2000At flush() and close(), a request that is still running and older than this is reported as in progress. The finished record replaces it.
compression'auto''auto' (zstd, else gzip), 'zstd', 'gzip' or 'none'.
failure_threshold, circuit_open_ms3, 30000Circuit breaker for failed sends.
auto_flushtrueRegister the shutdown flush and, in long-running workers, send by time and size. With false only flush() and close() send.
long_runningdetectedtrue for CLI workers (the default for the cli SAPI: queue workers, Swoole, RoadRunner, Octane), false for request-bound processes such as PHP-FPM. Long-running workers also send by time and keep a failed batch to send it again. See PHP-FPM and long-running workers.
sendercURLAn InspectorRest\Sender that replaces the built-in cURL transport, for example to send through your own HTTP stack or in tests.

Methods

Method
flush()Sends everything recorded so far now. Bounded by close_timeout_ms; never throws.
close()Final delivery attempt (requests that are still running are reported as in progress), then stops the inspector.
tick()For loops that wait a long time between requests: sends when a time or size threshold is due.
stats()Counters for diagnostics: state, captured and sent records, queued records, held bytes and send failures.

Errors inside the SDK are swallowed. They never reach your application.