mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	add context to restclient metrics
This commit is contained in:
		@@ -604,7 +604,7 @@ func (r *Request) tryThrottleWithInfo(ctx context.Context, retryInfo string) err
 | 
			
		||||
		// but we use a throttled logger to prevent spamming.
 | 
			
		||||
		globalThrottledLogger.Infof(message)
 | 
			
		||||
	}
 | 
			
		||||
	metrics.RateLimiterLatency.Observe(r.verb, r.finalURLTemplate(), latency)
 | 
			
		||||
	metrics.RateLimiterLatency.Observe(ctx, r.verb, r.finalURLTemplate(), latency)
 | 
			
		||||
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
@@ -691,7 +691,7 @@ func (r *Request) Watch(ctx context.Context) (watch.Interface, error) {
 | 
			
		||||
	}
 | 
			
		||||
	r.backoff.Sleep(r.backoff.CalculateBackoff(r.URL()))
 | 
			
		||||
	resp, err := client.Do(req)
 | 
			
		||||
	updateURLMetrics(r, resp, err)
 | 
			
		||||
	updateURLMetrics(ctx, r, resp, err)
 | 
			
		||||
	if r.c.base != nil {
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			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.
 | 
			
		||||
// 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"
 | 
			
		||||
	if req.c.base != nil {
 | 
			
		||||
		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
 | 
			
		||||
	// system so we just report them as `<error>`.
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		metrics.RequestResult.Increment("<error>", req.verb, url)
 | 
			
		||||
		metrics.RequestResult.Increment(ctx, "<error>", req.verb, url)
 | 
			
		||||
	} else {
 | 
			
		||||
		//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()))
 | 
			
		||||
	resp, err := client.Do(req)
 | 
			
		||||
	updateURLMetrics(r, resp, err)
 | 
			
		||||
	updateURLMetrics(ctx, r, resp, err)
 | 
			
		||||
	if r.c.base != nil {
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			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
 | 
			
		||||
	start := time.Now()
 | 
			
		||||
	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 {
 | 
			
		||||
@@ -904,7 +904,7 @@ func (r *Request) request(ctx context.Context, fn func(*http.Request, *http.Resp
 | 
			
		||||
			retryInfo = ""
 | 
			
		||||
		}
 | 
			
		||||
		resp, err := client.Do(req)
 | 
			
		||||
		updateURLMetrics(r, resp, err)
 | 
			
		||||
		updateURLMetrics(ctx, r, resp, err)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			r.backoff.UpdateBackoff(r.URL(), err, 0)
 | 
			
		||||
		} else {
 | 
			
		||||
 
 | 
			
		||||
@@ -19,6 +19,7 @@ limitations under the License.
 | 
			
		||||
package metrics
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
@@ -38,12 +39,12 @@ type ExpiryMetric interface {
 | 
			
		||||
 | 
			
		||||
// LatencyMetric observes client latency partitioned by verb and url.
 | 
			
		||||
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.
 | 
			
		||||
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.
 | 
			
		||||
@@ -113,11 +114,11 @@ func (noopExpiry) Set(*time.Time) {}
 | 
			
		||||
 | 
			
		||||
type noopLatency struct{}
 | 
			
		||||
 | 
			
		||||
func (noopLatency) Observe(string, url.URL, time.Duration) {}
 | 
			
		||||
func (noopLatency) Observe(context.Context, string, url.URL, time.Duration) {}
 | 
			
		||||
 | 
			
		||||
type noopResult struct{}
 | 
			
		||||
 | 
			
		||||
func (noopResult) Increment(string, string, string) {}
 | 
			
		||||
func (noopResult) Increment(context.Context, string, string, string) {}
 | 
			
		||||
 | 
			
		||||
type noopCalls struct{}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -17,6 +17,7 @@ limitations under the License.
 | 
			
		||||
package restclient
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"math"
 | 
			
		||||
	"net/url"
 | 
			
		||||
@@ -137,16 +138,16 @@ type latencyAdapter struct {
 | 
			
		||||
	m *k8smetrics.HistogramVec
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (l *latencyAdapter) Observe(verb string, u url.URL, latency time.Duration) {
 | 
			
		||||
	l.m.WithLabelValues(verb, u.String()).Observe(latency.Seconds())
 | 
			
		||||
func (l *latencyAdapter) Observe(ctx context.Context, verb string, u url.URL, latency time.Duration) {
 | 
			
		||||
	l.m.WithContext(ctx).WithLabelValues(verb, u.String()).Observe(latency.Seconds())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type resultAdapter struct {
 | 
			
		||||
	m *k8smetrics.CounterVec
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *resultAdapter) Increment(code, method, host string) {
 | 
			
		||||
	r.m.WithLabelValues(code, method, host).Inc()
 | 
			
		||||
func (r *resultAdapter) Increment(ctx context.Context, code, method, host string) {
 | 
			
		||||
	r.m.WithContext(ctx).WithLabelValues(code, method, host).Inc()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type expiryToTTLAdapter struct {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user