-
Notifications
You must be signed in to change notification settings - Fork 14
/
roundtripper.go
47 lines (36 loc) · 953 Bytes
/
roundtripper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package internal
import (
"context"
"net/http"
"time"
"github.com/anz-bank/sysl-go/log"
)
type loggingRoundtripper struct {
ctx context.Context
base http.RoundTripper
}
func NewLoggingRoundTripper(ctx context.Context, base http.RoundTripper) http.RoundTripper {
return &loggingRoundtripper{
ctx: ctx,
base: base,
}
}
func (t *loggingRoundtripper) RoundTrip(req *http.Request) (*http.Response, error) {
start := time.Now()
var resp *http.Response
reqLogger, _ := NewRequestLogger(t.ctx, req)
defer func() {
reqLogger.LogResponse(resp)
}()
resp, err := t.base.RoundTrip(req)
if err != nil {
return nil, err
}
if val, ok := req.Context().Value(unclosedResponseBodyMonitorContextKey{}).(*unclosedResponseBodyMonitor); ok {
val.addResponse(resp)
}
reqTime := time.Since(start)
ctx := initCommonLogFields(t.ctx, resp.StatusCode, reqTime, resp.Request)
log.Info(ctx, "Backend request completed")
return resp, nil
}