Skip to content

Commit

Permalink
Add start time to request logger middleware values (#1991)
Browse files Browse the repository at this point in the history
  • Loading branch information
aldas committed Sep 23, 2021
1 parent 4651c7a commit a2e6ca7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
6 changes: 5 additions & 1 deletion middleware/request_logger.go
Expand Up @@ -124,6 +124,8 @@ type RequestLoggerConfig struct {

// RequestLoggerValues contains extracted values from logger.
type RequestLoggerValues struct {
// StartTime is time recorded before next middleware/handler is executed.
StartTime time.Time
// Latency is duration it took to execute rest of the handler chain (next(c) call).
Latency time.Duration
// Protocol is request protocol (i.e. `HTTP/1.1` or `HTTP/2`)
Expand Down Expand Up @@ -215,7 +217,9 @@ func (config RequestLoggerConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
}
err := next(c)

v := RequestLoggerValues{}
v := RequestLoggerValues{
StartTime: start,
}
if config.LogLatency {
v.Latency = now().Sub(start)
}
Expand Down
1 change: 1 addition & 0 deletions middleware/request_logger_test.go
Expand Up @@ -296,6 +296,7 @@ func TestRequestLogger_allFields(t *testing.T) {
err := mw(c)

assert.NoError(t, err)
assert.Equal(t, time.Unix(1631045377, 0), expect.StartTime)
assert.Equal(t, 10*time.Second, expect.Latency)
assert.Equal(t, "HTTP/1.1", expect.Protocol)
assert.Equal(t, "8.8.8.8", expect.RemoteIP)
Expand Down

0 comments on commit a2e6ca7

Please sign in to comment.