Guzzle middleware
Record every Guzzle request with GuzzleMiddleware - push it on the handler stack, add labels per client or per request, and understand where in the stack to place it.
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use InspectorRest\GuzzleMiddleware;
use InspectorRest\Inspector;
$inspector = new Inspector(['server_key' => getenv('INSPECTOR_REST_KEY')]);
$stack = HandlerStack::create();
$stack->push(GuzzleMiddleware::create($inspector, ['labels' => ['client' => 'billing-api']]));
$client = new Client(['handler' => $stack]);
$client->get($url, ['inspector_labels' => ['op' => 'list']]); // labels of a single requestWhat is recorded
For every request the middleware reports the method and URL, the request headers and body, the response status, headers and body, the duration (measured with a monotonic clock) and any error. A 4xx or 5xx response counts as an error with the name HttpError, the same as in the Node.js SDK, even when http_errors is off. Exceptions (connection errors, timeouts) are recorded with their cause chain (getPrevious()).
Labels
Labels merge per key; later sources win:
- Automatic labels (
server_host,app_package,app_commit) labelsof theInspectorlabelsoption of the middleware- The context set with
Labels::with([...], fn) inspector_labelsof a single request
use InspectorRest\Labels;
Labels::with(['tenant' => 'acme', 'job' => 'sync'], function () use ($client) {
$client->get('https://api.example.com/items'); // carries tenant=acme, job=sync
});In PHP the context is synchronous: it applies to code that runs inside the callback. See Labels.
Where to push the middleware
$stack->push(...)adds the middleware close to the network. It sees the final request as it is sent (includingContent-Lengthand cookies), and every redirect step is recorded as its own request.$stack->unshift(...)adds it at the outer end. It sees the logical request after redirects.
Pick push when you want to see exactly what goes on the wire.
Bodies
When capture_body is 'never', the SDK does not read the bodies at all. Otherwise it reads the PSR-7 streams after the response is there and leaves them as they were, so your application still reads the same body. Streams that cannot be rewound are recorded without a body (omitted: "stream"). Binary bodies follow the binary capture rules.
Retries
The middleware does not track retries: every request that passes through it is one record with one attempt. If you use a retry middleware, place the Inspector middleware where you want to see each try.