mirror of
https://github.com/foomo/keel.git
synced 2025-10-16 12:35:34 +00:00
feat: retrieve values from context
This commit is contained in:
parent
d8e1741be1
commit
e6596bf2d1
@ -4,6 +4,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/foomo/keel"
|
"github.com/foomo/keel"
|
||||||
|
"github.com/foomo/keel/log"
|
||||||
keelhttp "github.com/foomo/keel/net/http"
|
keelhttp "github.com/foomo/keel/net/http"
|
||||||
"github.com/foomo/keel/net/http/middleware"
|
"github.com/foomo/keel/net/http/middleware"
|
||||||
"github.com/foomo/keel/net/http/roundtripware"
|
"github.com/foomo/keel/net/http/roundtripware"
|
||||||
@ -25,6 +26,7 @@ func main() {
|
|||||||
// create demo service
|
// create demo service
|
||||||
svs := http.NewServeMux()
|
svs := http.NewServeMux()
|
||||||
svs.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
svs.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
l := log.WithHTTPRequest(l, r)
|
||||||
// send internal http request
|
// send internal http request
|
||||||
if req, err := http.NewRequestWithContext(r.Context(), http.MethodGet, "http://localhost:8080/internal", nil); err != nil {
|
if req, err := http.NewRequestWithContext(r.Context(), http.MethodGet, "http://localhost:8080/internal", nil); err != nil {
|
||||||
httputils.InternalServerError(l, w, r, err)
|
httputils.InternalServerError(l, w, r, err)
|
||||||
@ -35,10 +37,12 @@ func main() {
|
|||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
_, _ = w.Write([]byte(r.Header.Get(keelhttp.HeaderXRequestID) + " - " + resp.Header.Get(keelhttp.HeaderXRequestID)))
|
_, _ = w.Write([]byte(r.Header.Get(keelhttp.HeaderXRequestID) + " - " + resp.Header.Get(keelhttp.HeaderXRequestID)))
|
||||||
|
log.WithHTTPRequestOut(l, req).Info("sent internal request")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
svs.HandleFunc("/internal", func(w http.ResponseWriter, r *http.Request) {
|
svs.HandleFunc("/internal", func(w http.ResponseWriter, r *http.Request) {
|
||||||
l.Info("internal: " + r.Header.Get(keelhttp.HeaderXRequestID))
|
l := log.WithHTTPRequest(l, r)
|
||||||
|
l.Info("handled internal request")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
10
log/with.go
10
log/with.go
@ -9,6 +9,8 @@ import (
|
|||||||
|
|
||||||
"go.opentelemetry.io/otel/trace"
|
"go.opentelemetry.io/otel/trace"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
|
keelhttpcontext "github.com/foomo/keel/net/http/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
func With(l *zap.Logger, fields ...zap.Field) *zap.Logger {
|
func With(l *zap.Logger, fields ...zap.Field) *zap.Logger {
|
||||||
@ -54,9 +56,13 @@ func WithHTTPRequest(l *zap.Logger, r *http.Request) *zap.Logger {
|
|||||||
}
|
}
|
||||||
if id := r.Header.Get("X-Request-ID"); id != "" {
|
if id := r.Header.Get("X-Request-ID"); id != "" {
|
||||||
fields = append(fields, FHTTPRequestID(id))
|
fields = append(fields, FHTTPRequestID(id))
|
||||||
|
} else if id, ok := keelhttpcontext.GetRequestID(r.Context()); ok && id != "" {
|
||||||
|
fields = append(fields, FHTTPRequestID(id))
|
||||||
}
|
}
|
||||||
if id := r.Header.Get("X-Session-ID"); id != "" {
|
if id := r.Header.Get("X-Session-ID"); id != "" {
|
||||||
fields = append(fields, FHTTPSessionID(id))
|
fields = append(fields, FHTTPSessionID(id))
|
||||||
|
} else if id, ok := keelhttpcontext.GetSessionID(r.Context()); ok && id != "" {
|
||||||
|
fields = append(fields, FHTTPSessionID(id))
|
||||||
}
|
}
|
||||||
if r.TLS != nil {
|
if r.TLS != nil {
|
||||||
fields = append(fields, FHTTPScheme("https"))
|
fields = append(fields, FHTTPScheme("https"))
|
||||||
@ -106,9 +112,13 @@ func WithHTTPRequestOut(l *zap.Logger, r *http.Request) *zap.Logger {
|
|||||||
}
|
}
|
||||||
if id := r.Header.Get("X-Request-ID"); id != "" {
|
if id := r.Header.Get("X-Request-ID"); id != "" {
|
||||||
fields = append(fields, FHTTPRequestID(id))
|
fields = append(fields, FHTTPRequestID(id))
|
||||||
|
} else if id, ok := keelhttpcontext.GetRequestID(r.Context()); ok && id != "" {
|
||||||
|
fields = append(fields, FHTTPRequestID(id))
|
||||||
}
|
}
|
||||||
if id := r.Header.Get("X-Session-ID"); id != "" {
|
if id := r.Header.Get("X-Session-ID"); id != "" {
|
||||||
fields = append(fields, FHTTPSessionID(id))
|
fields = append(fields, FHTTPSessionID(id))
|
||||||
|
} else if id, ok := keelhttpcontext.GetSessionID(r.Context()); ok && id != "" {
|
||||||
|
fields = append(fields, FHTTPSessionID(id))
|
||||||
}
|
}
|
||||||
if r.TLS != nil {
|
if r.TLS != nil {
|
||||||
fields = append(fields, FHTTPScheme("https"))
|
fields = append(fields, FHTTPScheme("https"))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user