From: Aleksandar Lazic Date: Thu, 3 Dec 2026 20:00:18 +0000 (+0100) Subject: MINOR: haterm: report the real times in the X-req and X-rsp headers X-Git-Url: https://git.kaiwu.me/web/http/static/ngx_http_proxy_module.html?a=commitdiff_plain;h=f95abc4d4a655df2955424ed912085636e5a4f80;p=haproxy.git MINOR: haterm: report the real times in the X-req and X-rsp headers The X-req and X-rsp headers inherited from httpterm always reported "time=0 ms" and "time=0 ms (0 real)", marked as TODO in the code. This makes them useless to see how long a request took to arrive or how long haterm really waited before responding. The dates of the stream are now set using now_ns. X-req reports the time from the stream creation to the end of the request, which covers the upload of the request body, or -1 when the body is drained after the response (?A=1). X-rsp reports the delay requested with ?t= followed by the one really spent waiting. Both are in microseconds ("us") since most responses take less than a millisecond. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Aleksandar Lazic --- diff --git a/src/haterm.c b/src/haterm.c index 26eddc1ea..9b78967ad 100644 --- a/src/haterm.c +++ b/src/haterm.c @@ -273,9 +273,10 @@ static int hstream_htx_buf_rcv(struct connection *conn, struct hstream *hs) read = CALL_MUX_WITH_RET(conn->mux, rcv_buf(hs->sc, &hs->req, max, 0)); cur_read += read; if (!htx_expect_more(htxbuf(&hs->req))) { - fin = 1; - hs->flags |= HS_ST_HTTP_EOM_RCVD; - break; + fin = 1; + hs->flags |= HS_ST_HTTP_EOM_RCVD; + hs->req_eom_ns = now_ns; + break; } if (!read) @@ -628,6 +629,7 @@ static int hstream_build_http_resp(struct hstream *hs) struct htx *htx; unsigned int flags = HTX_SL_F_IS_RESP | HTX_SL_F_XFER_LEN | (!(hs->flags & HS_ST_OPT_CHUNK_RES) ? HTX_SL_F_CLEN : 0); struct htx_sl *sl; + ullong wait_us; char *end; TRACE_ENTER(HS_EV_HSTRM_RESP, hs); @@ -636,6 +638,7 @@ static int hstream_build_http_resp(struct hstream *hs) end = ultoa_o(hs->req_code, trash.area, trash.size); if (!end) goto err; + buf = hstream_get_buf(hs, &hs->res); if (!buf) { TRACE_ERROR("could not allocate response buffer", HS_EV_HSTRM_RESP, hs); @@ -679,45 +682,107 @@ static int hstream_build_http_resp(struct hstream *hs) goto err; } - /* XXX TODO time? XXX */ + /* time from the stream creation to the end of the request, in + * microseconds, or -1 if the request was not fully received yet + * (e.g. its body is drained after the response with ?A=1). + */ chunk_reset(&trash); - if (!chunk_strcat(&trash, "time=0 ms") || - !htx_add_header(htx, ist("X-req"), ist2(trash.area, trash.data))) { + if (!chunk_strcat(&trash, "time=")) { + TRACE_ERROR("could not build x-req HTX header", HS_EV_HSTRM_RESP, hs); + goto err; + } + + if (hs->flags & HS_ST_HTTP_EOM_RCVD) { + end = ulltoa((hs->req_eom_ns - hs->accept_ns) / 1000, + trash.area + trash.data, trash.size - trash.data); + if (!end) + goto err; + + trash.data = end - trash.area; + } + else if (!chunk_strcat(&trash, "-1")) { + TRACE_ERROR("could not build x-req HTX header", HS_EV_HSTRM_RESP, hs); + goto err; + } + + if (!chunk_strcat(&trash, " us")) { + TRACE_ERROR("could not build x-req HTX header", HS_EV_HSTRM_RESP, hs); + goto err; + } + + if (!htx_add_header(htx, ist("X-req"), ist2(trash.area, trash.data))) { TRACE_ERROR("could not add x-req HTX header", HS_EV_HSTRM_RESP, hs); - goto err; + goto err; } - /* XXX TODO time? XXX */ chunk_reset(&trash); if (!chunk_strcat(&trash, "id=dummy, code=")) { TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs); goto err; } + end = ultoa_o(hs->req_code, trash.area + trash.data, trash.size - trash.data); if (!end) goto err; + trash.data = end - trash.area; if (!chunk_strcat(&trash, ((hs->flags & HS_ST_OPT_NO_CACHE) ? ", cache=0" : ", cache=1"))) { TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs); goto err; } + if ((hs->flags & HS_ST_OPT_CHUNK_RES) && !chunk_strcat(&trash, ", chunked,")) { TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs); goto err; } + if (!chunk_strcat(&trash, " size=")) { TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs); goto err; } + end = ultoa_o(hs->req_size, trash.area + trash.data, trash.size - trash.data); if (!end) goto err; + trash.data = end - trash.area; + /* the delay requested with ?t= followed by the one really spent + * waiting, both in microseconds. + */ + if (!chunk_strcat(&trash, ", time=")) { + TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs); + goto err; + } + + wait_us = 0; + if (tick_isset(hs->res_wait)) + wait_us = (ullong)TICKS_TO_MS(hs->res_wait) * 1000; + end = ulltoa(wait_us, trash.area + trash.data, trash.size - trash.data); + if (!end) + goto err; - if (!chunk_strcat(&trash, ", time=0 ms (0 real)") || - !htx_add_header(htx, ist("X-rsp"), ist2(trash.area, trash.data))) { + trash.data = end - trash.area; + if (!chunk_strcat(&trash, " us (")) { + TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs); + goto err; + } + + wait_us = 0; + if (tick_isset(hs->res_time)) + wait_us = (now_ns - hs->wait_ns) / 1000; + end = ulltoa(wait_us, trash.area + trash.data, trash.size - trash.data); + if (!end) + goto err; + + trash.data = end - trash.area; + if (!chunk_strcat(&trash, " real)")) { + TRACE_ERROR("could not build x-rsp HTX header", HS_EV_HSTRM_RESP, hs); + goto err; + } + + if (!htx_add_header(htx, ist("X-rsp"), ist2(trash.area, trash.data))) { TRACE_ERROR("could not add x-rsp HTX header", HS_EV_HSTRM_RESP, hs); - goto err; + goto err; } if (!htx_add_endof(htx, HTX_BLK_EOH)) { @@ -988,6 +1053,7 @@ static struct task *process_hstream(struct task *t, void *context, unsigned int /* The request body has always been fully received */ TRACE_STATE("no more expected data", HS_EV_HSTRM_RESP, hs); hs->flags |= HS_ST_HTTP_EOM_RCVD; + hs->req_eom_ns = now_ns; } if (hstream_must_drain(hs)) { @@ -1002,6 +1068,7 @@ static struct task *process_hstream(struct task *t, void *context, unsigned int if (tick_isset(hs->res_wait)) { TRACE_STATE("task scheduled", HS_EV_HSTRM_IO_CB, hs); hs->res_time = tick_add(now_ms, hs->res_wait); + hs->wait_ns = now_ns; task_schedule(t, hs->res_time); goto leave; } @@ -1029,6 +1096,7 @@ static struct task *process_hstream(struct task *t, void *context, unsigned int if (tick_isset(hs->res_wait) && !tick_isset(hs->res_time)) { TRACE_STATE("task scheduled", HS_EV_HSTRM_IO_CB, hs); hs->res_time = tick_add(now_ms, hs->res_wait); + hs->wait_ns = now_ns; task_schedule(t, hs->res_time); goto leave; } @@ -1148,6 +1216,9 @@ void *hstream_new(struct session *sess, struct stconn *sc, struct buffer *input) hs->res_wait = TICK_ETERNITY; hs->res_time = TICK_ETERNITY; hs->req_meth = HTTP_METH_OTHER; + hs->accept_ns = now_ns; + hs->req_eom_ns = 0; + hs->wait_ns = 0; if (sc_conn(sc)) { const struct mux_ops *mux = sc_mux_ops(sc);