Any equivalent of $request_time and $upstream_header_time from nginx?

I know I haven’t followed the template, but I haven’t made the switch to Caddy yet. I’ve seen all the maintainability benefits Caddy would bring over my nginx configuration, and I’ve been able to find equivalents for all things in the configuration, with one exception.

add_header X-Trip-Time    $request_time;
add_header X-Process-Time $upstream_header_time;

Within my nginx.conf, I add these two headers to the response sent to the client. $request_time represents the time between nginx receiving the client’s request and sending out its response to client. $upstream_header_time represents the time between nginx sending a request to the upstream server and receiving a response back (specifically the headers of that response, since nginx does not wait for the response body before forwarding its headers).

I’ve checked out the available placeholders documented here, but couldn’t find anything related. I understand that it’s probably possible to add this functionality if I look into making caddy plugins, but is there any placeholder I might have missed that would allow me to report to client how long the request took, and how long our upstream server spent within that request?

Good question. I would recommend opening feature requests for these! Issues · caddyserver/caddy · GitHub

(New features typically get worked on pretty quickly but it can be hit-or-miss. These look easy.)

Caddy does have the {http.response.duration} placeholder, but it’s not available until after the request is complete and about to be logged.

Maybe what could be done is write the request start time to r.Context() and then have a “response time so far” placeholder which would calculate the current time minus the start time. The accuracy of that timer would depend on how late in the request pipeline it gets replaced. But it should be fine to use it with the header handler plus its defer option.

1 Like

Alright thanks guys, I’ve opened a feature request here.

1 Like

Pull request opened:

Please give it a try. :slight_smile: Thanks!

(Also, X- prefixes on headers have long been deprecated; I recommend stripping them.)

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.