Merge pull request #98973 from YoyinZyc/add_context_outside_apiserver

Apply context to restclient and serviceaccount metrics

Kubernetes-commit: 9457a5c3d8dd3892bb1133faa58d20399e05e871
This commit is contained in:
Kubernetes Publisher 2021-03-08 22:53:11 -08:00
commit db49dde90f
2 changed files with 13 additions and 12 deletions

View File

@ -604,7 +604,7 @@ func (r *Request) tryThrottleWithInfo(ctx context.Context, retryInfo string) err
// but we use a throttled logger to prevent spamming. // but we use a throttled logger to prevent spamming.
globalThrottledLogger.Infof(message) globalThrottledLogger.Infof(message)
} }
metrics.RateLimiterLatency.Observe(r.verb, r.finalURLTemplate(), latency) metrics.RateLimiterLatency.Observe(ctx, r.verb, r.finalURLTemplate(), latency)
return err return err
} }
@ -691,7 +691,7 @@ func (r *Request) Watch(ctx context.Context) (watch.Interface, error) {
} }
r.backoff.Sleep(r.backoff.CalculateBackoff(r.URL())) r.backoff.Sleep(r.backoff.CalculateBackoff(r.URL()))
resp, err := client.Do(req) resp, err := client.Do(req)
updateURLMetrics(r, resp, err) updateURLMetrics(ctx, r, resp, err)
if r.c.base != nil { if r.c.base != nil {
if err != nil { if err != nil {
r.backoff.UpdateBackoff(r.c.base, err, 0) r.backoff.UpdateBackoff(r.c.base, err, 0)
@ -740,7 +740,7 @@ func (r *Request) Watch(ctx context.Context) (watch.Interface, error) {
// updateURLMetrics is a convenience function for pushing metrics. // updateURLMetrics is a convenience function for pushing metrics.
// It also handles corner cases for incomplete/invalid request data. // It also handles corner cases for incomplete/invalid request data.
func updateURLMetrics(req *Request, resp *http.Response, err error) { func updateURLMetrics(ctx context.Context, req *Request, resp *http.Response, err error) {
url := "none" url := "none"
if req.c.base != nil { if req.c.base != nil {
url = req.c.base.Host url = req.c.base.Host
@ -749,10 +749,10 @@ func updateURLMetrics(req *Request, resp *http.Response, err error) {
// Errors can be arbitrary strings. Unbound label cardinality is not suitable for a metric // Errors can be arbitrary strings. Unbound label cardinality is not suitable for a metric
// system so we just report them as `<error>`. // system so we just report them as `<error>`.
if err != nil { if err != nil {
metrics.RequestResult.Increment("<error>", req.verb, url) metrics.RequestResult.Increment(ctx, "<error>", req.verb, url)
} else { } else {
//Metrics for failure codes //Metrics for failure codes
metrics.RequestResult.Increment(strconv.Itoa(resp.StatusCode), req.verb, url) metrics.RequestResult.Increment(ctx, strconv.Itoa(resp.StatusCode), req.verb, url)
} }
} }
@ -785,7 +785,7 @@ func (r *Request) Stream(ctx context.Context) (io.ReadCloser, error) {
} }
r.backoff.Sleep(r.backoff.CalculateBackoff(r.URL())) r.backoff.Sleep(r.backoff.CalculateBackoff(r.URL()))
resp, err := client.Do(req) resp, err := client.Do(req)
updateURLMetrics(r, resp, err) updateURLMetrics(ctx, r, resp, err)
if r.c.base != nil { if r.c.base != nil {
if err != nil { if err != nil {
r.backoff.UpdateBackoff(r.URL(), err, 0) r.backoff.UpdateBackoff(r.URL(), err, 0)
@ -850,7 +850,7 @@ func (r *Request) request(ctx context.Context, fn func(*http.Request, *http.Resp
//Metrics for total request latency //Metrics for total request latency
start := time.Now() start := time.Now()
defer func() { defer func() {
metrics.RequestLatency.Observe(r.verb, r.finalURLTemplate(), time.Since(start)) metrics.RequestLatency.Observe(ctx, r.verb, r.finalURLTemplate(), time.Since(start))
}() }()
if r.err != nil { if r.err != nil {
@ -904,7 +904,7 @@ func (r *Request) request(ctx context.Context, fn func(*http.Request, *http.Resp
retryInfo = "" retryInfo = ""
} }
resp, err := client.Do(req) resp, err := client.Do(req)
updateURLMetrics(r, resp, err) updateURLMetrics(ctx, r, resp, err)
if err != nil { if err != nil {
r.backoff.UpdateBackoff(r.URL(), err, 0) r.backoff.UpdateBackoff(r.URL(), err, 0)
} else { } else {

View File

@ -19,6 +19,7 @@ limitations under the License.
package metrics package metrics
import ( import (
"context"
"net/url" "net/url"
"sync" "sync"
"time" "time"
@ -38,12 +39,12 @@ type ExpiryMetric interface {
// LatencyMetric observes client latency partitioned by verb and url. // LatencyMetric observes client latency partitioned by verb and url.
type LatencyMetric interface { type LatencyMetric interface {
Observe(verb string, u url.URL, latency time.Duration) Observe(ctx context.Context, verb string, u url.URL, latency time.Duration)
} }
// ResultMetric counts response codes partitioned by method and host. // ResultMetric counts response codes partitioned by method and host.
type ResultMetric interface { type ResultMetric interface {
Increment(code string, method string, host string) Increment(ctx context.Context, code string, method string, host string)
} }
// CallsMetric counts calls that take place for a specific exec plugin. // CallsMetric counts calls that take place for a specific exec plugin.
@ -113,11 +114,11 @@ func (noopExpiry) Set(*time.Time) {}
type noopLatency struct{} type noopLatency struct{}
func (noopLatency) Observe(string, url.URL, time.Duration) {} func (noopLatency) Observe(context.Context, string, url.URL, time.Duration) {}
type noopResult struct{} type noopResult struct{}
func (noopResult) Increment(string, string, string) {} func (noopResult) Increment(context.Context, string, string, string) {}
type noopCalls struct{} type noopCalls struct{}