Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP Client request metrics: consolidating and adding request statuses. #16271

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/client/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ var (
},
[]string{"verb", "url"},
)

RequestResult = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: restClientSubsystem,
Name: "request_status_codes",
Help: "Number of http requests, partitioned by metadata",
},
[]string{"code", "method", "host"},
)
)

var registerMetrics sync.Once
Expand All @@ -48,6 +57,7 @@ func Register() {
// Register the metrics.
registerMetrics.Do(func() {
prometheus.MustRegister(RequestLatency)
prometheus.MustRegister(RequestResult)
})
}

Expand Down
34 changes: 26 additions & 8 deletions pkg/client/unversioned/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ func (r *Request) Watch() (watch.Interface, error) {
client = http.DefaultClient
}
resp, err := client.Do(req)
updateURLMetrics(r, resp, err)
if err != nil {
// The watch stream mechanism handles many common partial data errors, so closed
// connections can be retried in many cases.
Expand All @@ -587,6 +588,23 @@ func (r *Request) Watch() (watch.Interface, error) {
return watch.NewStreamWatcher(watchjson.NewDecoder(resp.Body, r.codec)), nil
}

// updateURLMetrics is a convenience function for pushing metrics.
// It also handles corner cases for incomplete/invalid request data.
func updateURLMetrics(req *Request, resp *http.Response, err error) {
url := "none"
if req.baseURL != nil {
url = req.baseURL.Host
}

// If we have an error (i.e. apiserver down) we report that as a metric label.
if err != nil {
metrics.RequestResult.WithLabelValues(err.Error(), req.verb, url).Inc()
} else {
//Metrics for failure codes
metrics.RequestResult.WithLabelValues(strconv.Itoa(resp.StatusCode), req.verb, url).Inc()
}
}

// Stream formats and executes the request, and offers streaming of the response.
// Returns io.ReadCloser which could be used for streaming of the response, or an error
// Any non-2xx http status code causes an error. If we get a non-2xx code, we try to convert the body into an APIStatus object.
Expand All @@ -605,6 +623,7 @@ func (r *Request) Stream() (io.ReadCloser, error) {
client = http.DefaultClient
}
resp, err := client.Do(req)
updateURLMetrics(r, resp, err)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -641,6 +660,12 @@ func (r *Request) Stream() (io.ReadCloser, error) {
// fn at most once. It will return an error if a problem occurred prior to connecting to the
// server - the provided function is responsible for handling server errors.
func (r *Request) request(fn func(*http.Request, *http.Response)) error {
//Metrics for total request latency
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is right, now that I look at it again. You're timing all retries in one chunk. You should measure them individually, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the current code is chunking retries already. Maybe separate issue? Or should I fix it all in here ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really not sure-- what is the goal of this latency measurement?

On Wed, Nov 11, 2015 at 10:43 AM, jay vyas notifications@github.com wrote:

In pkg/client/unversioned/request.go
#16271 (comment)
:

@@ -641,6 +660,12 @@ func (r _Request) Stream() (io.ReadCloser, error) {
// fn at most once. It will return an error if a problem occurred prior to connecting to the
// server - the provided function is responsible for handling server errors.
func (r *Request) request(fn func(_http.Request, *http.Response)) error {

  • //Metrics for total request latency

the current code is chunking retries already. Maybe separate issue? Or
should I fix it all in here ?


Reply to this email directly or view it on GitHub
https://github.com/kubernetes/kubernetes/pull/16271/files#r44568576.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I created a separate issue below. I think your onto something: The semantics of that metrics are ill-defined.
  • As this stands, this code doesn't change the metric.

start := time.Now()
defer func() {
metrics.RequestLatency.WithLabelValues(r.verb, r.finalURLTemplate()).Observe(metrics.SinceInMicroseconds(start))
}()

if r.err != nil {
return r.err
}
Expand Down Expand Up @@ -671,6 +696,7 @@ func (r *Request) request(fn func(*http.Request, *http.Response)) error {
req.Header = r.headers

resp, err := client.Do(req)
updateURLMetrics(r, resp, err)
if err != nil {
return err
}
Expand Down Expand Up @@ -704,10 +730,6 @@ func (r *Request) request(fn func(*http.Request, *http.Response)) error {
// * If the server responds with a status: *errors.StatusError or *errors.UnexpectedObjectError
// * http.Client.Do errors are returned directly.
func (r *Request) Do() Result {
start := time.Now()
defer func() {
metrics.RequestLatency.WithLabelValues(r.verb, r.finalURLTemplate()).Observe(metrics.SinceInMicroseconds(start))
}()
var result Result
err := r.request(func(req *http.Request, resp *http.Response) {
result = r.transformResponse(resp, req)
Expand All @@ -720,10 +742,6 @@ func (r *Request) Do() Result {

// DoRaw executes the request but does not process the response body.
func (r *Request) DoRaw() ([]byte, error) {
start := time.Now()
defer func() {
metrics.RequestLatency.WithLabelValues(r.verb, r.finalURLTemplate()).Observe(metrics.SinceInMicroseconds(start))
}()
var result Result
err := r.request(func(req *http.Request, resp *http.Response) {
result.body, result.err = ioutil.ReadAll(resp.Body)
Expand Down