mirror of
https://github.com/foomo/keel.git
synced 2025-10-16 12:35:34 +00:00
refactor: move labeler into separate package
This commit is contained in:
parent
da9e516c94
commit
91f39d3f26
3
go.sum
3
go.sum
@ -121,8 +121,6 @@ github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
|
||||
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
|
||||
github.com/felixge/httpsnoop v1.0.2 h1:+nS9g82KMXccJ/wp0zyRW9ZBHFETmMGtkk+2CTTrW4o=
|
||||
github.com/felixge/httpsnoop v1.0.2/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
|
||||
github.com/foomo/gotsrpc/v2 v2.7.1-0.20230710064745-092c92a2fd86 h1:g1j/myDQfjEPKOuDZi7uotQ6/TsGDyjnFVZ7pN7uNsE=
|
||||
github.com/foomo/gotsrpc/v2 v2.7.1-0.20230710064745-092c92a2fd86/go.mod h1:n5SiKVNCZ7Tob6wcROWT5Sx1i/W42+ErpTbNqT3etM8=
|
||||
github.com/foomo/gotsrpc/v2 v2.7.1-0.20230710070331-3d520949b454 h1:VXL2V7Gi6MhEgHRqo6E+ycAdP7HHaNBBdfhzfd/isLY=
|
||||
github.com/foomo/gotsrpc/v2 v2.7.1-0.20230710070331-3d520949b454/go.mod h1:n5SiKVNCZ7Tob6wcROWT5Sx1i/W42+ErpTbNqT3etM8=
|
||||
github.com/foomo/opentelemetry-go-contrib/instrumentation/net/http/otelhttp v0.32.1-0.20220517120905-10e2553b9bac h1:z8UXpHe2Hb9IzRawoeT49eUcxgGUY6g8Emj8TpUWl+4=
|
||||
@ -221,7 +219,6 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
|
||||
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
|
||||
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
|
||||
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
|
||||
github.com/google/martian/v3 v3.2.1/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk=
|
||||
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||
github.com/google/pprof v0.0.0-20191218002539-d4f498aebedc/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/foomo/gotsrpc/v2"
|
||||
httplog "github.com/foomo/keel/net/http/log"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"go.uber.org/zap"
|
||||
@ -41,7 +42,7 @@ func Telemetry() middleware.Middleware {
|
||||
next.ServeHTTP(w, r)
|
||||
|
||||
if stats, ok := gotsrpc.GetStatsForRequest(r); ok {
|
||||
if labeler, ok := middleware.LoggerLabelerFromRequest(r); ok {
|
||||
if labeler, ok := httplog.LabelerFromRequest(r); ok {
|
||||
labeler.Add(
|
||||
zap.String(defaultGOTSRPCFunctionLabel, stats.Func),
|
||||
zap.String(defaultGOTSRPCServiceLabel, stats.Service),
|
||||
|
||||
27
net/http/log/labeler.go
Normal file
27
net/http/log/labeler.go
Normal file
@ -0,0 +1,27 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/foomo/keel/log"
|
||||
)
|
||||
|
||||
const loggerLabelerContextKey log.LabelerContextKey = "github.com/foomo/keel/net/log.Labeler"
|
||||
|
||||
func LabelerFromContext(ctx context.Context) (*log.Labeler, bool) {
|
||||
return log.LabelerFromContext(ctx, loggerLabelerContextKey)
|
||||
}
|
||||
|
||||
func LabelerFromRequest(r *http.Request) (*log.Labeler, bool) {
|
||||
return log.LabelerFromContext(r.Context(), loggerLabelerContextKey)
|
||||
}
|
||||
|
||||
func InjectLabelerIntoContext(ctx context.Context) (context.Context, *log.Labeler) {
|
||||
return log.InjectLabeler(ctx, loggerLabelerContextKey)
|
||||
}
|
||||
|
||||
func InjectLabelerIntoRequest(r *http.Request) (*http.Request, *log.Labeler) {
|
||||
ctx, labeler := InjectLabelerIntoContext(r.Context())
|
||||
return r.WithContext(ctx), labeler
|
||||
}
|
||||
@ -1,18 +1,16 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
httplog "github.com/foomo/keel/net/http/log"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/foomo/keel/log"
|
||||
keeltime "github.com/foomo/keel/time"
|
||||
)
|
||||
|
||||
const loggerLabelerContextKey log.LabelerContextKey = "github.com/foomo/keel/net/middleware.Logger"
|
||||
|
||||
type (
|
||||
LoggerOptions struct {
|
||||
Message string
|
||||
@ -86,9 +84,7 @@ func LoggerWithOptions(opts LoggerOptions) Middleware {
|
||||
var labeler *log.Labeler
|
||||
|
||||
if labeler == nil && opts.InjectLabeler {
|
||||
var labelerCtx context.Context
|
||||
labelerCtx, labeler = log.InjectLabeler(r.Context(), loggerLabelerContextKey)
|
||||
r = r.WithContext(labelerCtx)
|
||||
r, labeler = httplog.InjectLabelerIntoRequest(r)
|
||||
}
|
||||
|
||||
next.ServeHTTP(wr, r)
|
||||
@ -114,11 +110,3 @@ func LoggerWithOptions(opts LoggerOptions) Middleware {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func LoggerLabelerFromContext(ctx context.Context) (*log.Labeler, bool) {
|
||||
return log.LabelerFromContext(ctx, loggerLabelerContextKey)
|
||||
}
|
||||
|
||||
func LoggerLabelerFromRequest(r *http.Request) (*log.Labeler, bool) {
|
||||
return log.LabelerFromContext(r.Context(), loggerLabelerContextKey)
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/foomo/keel/log"
|
||||
httplog "github.com/foomo/keel/net/http/log"
|
||||
"github.com/foomo/keel/net/http/middleware"
|
||||
keeltest "github.com/foomo/keel/test"
|
||||
"go.uber.org/zap"
|
||||
@ -57,7 +58,7 @@ func ExampleLoggerWithInjectLabeler() {
|
||||
keeltest.NewServiceHTTP(l, "demo", svs,
|
||||
func(l *zap.Logger, s string, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if labeler, ok := middleware.LoggerLabelerFromRequest(r); ok {
|
||||
if labeler, ok := httplog.LabelerFromRequest(r); ok {
|
||||
labeler.Add(zap.String("injected", "message"))
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
|
||||
@ -3,8 +3,7 @@ package httputils
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
httplog "github.com/foomo/keel/net/http/log"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/foomo/keel/log"
|
||||
@ -43,11 +42,13 @@ func NotFoundServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request,
|
||||
// ServerError http response
|
||||
func ServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, code int, err error) {
|
||||
if err != nil {
|
||||
l = log.WithError(l, err)
|
||||
if labeler, ok := otelhttp.LabelerFromContext(r.Context()); ok {
|
||||
labeler.Add(attribute.Bool("error", true))
|
||||
if labeler, ok := httplog.LabelerFromRequest(r); ok {
|
||||
labeler.Add(log.FErrorType(err), log.FError(err))
|
||||
} else {
|
||||
l = log.WithError(l, err)
|
||||
l = log.WithHTTPRequest(l, r)
|
||||
l.Error("http server error", log.FHTTPStatusCode(code))
|
||||
}
|
||||
log.WithHTTPRequest(l, r).Error("http server error", log.FHTTPStatusCode(code))
|
||||
// w.Header().Set(keelhttp.HeaderXError, err.Error()) TODO make configurable with better value
|
||||
http.Error(w, http.StatusText(code), code)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user