diff --git a/config/config_test.go b/config/config_test.go index 3ee5464b..7e1a5477 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -929,7 +929,7 @@ func TestRequestLogging(t *testing.T) { expectedLogInfo []logComponents }{ { - name: "invalid_nagative", // must be greater or equal to zero. Expect fatal log + name: "invalid_negative", // must be greater or equal to zero. Expect fatal log inRequestLoggingCfg: &RequestLogging{ RefererSamplingRate: -0.1, }, diff --git a/endpoints/get.go b/endpoints/get.go index 296e91a3..cd225ca6 100644 --- a/endpoints/get.go +++ b/endpoints/get.go @@ -47,10 +47,10 @@ func NewGetHandler(storage backends.Backend, metrics *metrics.Metrics, allowCust func (e *GetHandler) handle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { e.metrics.RecordGetTotal() - if utils.RandomPick(e.cfg.refererLogRate) == true { - if referer := r.Referer(); referer != "" { - log.Info("GET request Referer header: " + referer) - } + // If incoming request comes with a referer header, there's a e.cfg.refererLogRate percent chance + // getting it logged + if referer := r.Referer(); referer != "" && utils.RandomPick(e.cfg.refererLogRate) { + log.Info("GET request Referer header: " + referer) } start := time.Now() diff --git a/endpoints/put.go b/endpoints/put.go index 2d13c562..4cc5b3aa 100644 --- a/endpoints/put.go +++ b/endpoints/put.go @@ -186,10 +186,10 @@ func logBackendError(err error) { func (e *PutHandler) handle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { e.metrics.RecordPutTotal() - if utils.RandomPick(e.cfg.refererLogRate) == true { - if referer := r.Referer(); referer != "" { - logrus.Info("PUT request Referer header: " + referer) - } + // If incoming request comes with a referer header, there's a e.cfg.refererLogRate percent chance + // getting it logged + if referer := r.Referer(); referer != "" && utils.RandomPick(e.cfg.refererLogRate) { + logrus.Info("PUT request Referer header: " + referer) } start := time.Now() diff --git a/utils/utils_test.go b/utils/utils_test.go index 9333b7ab..756d118d 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -8,22 +8,24 @@ import ( func TestRandomPick(t *testing.T) { testCases := []struct { - desc string + name string inPickProbability float64 expected bool }{ { - desc: "Zero logging rate. Expect false", + name: "zero", // Zero probablity of true, expect false inPickProbability: 0.00, expected: false, }, { - desc: "100% logging rate, expect true", + name: "one", // 100% probability of true, expect true inPickProbability: 1.00, expected: true, }, } for _, tc := range testCases { - assert.Equal(t, tc.expected, RandomPick(tc.inPickProbability), tc.desc) + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.expected, RandomPick(tc.inPickProbability), tc.name) + }) } }