Skip to content

Commit

Permalink
add instrumentation to http client dial
Browse files Browse the repository at this point in the history
  • Loading branch information
srivastavankit committed May 22, 2017
1 parent ab97b38 commit 86387c8
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions runtime/http_client.go
Expand Up @@ -21,32 +21,68 @@
package zanzibar

import "net/http"
import "go.uber.org/zap"
import (
"net"
"time"

"github.com/uber-go/tally"
"go.uber.org/zap"
)

// HTTPClient defines a http client.
type HTTPClient struct {
gateway *Gateway

Client *http.Client
Logger *zap.Logger
Scope *tally.Scope
BaseURL string
}

// NewHTTPClient will allocate a http client.
func NewHTTPClient(
gateway *Gateway, baseURL string,
) *HTTPClient {
scope := gateway.MetricScope.SubScope("") // TODO: get http client name
timer := scope.Timer("dial")
success := scope.Counter("dial.success")
error := scope.Counter("dial.error")

return &HTTPClient{
gateway: gateway,

Logger: gateway.Logger,
Logger: gateway.Logger,
Client: &http.Client{
Transport: &http.Transport{
DisableKeepAlives: false,
MaxIdleConns: 500,
MaxIdleConnsPerHost: 500,
Dial: getInstrumentedDial(timer, success, error),
},
},
BaseURL: baseURL,
}
}

func getInstrumentedDial(timer tally.Timer, successCtr, errorCtr tally.Counter) func(string, string) (net.Conn, error) {
return func(n, a string) (conn net.Conn, err error) {
stat := instrument(timer, successCtr, errorCtr)
defer func() {
stat(err)
}()

return net.Dial(n, a)
}
}

func instrument(timer tally.Timer, successCtr, errorCtr tally.Counter) func(error) {
start := time.Now()

return func(err error) {
timer.Record(time.Since(start))
if err == nil {
successCtr.Inc(1)
} else {
errorCtr.Inc(1)
}
}
}

0 comments on commit 86387c8

Please sign in to comment.