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

Apply context to restclient and serviceaccount metrics #98973

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
4 changes: 2 additions & 2 deletions pkg/serviceaccount/claims.go
Expand Up @@ -192,9 +192,9 @@ func (v *validator) Validate(ctx context.Context, _ string, public *jwt.Claims,
secondsAfterWarn := nowTime.Unix() - warnafter.Time().Unix()
auditInfo := fmt.Sprintf("subject: %s, seconds after warning threshold: %d", public.Subject, secondsAfterWarn)
audit.AddAuditAnnotation(ctx, "authentication.k8s.io/stale-token", auditInfo)
staleTokensTotal.Inc()
staleTokensTotal.WithContext(ctx).Inc()
} else {
validTokensTotal.Inc()
validTokensTotal.WithContext(ctx).Inc()
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/serviceaccount/jwt.go
Expand Up @@ -290,7 +290,7 @@ func (j *jwtTokenAuthenticator) AuthenticateToken(ctx context.Context, tokenData
if len(tokenAudiences) == 0 {
// only apiserver audiences are allowed for legacy tokens
audit.AddAuditAnnotation(ctx, "authentication.k8s.io/legacy-token", public.Subject)
legacyTokensTotal.Inc()
legacyTokensTotal.WithContext(ctx).Inc()
tokenAudiences = j.implicitAuds
}

Expand Down
16 changes: 8 additions & 8 deletions staging/src/k8s.io/client-go/rest/request.go
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions staging/src/k8s.io/client-go/tools/metrics/metrics.go
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
package metrics

import (
"context"
"net/url"
"sync"
"time"
Expand All @@ -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.
Expand Down Expand Up @@ -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{}

Expand Down
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package restclient

import (
"context"
"fmt"
"math"
"net/url"
Expand Down Expand Up @@ -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 {
Expand Down