Skip to main content
Apivalk has no event system of its own. The two PSR-15 middleware stacks, the exception handler and the trace id cover every point an event would mark, with the same tools you already use for everything else.

What you get out of the box

Trace id

Every request gets an ApivalkTraceId, 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

The AccessLogMiddleware 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):
Label with the route template, $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 at error 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:
It runs for every throwable 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 the opentelemetry PHP extension as the OpenTelemetry PHP docs describe, then the SDK, the PSR-15 instrumentation and an exporter:
Configure it through the environment, e.g.:
What you get:
  • Apivalk::handle() is the first RequestHandlerInterface::handle() of the request, so it becomes the server span. It continues an incoming traceparent. run() goes through handle(), 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 carries url.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.
The instrumentation stores the server span on the request under the attribute 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:
A 404 or 405 never reaches a routed middleware, so its span keeps the plain method name.

Route match, request population and respond

The PSR-15 instrumentation stops at handle() 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:
A routed request then looks like this (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 is DispatchHandler::handle minus apivalk.respond.
  • apivalk.respond also shows up where a middleware answers (401, 403, 422, 429) and directly under RoutingHandler for the 404, 405 and the exception handler’s 500.
  • The hook on AbstractRouter::match covers 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:
It needs no 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.

Long-running workers

Every recipe here keeps its request state in local variables and request attributes, never on the middleware, so it is safe in a FrankenPHP worker, where one middleware instance serves every request.