Skip to content

Commit

Permalink
Add HTTP client request histogram function (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
leth authored Jul 12, 2017
1 parent 493a1f7 commit f6304fb
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions http/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package client

import (
"context"
"fmt"
"net/http"
"strconv"

"github.com/prometheus/client_golang/prometheus"
"github.com/weaveworks/common/instrument"
oldcontext "golang.org/x/net/context"
)

// TimeRequestHistogram performs an HTTP client request and records the duration in a histogram
func TimeRequestHistogram(ctx context.Context, operation string, metric *prometheus.HistogramVec, client *http.Client, request *http.Request) (*http.Response, error) {
var response *http.Response
doRequest := func(_ oldcontext.Context) error {
var err error
response, err = client.Do(request)
return err
}
toStatusCode := func(err error) string {
if err == nil {
return strconv.Itoa(response.StatusCode)
}
return "error"
}
err := instrument.TimeRequestHistogramStatus(ctx, fmt.Sprintf("%s %s", request.Method, operation), metric, toStatusCode, doRequest)
return response, err
}

0 comments on commit f6304fb

Please sign in to comment.