mirror of
https://github.com/foomo/keel.git
synced 2025-10-16 12:35:34 +00:00
feat: extract request id provider
This commit is contained in:
parent
706738579e
commit
f8c7405f20
@ -53,8 +53,11 @@ func main() {
|
||||
|
||||
svr.AddService(
|
||||
keel.NewServiceHTTP(l, "demo", "localhost:8080", svs,
|
||||
// add middleware
|
||||
middleware.RequestID(),
|
||||
// add middleware
|
||||
middleware.SessionID(),
|
||||
// add middleware
|
||||
middleware.TrackingID(),
|
||||
),
|
||||
)
|
||||
|
||||
@ -3,34 +3,28 @@ package middleware
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"go.uber.org/zap"
|
||||
|
||||
keelhttpcontext "github.com/foomo/keel/net/http/context"
|
||||
"github.com/foomo/keel/net/http/provider"
|
||||
)
|
||||
|
||||
type (
|
||||
RequestIDOptions struct {
|
||||
Generator RequestIDGenerator
|
||||
Provider provider.RequestID
|
||||
RequestHeader []string
|
||||
ResponseHeader string
|
||||
SetRequestHeader bool
|
||||
SetResponseHeader bool
|
||||
SetContext bool
|
||||
}
|
||||
RequestIDOption func(*RequestIDOptions)
|
||||
RequestIDGenerator func() string
|
||||
RequestIDOption func(*RequestIDOptions)
|
||||
)
|
||||
|
||||
// DefaultRequestIDGenerator function
|
||||
func DefaultRequestIDGenerator() string {
|
||||
return uuid.New().String()
|
||||
}
|
||||
|
||||
// GetDefaultRequestIDOptions returns the default options
|
||||
func GetDefaultRequestIDOptions() RequestIDOptions {
|
||||
return RequestIDOptions{
|
||||
Generator: DefaultRequestIDGenerator,
|
||||
Provider: provider.DefaultRequestID,
|
||||
RequestHeader: []string{"X-Request-ID", "Cf-Ray"},
|
||||
ResponseHeader: "X-Request-ID",
|
||||
SetRequestHeader: true,
|
||||
@ -68,9 +62,9 @@ func RequestIDWithSetResponseHeader(v bool) RequestIDOption {
|
||||
}
|
||||
|
||||
// RequestIDWithGenerator middleware option
|
||||
func RequestIDWithGenerator(v RequestIDGenerator) RequestIDOption {
|
||||
func RequestIDWithGenerator(v provider.RequestID) RequestIDOption {
|
||||
return func(o *RequestIDOptions) {
|
||||
o.Generator = v
|
||||
o.Provider = v
|
||||
}
|
||||
}
|
||||
|
||||
@ -103,7 +97,7 @@ func RequestIDWithOptions(opts RequestIDOptions) Middleware {
|
||||
}
|
||||
}
|
||||
if requestID == "" {
|
||||
requestID = opts.Generator()
|
||||
requestID = opts.Provider()
|
||||
}
|
||||
if requestID != "" && opts.SetContext {
|
||||
r = r.WithContext(keelhttpcontext.SetRequestID(r.Context(), requestID))
|
||||
|
||||
12
net/http/provider/requestidprovider.go
Normal file
12
net/http/provider/requestidprovider.go
Normal file
@ -0,0 +1,12 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type RequestID func() string
|
||||
|
||||
// DefaultRequestID function
|
||||
func DefaultRequestID() string {
|
||||
return uuid.New().String()
|
||||
}
|
||||
@ -6,20 +6,23 @@ import (
|
||||
"go.uber.org/zap"
|
||||
|
||||
keelhttpcontext "github.com/foomo/keel/net/http/context"
|
||||
"github.com/foomo/keel/net/http/provider"
|
||||
)
|
||||
|
||||
type (
|
||||
RequestIDOptions struct {
|
||||
Header string
|
||||
Header string
|
||||
Provider provider.RequestID
|
||||
SetHeader bool
|
||||
}
|
||||
RequestIDOption func(*RequestIDOptions)
|
||||
RequestIDGenerator func() string
|
||||
RequestIDOption func(*RequestIDOptions)
|
||||
)
|
||||
|
||||
// GetDefaultRequestIDOptions returns the default options
|
||||
func GetDefaultRequestIDOptions() RequestIDOptions {
|
||||
return RequestIDOptions{
|
||||
Header: "X-Request-ID",
|
||||
Header: "X-Request-ID",
|
||||
Provider: provider.DefaultRequestID,
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,6 +33,13 @@ func RequestIDWithHeader(v string) RequestIDOption {
|
||||
}
|
||||
}
|
||||
|
||||
// RequestIDWithProvider middleware option
|
||||
func RequestIDWithProvider(v provider.RequestID) RequestIDOption {
|
||||
return func(o *RequestIDOptions) {
|
||||
o.Provider = v
|
||||
}
|
||||
}
|
||||
|
||||
// RequestID returns a RoundTripper which prints out the request & response object
|
||||
func RequestID(opts ...RequestIDOption) RoundTripware {
|
||||
o := GetDefaultRequestIDOptions()
|
||||
@ -41,8 +51,15 @@ func RequestID(opts ...RequestIDOption) RoundTripware {
|
||||
return func(l *zap.Logger, next Handler) Handler {
|
||||
return func(r *http.Request) (*http.Response, error) {
|
||||
if value := r.Header.Get(o.Header); value == "" {
|
||||
var requestID string
|
||||
if value, ok := keelhttpcontext.GetRequestID(r.Context()); ok && value != "" {
|
||||
r.Header.Set(o.Header, value)
|
||||
requestID = value
|
||||
}
|
||||
if requestID == "" {
|
||||
requestID = o.Provider()
|
||||
}
|
||||
if requestID != "" {
|
||||
r.Header.Set(o.Header, requestID)
|
||||
}
|
||||
}
|
||||
return next(r)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user