mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
Merge pull request #98973 from YoyinZyc/add_context_outside_apiserver
Apply context to restclient and serviceaccount metrics
This commit is contained in:
commit
9457a5c3d8
@ -192,9 +192,9 @@ func (v *validator) Validate(ctx context.Context, _ string, public *jwt.Claims,
|
|||||||
secondsAfterWarn := nowTime.Unix() - warnafter.Time().Unix()
|
secondsAfterWarn := nowTime.Unix() - warnafter.Time().Unix()
|
||||||
auditInfo := fmt.Sprintf("subject: %s, seconds after warning threshold: %d", public.Subject, secondsAfterWarn)
|
auditInfo := fmt.Sprintf("subject: %s, seconds after warning threshold: %d", public.Subject, secondsAfterWarn)
|
||||||
audit.AddAuditAnnotation(ctx, "authentication.k8s.io/stale-token", auditInfo)
|
audit.AddAuditAnnotation(ctx, "authentication.k8s.io/stale-token", auditInfo)
|
||||||
staleTokensTotal.Inc()
|
staleTokensTotal.WithContext(ctx).Inc()
|
||||||
} else {
|
} else {
|
||||||
validTokensTotal.Inc()
|
validTokensTotal.WithContext(ctx).Inc()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,7 +290,7 @@ func (j *jwtTokenAuthenticator) AuthenticateToken(ctx context.Context, tokenData
|
|||||||
if len(tokenAudiences) == 0 {
|
if len(tokenAudiences) == 0 {
|
||||||
// only apiserver audiences are allowed for legacy tokens
|
// only apiserver audiences are allowed for legacy tokens
|
||||||
audit.AddAuditAnnotation(ctx, "authentication.k8s.io/legacy-token", public.Subject)
|
audit.AddAuditAnnotation(ctx, "authentication.k8s.io/legacy-token", public.Subject)
|
||||||
legacyTokensTotal.Inc()
|
legacyTokensTotal.WithContext(ctx).Inc()
|
||||||
tokenAudiences = j.implicitAuds
|
tokenAudiences = j.implicitAuds
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 {
|
||||||
|
@ -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{}
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package restclient
|
package restclient
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -137,16 +138,16 @@ type latencyAdapter struct {
|
|||||||
m *k8smetrics.HistogramVec
|
m *k8smetrics.HistogramVec
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *latencyAdapter) Observe(verb string, u url.URL, latency time.Duration) {
|
func (l *latencyAdapter) Observe(ctx context.Context, verb string, u url.URL, latency time.Duration) {
|
||||||
l.m.WithLabelValues(verb, u.String()).Observe(latency.Seconds())
|
l.m.WithContext(ctx).WithLabelValues(verb, u.String()).Observe(latency.Seconds())
|
||||||
}
|
}
|
||||||
|
|
||||||
type resultAdapter struct {
|
type resultAdapter struct {
|
||||||
m *k8smetrics.CounterVec
|
m *k8smetrics.CounterVec
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *resultAdapter) Increment(code, method, host string) {
|
func (r *resultAdapter) Increment(ctx context.Context, code, method, host string) {
|
||||||
r.m.WithLabelValues(code, method, host).Inc()
|
r.m.WithContext(ctx).WithLabelValues(code, method, host).Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
type expiryToTTLAdapter struct {
|
type expiryToTTLAdapter struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user