A well-formed X-Request-ID from a proxy is kept, otherwise a UUIDv7 is generated. The ID is echoed in the response header, added to every log record written with the request context and returned in API error bodies. Closes #44 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
34 lines
844 B
Go
34 lines
844 B
Go
package platform
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/requestid"
|
|
)
|
|
|
|
func TestLoggerAddsRequestID(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
log, _ := newLogger(&buf, "test")
|
|
log = log.With("extra", 1)
|
|
|
|
log.InfoContext(requestid.NewContext(context.Background(), "abc"), "with id")
|
|
log.InfoContext(context.Background(), "without id")
|
|
|
|
dec := json.NewDecoder(&buf)
|
|
for _, want := range []any{"abc", nil} {
|
|
var rec map[string]any
|
|
if err := dec.Decode(&rec); err != nil {
|
|
t.Fatalf("decoding log line: %v", err)
|
|
}
|
|
if got := rec["request_id"]; got != want {
|
|
t.Errorf("%q: request_id = %v, want %v", rec["msg"], got, want)
|
|
}
|
|
if rec["extra"] != float64(1) || rec["component"] != "test" {
|
|
t.Errorf("%q: lost attributes from With: %v", rec["msg"], rec)
|
|
}
|
|
}
|
|
}
|