What you get out of the box
Trace id
Every request gets anApivalkTraceId, a UUIDv7, before the global middleware run. Apivalk’s request log lines carry it under apivalk_trace_id, and enableTraceIdInResponseHeaders() sends it back to the client. How to read it in a controller, a routed or a global middleware and the exception handler is in Trace id. Put it into your own log lines under the same key, and one id finds every line of a request.
What Apivalk logs
The first three go to the PSR-3 logger of
ApivalkConfiguration, a NullLogger when you pass none. JwtAuthenticator logs to the logger passed to its own constructor.
That is all. There is no line per request and nothing for a 4xx your routes and middleware answer: an invalid token, a failed validation or a 404 is an answer, not a failure. The access log, metrics and traces are yours, built from the hooks below.
Where to hook in
If you are looking for an event, this is where it lives in 4.0:
Access log
TheAccessLogMiddleware in Middleware is a global middleware, so the 404 and the 500 get a line too, with method, path, status and trace id. For the duration, take hrtime(true) before $handler->handle($request) and log the difference, as the metrics middleware below does. An access log line may carry the raw path; a metric label may not.
Metrics
A routed middleware that records method, route, status and duration.HttpMetrics stands for your metrics client (a Prometheus client, StatsD, an OpenTelemetry meter):
$route->getUrl() (/pets/{pet_id}), never with the request path (/pets/7). Every distinct label value is a new time series in your metrics backend: the template gives one series per route, the path one per id, and any client can create new series by requesting new ids until the backend runs out of memory.
Add it first to the routed stack, so it times authentication, rate limiting and validation and sees their 401, 403, 429 and 422. A 404 or 405 never reaches it. If you need those, count them in a global middleware under a fixed label, again never the path.
Error reporting
If your error tracker has a PSR-3 integration, you need no code: pass it as the logger, or add it as a handler to the logger you pass. Every unhandled throwable reaches it aterror level, with the throwable under exception and the trace id under apivalk_trace_id.
Otherwise report from the exception handler. $errorReporter stands for your tracker’s client:
handle() catches, one from a global middleware included, right after the error log line. The request is the PSR-7 request without an ApivalkContext, so the route is not there. If the reporter throws, Apivalk logs that at critical and the client still gets the default 500. See Error Handling.
OpenTelemetry
Apivalk is a PSR-15 RequestHandlerInterface and its middleware are PSR-15 middleware, so OpenTelemetry’s PSR-15 instrumentation works without anything Apivalk specific. Without the extension, a global middleware does the same job by hand.
Tested against Apivalk 4.0 on FrankenPHP, classic and worker mode, with
ext-opentelemetry 1.5.0, open-telemetry/sdk 1.18.0, open-telemetry/opentelemetry-auto-psr15 1.3.2 and the console exporter.Auto-instrumentation
Install theopentelemetry PHP extension as the OpenTelemetry PHP docs describe, then the SDK, the PSR-15 instrumentation and an exporter:
Apivalk::handle()is the firstRequestHandlerInterface::handle()of the request, so it becomes the server span. It continues an incomingtraceparent.run()goes throughhandle(), so a plain front controller is covered too.- One internal span per middleware
process()and per handler (MiddlewareChain,RoutingHandler,DispatchHandler), global and routed stack alike. - The server span is named after the HTTP method only (
GET) and carriesurl.path, not the route. The middleware below adds it. - The instrumentation marks the server span and every handler span as an error when the status is 400 or higher, a 404 or a 422 included.
- A throwable is recorded on the spans of the routed stack it passed through. The server span sees the 500 the exception handler built, not the exception.
OpenTelemetry\API\Trace\SpanInterface. A routed middleware names it after the route template, for the same cardinality reason as the metrics label, and adds the trace id, so a trace leads to the log lines:
Route match, request population and respond
The PSR-15 instrumentation stops athandle() and process(). Matching the route (loading the route index from the cache included) and building the typed request happen inside RoutingHandler, the controller and the JSON encoding inside DispatchHandler, so they only show up as the own time of those spans. The extension’s hook() gives them spans of their own, without changing Apivalk:
MiddlewareChain spans left out):
- The controller has no span of its own: every controller declares its own
__invoke(), there is no parent method to hook. Its time isDispatchHandler::handleminusapivalk.respond. apivalk.respondalso shows up where a middleware answers (401, 403, 422, 429) and directly underRoutingHandlerfor the 404, 405 and the exception handler’s 500.- The hook on
AbstractRouter::matchcovers your own router as well.
Without the extension: a span middleware
open-telemetry/sdk and an exporter are enough for a server span of your own, the extension is not needed. Add this first to the global stack, so it wraps everything else and every response, the 404 and the 500 included:
catch: inside the global stack, a throwable from routing, a routed middleware or the controller comes back as the exception handler’s response. The finally ends the span when a global middleware added after it throws.
It puts the span under the same request attribute as the auto-instrumentation, so RouteSpanMiddleware names it after the route as well. Use one or the other: with both, every request gets two server spans.
The SDK still reads the OTEL_* variables above when OTEL_PHP_AUTOLOAD_ENABLED=true; without autoloading, set up a tracer provider as the OpenTelemetry PHP docs show.