diff --git a/src/junction.c b/src/junction.c index 0f65957..b78fb6d 100644 --- a/src/junction.c +++ b/src/junction.c @@ -2596,6 +2596,40 @@ static int read_response(j_conn *up, j_upstream_resp *r, double idle_s, double d return 0; } +/* Ein 429/5xx nennt im Rumpf fast immer den Grund ("monthly usage limit", + * "context exhausted", ...). Ohne ihn bleibt im Log nur der Status, und die Ursache + * muss von Hand nachgestellt werden. Best effort: kurze Frist, gekappte Länge, + * Steuerzeichen werden zu Leerzeichen. */ +static void log_upstream_error_body(j_conn *up, j_upstream_resp *r, const char *endpoint) +{ + char body_err[96]; + char *body = NULL; + size_t len = 0; + + if (read_full_body(up, &r->buf, r->body_off, r->chunked, r->content_length, + 0.5, now_s() + 1.0, &body, &len, body_err, sizeof body_err) != 0) + return; + if (body != NULL && len > 0) { + char snip[241]; + size_t n = len > 200 ? 200 : len, o = 0, spaces = 0; + for (size_t i = 0; i < n; i++) { + unsigned char c = (unsigned char)body[i]; + if (c >= 32 && c < 127) { + snip[o++] = (char)c; + spaces = c == ' ' ? spaces + 1 : 0; + if (spaces > 1) o--; /* collapse runs, keep the line short */ + } else if (o > 0 && snip[o - 1] != ' ') { + snip[o++] = ' '; + spaces = 1; + } + } + while (o > 0 && snip[o - 1] == ' ') o--; + snip[o] = '\0'; + jlog("upstream_error", "endpoint=%s status=%d body=%s", endpoint, r->status, snip); + } + free(body); +} + /* Returns 0 = served, -1 = endpoint failed (fail over), -2 = bytes already sent to the * client, so failing over is impossible. */ static int forward_step(j_conn *client, j_endpoint *ep, const j_req *rq, @@ -2814,6 +2848,7 @@ static int forward_step(j_conn *client, j_endpoint *ep, const j_req *rq, } if (r.status >= 500 || r.status == 429) { t_heartbeat = NULL; + log_upstream_error_body(&up, &r, ep->name); snprintf(err, errlen, "upstream_status_%d", r.status); /* Was der Anbieter selbst sagt, ist besser als jede eigene Wartezeit: bei 429 * und 503 steht dort oft, wann es wieder geht. Nur Sekunden werden gelesen — diff --git a/tests/retry-after.sh b/tests/retry-after.sh index d9a4fc5..79e0dda 100755 --- a/tests/retry-after.sh +++ b/tests/retry-after.sh @@ -169,3 +169,8 @@ R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' -d "$RABODY" \ contains "$R" "cooling" "the next request skips an endpoint benched by Retry-After" contains "$(grep -o 'reason=retry_after reset_s=[0-9]*' "$TMP/rab.log" | head -1)" \ "reset_s=30" "the bench uses the provider's reset time, not the 1 s cooldown" + +# Der Status allein sagt nicht, warum der Anbieter ablehnt. Der Rumpf nennt den Grund +# ("monthly usage limit", ...) und gehört ins Log, sonst muss man ihn von Hand holen. +contains "$(grep 'upstream_error' "$TMP/rab.log" | head -1)" \ + 'body={"error":"not serving yet"}' "the provider's error body is in the log"