reddit: round tripper now shows ms duration of request
This commit is contained in:
parent
fbbf5a3af0
commit
2eed06b320
|
@ -2,6 +2,7 @@ package reddit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/tigorlazuardi/redmage/config"
|
"github.com/tigorlazuardi/redmage/config"
|
||||||
"github.com/tigorlazuardi/redmage/pkg/log"
|
"github.com/tigorlazuardi/redmage/pkg/log"
|
||||||
|
@ -25,22 +26,26 @@ func (ro roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error)
|
||||||
|
|
||||||
func createRoundTripper(cfg *config.Config) roundTripperFunc {
|
func createRoundTripper(cfg *config.Config) roundTripperFunc {
|
||||||
return func(r *http.Request) (*http.Response, error) {
|
return func(r *http.Request) (*http.Response, error) {
|
||||||
|
start := time.Now()
|
||||||
r.Header.Set("User-Agent", cfg.String("download.useragent"))
|
r.Header.Set("User-Agent", cfg.String("download.useragent"))
|
||||||
resp, err := http.DefaultTransport.RoundTrip(r)
|
resp, err := http.DefaultTransport.RoundTrip(r)
|
||||||
|
end := time.Now()
|
||||||
|
duration := end.Sub(start)
|
||||||
|
durStr := (duration / time.Millisecond).String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.New(r.Context()).
|
log.New(r.Context()).
|
||||||
Err(err).
|
Err(err).
|
||||||
Errorf("reddit: %s %s", r.Method, r.URL)
|
Errorf("reddit: %s %s %sms", r.Method, r.URL, durStr)
|
||||||
return resp, err
|
return resp, err
|
||||||
}
|
}
|
||||||
if resp.StatusCode >= 400 {
|
if resp.StatusCode >= 400 {
|
||||||
log.New(r.Context()).
|
log.New(r.Context()).
|
||||||
Errorf("reddit: %s %s %d", r.Method, r.URL, resp.StatusCode)
|
Errorf("reddit: %s %s %sms", r.Method, r.URL, durStr)
|
||||||
return resp, err
|
return resp, err
|
||||||
}
|
}
|
||||||
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
||||||
log.New(r.Context()).
|
log.New(r.Context()).
|
||||||
Infof("reddit: %s %s %d", r.Method, r.URL, resp.StatusCode)
|
Infof("reddit: %s %s %d %sms", r.Method, r.URL, resp.StatusCode, durStr)
|
||||||
}
|
}
|
||||||
return resp, err
|
return resp, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue