New logger for the Traefik logs
This commit is contained in:
parent
27c02b5a56
commit
56f7515ecd
297 changed files with 2337 additions and 1934 deletions
|
@ -10,11 +10,11 @@ import (
|
|||
|
||||
"github.com/mailgun/ttlmap"
|
||||
"github.com/opentracing/opentracing-go/ext"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
||||
"github.com/traefik/traefik/v2/pkg/log"
|
||||
"github.com/traefik/traefik/v2/pkg/middlewares"
|
||||
"github.com/traefik/traefik/v2/pkg/tracing"
|
||||
"github.com/vulcand/oxy/utils"
|
||||
"github.com/vulcand/oxy/v2/utils"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
|
@ -45,8 +45,10 @@ type rateLimiter struct {
|
|||
|
||||
// New returns a rate limiter middleware.
|
||||
func New(ctx context.Context, next http.Handler, config dynamic.RateLimit, name string) (http.Handler, error) {
|
||||
ctxLog := log.With(ctx, log.Str(log.MiddlewareName, name), log.Str(log.MiddlewareType, typeName))
|
||||
log.FromContext(ctxLog).Debug("Creating middleware")
|
||||
logger := middlewares.GetLogger(ctx, name, typeName)
|
||||
logger.Debug().Msg("Creating middleware")
|
||||
|
||||
ctxLog := logger.WithContext(ctx)
|
||||
|
||||
if config.SourceCriterion == nil ||
|
||||
config.SourceCriterion.IPStrategy == nil &&
|
||||
|
@ -122,19 +124,19 @@ func (rl *rateLimiter) GetTracingInformation() (string, ext.SpanKindEnum) {
|
|||
return rl.name, tracing.SpanKindNoneEnum
|
||||
}
|
||||
|
||||
func (rl *rateLimiter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := middlewares.GetLoggerCtx(r.Context(), rl.name, typeName)
|
||||
logger := log.FromContext(ctx)
|
||||
func (rl *rateLimiter) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
logger := middlewares.GetLogger(req.Context(), rl.name, typeName)
|
||||
ctx := logger.WithContext(req.Context())
|
||||
|
||||
source, amount, err := rl.sourceMatcher.Extract(r)
|
||||
source, amount, err := rl.sourceMatcher.Extract(req)
|
||||
if err != nil {
|
||||
logger.Errorf("could not extract source of request: %v", err)
|
||||
http.Error(w, "could not extract source of request", http.StatusInternalServerError)
|
||||
logger.Error().Err(err).Msg("Could not extract source of request")
|
||||
http.Error(rw, "could not extract source of request", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if amount != 1 {
|
||||
logger.Infof("ignoring token bucket amount > 1: %d", amount)
|
||||
logger.Info().Msgf("ignoring token bucket amount > 1: %d", amount)
|
||||
}
|
||||
|
||||
var bucket *rate.Limiter
|
||||
|
@ -148,8 +150,8 @@ func (rl *rateLimiter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
// because we want to update the expiryTime everytime we get the source,
|
||||
// as the expiryTime is supposed to reflect the activity (or lack thereof) on that source.
|
||||
if err := rl.buckets.Set(source, bucket, rl.ttl); err != nil {
|
||||
logger.Errorf("could not insert/update bucket: %v", err)
|
||||
http.Error(w, "could not insert/update bucket", http.StatusInternalServerError)
|
||||
logger.Error().Err(err).Msg("Could not insert/update bucket")
|
||||
http.Error(rw, "could not insert/update bucket", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -159,19 +161,19 @@ func (rl *rateLimiter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
// However, for now we take advantage of this behavior to provide the no-limit ratelimiter when config.Average is 0.
|
||||
res := bucket.Reserve()
|
||||
if !res.OK() {
|
||||
http.Error(w, "No bursty traffic allowed", http.StatusTooManyRequests)
|
||||
http.Error(rw, "No bursty traffic allowed", http.StatusTooManyRequests)
|
||||
return
|
||||
}
|
||||
|
||||
delay := res.Delay()
|
||||
if delay > rl.maxDelay {
|
||||
res.Cancel()
|
||||
rl.serveDelayError(ctx, w, delay)
|
||||
rl.serveDelayError(ctx, rw, delay)
|
||||
return
|
||||
}
|
||||
|
||||
time.Sleep(delay)
|
||||
rl.next.ServeHTTP(w, r)
|
||||
rl.next.ServeHTTP(rw, req)
|
||||
}
|
||||
|
||||
func (rl *rateLimiter) serveDelayError(ctx context.Context, w http.ResponseWriter, delay time.Duration) {
|
||||
|
@ -180,6 +182,6 @@ func (rl *rateLimiter) serveDelayError(ctx context.Context, w http.ResponseWrite
|
|||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
|
||||
if _, err := w.Write([]byte(http.StatusText(http.StatusTooManyRequests))); err != nil {
|
||||
log.FromContext(ctx).Errorf("could not serve 429: %v", err)
|
||||
log.Ctx(ctx).Error().Err(err).Msg("Could not serve 429")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue