Skip to content

Commit

Permalink
fix: fix testServiceURL typo, add testServiceName, add ServiceNames f…
Browse files Browse the repository at this point in the history
…ilter tests
  • Loading branch information
ttys3 committed Mar 1, 2024
1 parent 7a90a27 commit 4c7bc68
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/middlewares/accesslog/filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package accesslog
5 changes: 4 additions & 1 deletion pkg/middlewares/accesslog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,10 @@ func (h *Handler) logTheRoundTrip(logDataTable *LogData) {
totalDuration := time.Now().UTC().Sub(core[StartUTC].(time.Time))
core[Duration] = totalDuration

serviceName := core[ServiceName].(string)
var serviceName string
if tmpSvc, ok := core[ServiceName].(string); ok {
serviceName = tmpSvc
}

if h.keepAccessLog(status, retryAttempts, totalDuration, serviceName) {
size := logDataTable.DownstreamResponse.size
Expand Down
32 changes: 28 additions & 4 deletions pkg/middlewares/accesslog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const delta float64 = 1e-10
var (
logFileNameSuffix = "/traefik/logger/test.log"
testContent = "Hello, World"
testServiceName = "http://127.0.0.1/testService"
testServiceURL = "http://127.0.0.1/testService"
testServiceName = "testService"
testRouterName = "testRouter"
testStatus = 123
testContentSize int64 = 12
Expand Down Expand Up @@ -323,7 +324,7 @@ func TestLoggerJSON(t *testing.T) {
RequestRefererHeader: assertString(testReferer),
RequestUserAgentHeader: assertString(testUserAgent),
RouterName: assertString(testRouterName),
ServiceURL: assertString(testServiceName),
ServiceURL: assertString(testServiceURL),
ClientUsername: assertString(testUsername),
ClientHost: assertString(testHostname),
ClientPort: assertString(strconv.Itoa(testPort)),
Expand Down Expand Up @@ -363,7 +364,7 @@ func TestLoggerJSON(t *testing.T) {
RequestRefererHeader: assertString(testReferer),
RequestUserAgentHeader: assertString(testUserAgent),
RouterName: assertString(testRouterName),
ServiceURL: assertString(testServiceName),
ServiceURL: assertString(testServiceURL),
ClientUsername: assertString(testUsername),
ClientHost: assertString(testHostname),
ClientPort: assertString(strconv.Itoa(testPort)),
Expand Down Expand Up @@ -684,6 +685,28 @@ func TestNewLogHandlerOutputStdout(t *testing.T) {
},
expectedLog: `- - TestUser [-] "- - -" - - "REDACTED" "testUserAgent" - "-" "-" 0ms`,
},
{
desc: "ServiceNames filter matching",
config: &types.AccessLog{
FilePath: "",
Format: CommonFormat,
Filters: &types.AccessLogFilters{
ServiceNames: []string{"testService"},
},
},
expectedLog: `TestHost - TestUser [13/Apr/2016:07:14:19 -0700] "POST testpath HTTP/0.0" 123 12 "testReferer" "testUserAgent" 23 "testRouter" "http://127.0.0.1/testService" 1ms`,
},
{
desc: "ServiceNames filter not matching",
config: &types.AccessLog{
FilePath: "",
Format: CommonFormat,
Filters: &types.AccessLogFilters{
ServiceNames: []string{"fooService"},
},
},
expectedLog: ``,
},
}

for _, test := range testCases {
Expand Down Expand Up @@ -822,7 +845,8 @@ func logWriterTestHandlerFunc(rw http.ResponseWriter, r *http.Request) {
logData := GetLogData(r)
if logData != nil {
logData.Core[RouterName] = testRouterName
logData.Core[ServiceURL] = testServiceName
logData.Core[ServiceURL] = testServiceURL
logData.Core[ServiceName] = testServiceName
logData.Core[OriginStatus] = testStatus
logData.Core[OriginContentSize] = testContentSize
logData.Core[RetryAttempts] = testRetryAttempts
Expand Down

0 comments on commit 4c7bc68

Please sign in to comment.