1
0
Fork 0

add access log filter for retry attempts

This commit is contained in:
Marco Jantke 2018-03-23 09:28:03 +01:00 committed by Traefiker Bot
parent 5792a19b97
commit c762b9bb2e
6 changed files with 116 additions and 28 deletions

View file

@ -33,7 +33,8 @@ type StatusCodes []string
// AccessLogFilters holds filters configuration
type AccessLogFilters struct {
StatusCodes StatusCodes `json:"statusCodes,omitempty" description:"Keep only specific ranges of HTTP Status codes" export:"true"`
StatusCodes StatusCodes `json:"statusCodes,omitempty" description:"Keep access logs with status codes in the specified range" export:"true"`
RetryAttempts bool `json:"retryAttempts,omitempty" description:"Keep access logs when at least one retry happened" export:"true"`
}
// FieldNames holds maps of fields with specific mode

View file

@ -540,7 +540,7 @@ func (clientTLS *ClientTLS) CreateTLSConfig() (*tls.Config, error) {
// HTTPCodeRanges holds HTTP code ranges
type HTTPCodeRanges [][2]int
// NewHTTPCodeRanges create a new NewHTTPCodeRanges from a given []string].
// NewHTTPCodeRanges creates HTTPCodeRanges from a given []string.
// Break out the http status code ranges into a low int and high int
// for ease of use at runtime
func NewHTTPCodeRanges(strBlocks []string) (HTTPCodeRanges, error) {
@ -563,3 +563,14 @@ func NewHTTPCodeRanges(strBlocks []string) (HTTPCodeRanges, error) {
}
return blocks, nil
}
// Contains tests whether the passed status code is within
// one of its HTTP code ranges.
func (h HTTPCodeRanges) Contains(statusCode int) bool {
for _, block := range h {
if statusCode >= block[0] && statusCode <= block[1] {
return true
}
}
return false
}

View file

@ -1,6 +1,7 @@
package types
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
@ -93,3 +94,45 @@ func TestNewHTTPCodeRanges(t *testing.T) {
})
}
}
func TestHTTPCodeRanges_Contains(t *testing.T) {
testCases := []struct {
strBlocks []string
statusCode int
contains bool
}{
{
strBlocks: []string{"200-299"},
statusCode: 200,
contains: true,
},
{
strBlocks: []string{"200"},
statusCode: 200,
contains: true,
},
{
strBlocks: []string{"201"},
statusCode: 200,
contains: false,
},
{
strBlocks: []string{"200-299", "500-599"},
statusCode: 400,
contains: false,
},
}
for _, test := range testCases {
test := test
testName := fmt.Sprintf("%q contains %d", test.strBlocks, test.statusCode)
t.Run(testName, func(t *testing.T) {
t.Parallel()
httpCodeRanges, err := NewHTTPCodeRanges(test.strBlocks)
assert.NoError(t, err)
assert.Equal(t, test.contains, httpCodeRanges.Contains(test.statusCode))
})
}
}