Context-based logging with request ID (#6899)

This commit is contained in:
Aleksey Kosov
2025-06-20 16:23:53 +03:00
committed by GitHub
parent a72c442945
commit 90c128e7a6
8 changed files with 302 additions and 48 deletions

View File

@@ -0,0 +1,26 @@
package request_id
import (
"context"
"net/http"
)
const AmzRequestIDHeader = "x-amz-request-id"
func Set(ctx context.Context, id string) context.Context {
return context.WithValue(ctx, AmzRequestIDHeader, id)
}
func Get(ctx context.Context) string {
if ctx == nil {
return ""
}
id, _ := ctx.Value(AmzRequestIDHeader).(string)
return id
}
func InjectToRequest(ctx context.Context, req *http.Request) {
if req != nil {
req.Header.Set(AmzRequestIDHeader, Get(ctx))
}
}