Skip to content

Commit

Permalink
move cleanup and logging into a go routine
Browse files Browse the repository at this point in the history
  • Loading branch information
bnfinet committed May 14, 2020
1 parent ff16ebb commit 5728584
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions pkg/timelog/timelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,32 @@ func TimeLog(nextHandler http.Handler) func(http.ResponseWriter, *http.Request)

// Stop timer
end := time.Now()
latency := end.Sub(start)
req++
avgLatency = avgLatency + ((int64(latency) - avgLatency) / req)
log.Debugf("Request handled successfully: %v", v.GetStatusCode())
var statusCode = v.GetStatusCode()

path := r.URL.Path
host := r.Host
referer := r.Header.Get("Referer")
clientIP := r.RemoteAddr
method := r.Method

log.Infow(fmt.Sprintf("|%d| %10v %s", statusCode, time.Duration(latency), path),
"statusCode", statusCode,
"request", req,
"latency", time.Duration(latency),
"avgLatency", time.Duration(avgLatency),
"ipPort", clientIP,
"method", method,
"host", host,
"path", path,
"referer", referer,
)

go func() {
latency := end.Sub(start)
req++
avgLatency = avgLatency + ((int64(latency) - avgLatency) / req)
// log.Debugf("Request handled successfully: %v", v.GetStatusCode())
var statusCode = v.GetStatusCode()

path := r.URL.Path
host := r.Host
referer := r.Header.Get("Referer")
clientIP := r.RemoteAddr
method := r.Method

log.Infow(fmt.Sprintf("|%d| %10v %s", statusCode, time.Duration(latency), path),
"statusCode", statusCode,
"request", req,
"latency", time.Duration(latency),
"avgLatency", time.Duration(avgLatency),
"ipPort", clientIP,
"method", method,
"host", host,
"path", path,
"referer", referer,
)
}()

}
}

0 comments on commit 5728584

Please sign in to comment.