Skip to content

Commit 3ffe11c

Browse files
author
Peter Mogensen
committed
Add an option to store a timestamp with RecordingResponseWriter - for req. metrics
1 parent e53926d commit 3ffe11c

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

http/handlers/accesslog/loghandler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ func (h *logHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
116116
recorder := rrwriter.MakeRecorder(w)
117117

118118
t := time.Now()
119+
recorder.SetTimeStamp(t)
120+
119121
pbuf := h.bufpool.Get().(*buffer)
120122
logbuf := pbuf[:0]
121123

@@ -145,6 +147,7 @@ func (h *logHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
145147
} else {
146148
if h.af != nil {
147149
recorder := rrwriter.MakeRecorder(w)
150+
//recorder.SetTimeStamp(time.Now())
148151
h.handler.ServeHTTP(recorder, req)
149152
h.af(recorder)
150153
} else {

http/rrwriter/responselogger.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"io"
1515
"net"
1616
"net/http"
17+
"time"
1718
)
1819

1920
// MakeRecorder returns a RecordingResponseWriter which wraps around the
@@ -41,6 +42,8 @@ type RecordingResponseWriter interface {
4142
http.Flusher
4243
Status() int
4344
Size() int
45+
GetTimeStamp() time.Time
46+
SetTimeStamp(time.Time)
4447
}
4548

4649
// responseRecorder is wrapper of http.ResponseWriter that keeps track of its HTTP
@@ -49,12 +52,15 @@ type responseRecorder struct {
4952
w http.ResponseWriter
5053
status int
5154
size int
55+
ts time.Time
5256
}
5357

58+
// Header implements http.ResponseWriter
5459
func (l *responseRecorder) Header() http.Header {
5560
return l.w.Header()
5661
}
5762

63+
// Write implements http.ResponseWriter
5864
func (l *responseRecorder) Write(b []byte) (int, error) {
5965
if l.status == 0 {
6066
// The status will be StatusOK if WriteHeader has not been called yet
@@ -65,11 +71,30 @@ func (l *responseRecorder) Write(b []byte) (int, error) {
6571
return size, err
6672
}
6773

74+
// WriteHeader implements http.ResponseWriter
6875
func (l *responseRecorder) WriteHeader(s int) {
6976
l.w.WriteHeader(s)
7077
l.status = s
7178
}
7279

80+
// GetTimeStamp returns the timestamp
81+
// This can be used to store a starttime for a request and return it
82+
// after the request is done.
83+
// It is not go-routine safe.
84+
func (l *responseRecorder) GetTimeStamp() (tout time.Time) {
85+
tout = l.ts
86+
return
87+
}
88+
89+
// SetTimeStamp sets a timestamp.
90+
// This can be used to store a starttime for a request and return it
91+
// after the request is done.
92+
// It is not go-routine safe.
93+
func (l *responseRecorder) SetTimeStamp(tin time.Time) {
94+
l.ts = tin
95+
}
96+
97+
7398
// Status returns the http status of the written request.
7499
func (l *responseRecorder) Status() int {
75100
return l.status

0 commit comments

Comments
 (0)