-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HTTP client request histogram function (#43)
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |