Expose response bytes written (and status) when the server finishes writing a response

Author: alvarosanchezCreated Sep 21, 2026Updated Sep 22, 2026
Labelstype: enhancement

Feature request

There is no public way to learn how many bytes a response actually carried once the server has finished writing it.

This matters for anything that meters response volume: bandwidth quotas, usage-based billing, per-tenant transfer limits, or simply richer access logging. For a fixed-length body a filter can read Content-Length, but for a streamed body — a Publisher, SSE, StreamedFile — @ResponseFilter runs before the body is consumed, so at filter time the size is unknown and often unknowable.

What exists today

HttpRequestTerminatedEvent carries only the request, and its Javadoc explicitly disclaims delivery:

java
// http/src/main/java/io/micronaut/http/context/event/HttpRequestTerminatedEvent.java
public class HttpRequestTerminatedEvent extends ApplicationEvent {
    public HttpRequestTerminatedEvent(HttpRequest<?> request) { super(request); }
    @Override public HttpRequest<?> getSource() { return (HttpRequest<?>) super.getSource(); }
}

So there is no status and no byte count on the public surface. io.micronaut.http.context.event contains only HttpRequestReceivedEvent, HttpRequestTerminatedEvent and package-info.

The information does exist internally. RequestHandler has a responseWritten hook that fires at exactly the right moment:

java
// http-server-netty/src/main/java/io/micronaut/http/server/netty/handler/RequestHandler.java
/**
 * Called roughly when a response has been written. In particular, it's called when the user
 * is "done" with the response and has no way of adding further data. The bytes may not have
 * been fully flushed yet, but e.g. the response {@link org.reactivestreams.Publisher} has been
 * fully consumed.
 */
default void responseWritten(@Nullable Object attachment) {
}

but it is @Internal, carries no byte count, and RoutingInBoundHandler uses it only to clean up the request.

Proposal

Expose the completion point publicly, with the response status and the number of bytes written — either by adding a HttpResponseWrittenEvent (request, status, bytes written) or by extending the existing terminated event with that information where the transport can supply it.

Requirements that make it useful:

  • Fires after a streamed body has been fully consumed, not when the response was handed off.
  • Reports bytes actually written for the body (compressed size, if compression applied, is the interesting number for a transfer quota — or expose both).
  • Fires on the error path too, so a partially written response that failed mid-stream is still observable.

If the byte count cannot be provided cheaply on every transport, exposing the completion moment alone would already be an improvement over HttpRequestTerminatedEvent, since it is the only signal that a streamed body has finished.

Related

#7704 (closed) was about the terminated event not firing at all for stream responses; on current branches it does fire, via RequestHandler.responseWritten. This request is about the event's payload rather than its timing.

Environment Information

Verified by reading source on branches 5.2.x and 5.3.x.

Version

5.2.2

Source: micronaut-projects/micronaut-core