#!/bin/bash # Wird von tests/run.sh eingebunden: eine Zeile je bedienter Anfrage. # # Das Request-Log ist die einzige Stelle, an der eine einzelne Anfrage vollständig # steht: Status, Dauer, Wartezeit, Regel, Cache-Quelle, Wiederholungen, Tokens, Kosten. # Prometheus kann das nicht (zu viele Dimensionen), journald nur über mehrere Zeilen. echo echo "== 35. Anfrage-Log: eine Zeile, alle Felder ==" LPORT=$((PORT+210)) LSTUB=$((PORT+211)) LLED="$TMP/reqlog.jsonl" cat > "$TMP/lstub.py" <<'PY' import http.server, json, sys class H(http.server.BaseHTTPRequestHandler): def log_message(self, *a): pass def do_POST(self): n = int(self.headers.get("Content-Length", "0")) self.rfile.read(n) out = json.dumps({"id": "l", "choices": [{"index": 0, "message": {"role": "assistant", "content": "ok"}}], "usage": {"prompt_tokens": 900, "completion_tokens": 120, "prompt_tokens_details": {"cached_tokens": 400}}}).encode() self.send_response(200) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(out))) self.end_headers() self.wfile.write(out) http.server.HTTPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever() PY python3 "$TMP/lstub.py" "$LSTUB" & PIDS+=($!) sleep 0.6 cat > "$TMP/lprices.conf" <<'CFG' # test/log in cached out test/log 0.30 0.006 1.20 CFG cat > "$TMP/reqlog.conf" < "$TMP/reqlog.log" 2>&1 & PIDS+=($!) wait_port "$LPORT" 10 || bad "the request-log instance did not come up" LBODY='{"model":"test/log","messages":[{"role":"user","content":"eine frage"}],"max_tokens":5,"temperature":0}' lask() { curl -s -m 10 -o /dev/null -X POST -H 'Content-Type: application/json' -d "$LBODY" \ "http://127.0.0.1:$LPORT/v1/chat/completions" } lask # miss: bezahlt beim Anbieter lask # hit: aus dem eigenen Cache logfield() { # logfield python3 - "$LLED" "$1" "$2" <<'PY' import json, sys lines = [l for l in open(sys.argv[1]) if l.strip()] idx = int(sys.argv[2]) print(json.loads(lines[idx]).get(sys.argv[3], "")) PY } nlines=$(python3 -c "print(sum(1 for l in open('$LLED') if l.strip()))") [ "$nlines" = "2" ] && ok "two served requests, two lines — no line per event, one per request" \ || bad "expected 2 log lines, found $nlines" [ "$(logfield 0 status)" = "200" ] && ok "the line carries the status" \ || bad "status missing: $(logfield 0 status)" [ "$(logfield 0 cache)" = "miss" ] && ok "the paid request says cache=miss" \ || bad "first line is not a miss: $(logfield 0 cache)" [ "$(logfield 1 cache)" = "hit" ] && ok "the cached request says cache=hit" \ || bad "second line is not a hit: $(logfield 1 cache)" [ "$(logfield 1 tokens_in)" = "0" ] && ok "a cache hit costs no tokens" \ || bad "cache hit reported $(logfield 1 tokens_in) input tokens" [ "$(logfield 0 tokens_in)" = "900" ] && ok "the paid request carries the provider's token numbers" \ || bad "tokens missing: $(logfield 0 tokens_in)" # Die Regelzeile wird nicht geraten: sie muss die Zeile der passenden Regel sein. rule_at=$(grep -n '^req for "\*"' "$TMP/reqlog.conf" | cut -d: -f1) [ "$(logfield 0 rule_line)" = "$rule_at" ] \ && ok "the line names the rule that matched (line $rule_at)" \ || bad "rule_line was $(logfield 0 rule_line), the rule sits on line $rule_at" [ "$(logfield 1 rule_line)" = "0" ] && ok "a cache hit names no rule — none was used" \ || bad "cache hit claims rule_line $(logfield 1 rule_line)" # latency_ms muss eine Zahl sein und zur Antwort passen; queue_ms ist bei freien Plätzen 0. python3 - "$LLED" <<'PY' import json, sys lines = [json.loads(l) for l in open(sys.argv[1]) if l.strip()] d = lines[0] ok = (isinstance(d.get("latency_ms"), (int, float)) and d["latency_ms"] >= 0 and d.get("upstream_ms", -1) >= 0 and d.get("queue_ms") == 0 and d.get("retries") == 0 and d.get("cost_usd", 0) > 0) sys.exit(0 if ok else 1) PY [ $? -eq 0 ] && ok "latency_ms, upstream_ms, queue_ms, retries and cost all present and plausible" \ || bad "a timing or cost field is missing or implausible" # Die Kosten aus dem Log sind dieselben, die /spend summiert. total=$(curl -s -m 5 "http://127.0.0.1:$LPORT/spend" | python3 -c "import sys,json;print('%.6f'%json.load(sys.stdin)['total_usd'])") logged=$(python3 - "$LLED" <<'PY' import json, sys print("%.6f" % sum(json.loads(l).get("cost_usd", 0) for l in open(sys.argv[1]) if l.strip())) PY ) [ "$total" = "$logged" ] && ok "/spend sums the same numbers the ledger writes ($total)" \ || bad "/spend says $total, the ledger says $logged"