feat(platform): tag requests, logs and api errors with a request id
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
This commit is contained in:
1 parent
b6843330a8
commit
c485d1a96f
6 files changed
+191
-12
No files matched your search
+11
-6
@@ -14,6 +14,7 @@ import (
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/auth"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/requestid"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/sealed"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
||||
@@ -130,17 +131,20 @@ func (s *Server) setLocale(c *gin.Context) {
|
||||
}
|
||||
|
||||
type apiError struct {
|
||||
Error string `json:"error"`
|
||||
Message string `json:"message"`
|
||||
Fields map[string]string `json:"fields,omitempty"`
|
||||
Error string `json:"error"`
|
||||
Message string `json:"message"`
|
||||
Fields map[string]string `json:"fields,omitempty"`
|
||||
RequestID string `json:"request_id,omitempty"`
|
||||
}
|
||||
|
||||
// fail aborts with a machine-readable code and a message translated from
|
||||
// the catalog entry "error.<code>". kv fills placeholders in that message.
|
||||
func fail(c *gin.Context, status int, code string, kv ...any) {
|
||||
ctx := c.Request.Context()
|
||||
c.AbortWithStatusJSON(status, apiError{
|
||||
Error: code,
|
||||
Message: i18n.T(c.Request.Context(), "error."+code, kv...),
|
||||
Error: code,
|
||||
Message: i18n.T(ctx, "error."+code, kv...),
|
||||
RequestID: requestid.FromContext(ctx),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -154,7 +158,8 @@ func bind(c *gin.Context, dst any) bool {
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
e := apiError{Error: "invalid_input", Message: i18n.T(c.Request.Context(), "error.invalid_input")}
|
||||
ctx := c.Request.Context()
|
||||
e := apiError{Error: "invalid_input", Message: i18n.T(ctx, "error.invalid_input"), RequestID: requestid.FromContext(ctx)}
|
||||
var ve validator.ValidationErrors
|
||||
if errors.As(err, &ve) {
|
||||
e.Fields = map[string]string{}
|
||||
|
||||
Reference in new issue
Block a user