From 28307268519fbc913704a1b1b25606d05f76e7a9 Mon Sep 17 00:00:00 2001 From: Sasha Kulbii Date: Thu, 26 Nov 2020 17:29:39 +0100 Subject: [PATCH 1/2] Add debug logging to indicate descriptor and limit Signed-off-by: Sasha Kulbii --- src/service/ratelimit.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/service/ratelimit.go b/src/service/ratelimit.go index 08b392d2..73506c9c 100644 --- a/src/service/ratelimit.go +++ b/src/service/ratelimit.go @@ -1,6 +1,7 @@ package ratelimit import ( + "fmt" "strings" "sync" @@ -113,7 +114,24 @@ func (this *service) shouldRateLimitWorker( limitsToCheck := make([]*config.RateLimit, len(request.Descriptors)) for i, descriptor := range request.Descriptors { + var descriptorEntryStrings []string + for _, descriptorEntry := range descriptor.GetEntries() { + descriptorEntryStrings = append( + descriptorEntryStrings, + fmt.Sprintf("(%s=%s)", descriptorEntry.Key, descriptorEntry.Value), + ) + } + logger.Debugf("got descriptor: %s", strings.Join(descriptorEntryStrings, ",")) limitsToCheck[i] = snappedConfig.GetLimit(ctx, request.Domain, descriptor) + if limitsToCheck[i] == nil { + logger.Debugf("descriptor does not match any limit, no limits applied") + } else { + logger.Debugf( + "applying limit: %d requests per %s", + limitsToCheck[i].Limit.RequestsPerUnit, + limitsToCheck[i].Limit.Unit.String(), + ) + } } responseDescriptorStatuses := this.cache.DoLimit(ctx, request, limitsToCheck) From 3c2603ae1147cc14cefc3ff29ed229a47d797caa Mon Sep 17 00:00:00 2001 From: Sasha Kulbii Date: Mon, 30 Nov 2020 11:49:50 +0100 Subject: [PATCH 2/2] Check for log level before computing descriptor strings for debug log Signed-off-by: Sasha Kulbii --- src/service/ratelimit.go | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/service/ratelimit.go b/src/service/ratelimit.go index 73506c9c..126bb776 100644 --- a/src/service/ratelimit.go +++ b/src/service/ratelimit.go @@ -114,23 +114,27 @@ func (this *service) shouldRateLimitWorker( limitsToCheck := make([]*config.RateLimit, len(request.Descriptors)) for i, descriptor := range request.Descriptors { - var descriptorEntryStrings []string - for _, descriptorEntry := range descriptor.GetEntries() { - descriptorEntryStrings = append( - descriptorEntryStrings, - fmt.Sprintf("(%s=%s)", descriptorEntry.Key, descriptorEntry.Value), - ) + if logger.IsLevelEnabled(logger.DebugLevel) { + var descriptorEntryStrings []string + for _, descriptorEntry := range descriptor.GetEntries() { + descriptorEntryStrings = append( + descriptorEntryStrings, + fmt.Sprintf("(%s=%s)", descriptorEntry.Key, descriptorEntry.Value), + ) + } + logger.Debugf("got descriptor: %s", strings.Join(descriptorEntryStrings, ",")) } - logger.Debugf("got descriptor: %s", strings.Join(descriptorEntryStrings, ",")) limitsToCheck[i] = snappedConfig.GetLimit(ctx, request.Domain, descriptor) - if limitsToCheck[i] == nil { - logger.Debugf("descriptor does not match any limit, no limits applied") - } else { - logger.Debugf( - "applying limit: %d requests per %s", - limitsToCheck[i].Limit.RequestsPerUnit, - limitsToCheck[i].Limit.Unit.String(), - ) + if logger.IsLevelEnabled(logger.DebugLevel) { + if limitsToCheck[i] == nil { + logger.Debugf("descriptor does not match any limit, no limits applied") + } else { + logger.Debugf( + "applying limit: %d requests per %s", + limitsToCheck[i].Limit.RequestsPerUnit, + limitsToCheck[i].Limit.Unit.String(), + ) + } } }