|
| 1 | +// Copyright 2025 NetApp, Inc. All Rights Reserved. |
| 2 | + |
| 3 | +package k8sclient |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "k8s.io/client-go/tools/metrics" |
| 15 | + |
| 16 | + . "github.com/netapp/trident/logging" |
| 17 | + "github.com/netapp/trident/utils/errors" |
| 18 | +) |
| 19 | + |
| 20 | +// registerK8sClientGoMetricsAdapter registers client-go metric adapters to feed Trident telemeters. |
| 21 | +// This is currently limited to rate limiter latency and request retries. |
| 22 | +func registerK8sClientGoMetricsAdapter() { |
| 23 | + metrics.Register(metrics.RegisterOpts{ |
| 24 | + RateLimiterLatency: rateLimiterLatencyAdapter{}, |
| 25 | + RequestRetry: requestRetryAdapter{}, |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +// rateLimiterLatencyAdapter plugs Trident telemeters into client-go rate limiter latency metrics. |
| 30 | +// This reflects the amount of time the client had to wait for a token from the limiter. |
| 31 | +// This is directly indicates throttling due to QPS/burst limits. |
| 32 | +type rateLimiterLatencyAdapter struct{} |
| 33 | + |
| 34 | +func (rateLimiterLatencyAdapter) Observe(ctx context.Context, verb string, u url.URL, latency time.Duration) { |
| 35 | + rec := NewContextBuilder(ctx). |
| 36 | + WithTarget(ContextRequestTargetKubernetes). |
| 37 | + WithAddress(u.Host). |
| 38 | + WithMethod(verb). |
| 39 | + WithDuration(latency). |
| 40 | + WithTelemetry(OutgoingAPIRequestLimitedDurationTelemeter). |
| 41 | + BuildTelemetry() |
| 42 | + |
| 43 | + err := errors.TooManyRequestsError("client-go rate limiter wait %s", latency.String()) |
| 44 | + rec(&err) |
| 45 | +} |
| 46 | + |
| 47 | +// requestRetryAdapter plugs Trident telemeters into client-go request retry metrics. |
| 48 | +// This adapter only captures retries, not initial requests. |
| 49 | +// Each retry indicates a failure that triggered the retry. |
| 50 | +type requestRetryAdapter struct{} |
| 51 | + |
| 52 | +func (requestRetryAdapter) IncrementRetry(ctx context.Context, code string, method string, host string) { |
| 53 | + rec := NewContextBuilder(ctx). |
| 54 | + WithTarget(ContextRequestTargetKubernetes). |
| 55 | + WithAddress(host). |
| 56 | + WithMethod(method). |
| 57 | + WithTelemetry(OutgoingAPIRequestRetryTotalTelemeter). |
| 58 | + BuildTelemetry() |
| 59 | + |
| 60 | + var err error |
| 61 | + // Only retries triggered by errors are counted. |
| 62 | + if err = assertErrorForCode(code); err != nil { |
| 63 | + // Assign to the outer err so the deferred recorder observes the retry |
| 64 | + err = errors.WrapWithMustRetryError(err, "retry triggered after http status: %s", code) |
| 65 | + } |
| 66 | + rec(&err) |
| 67 | +} |
| 68 | + |
| 69 | +// assertErrorForCode returns nil for 2xx/3xx HTTP status codes, error otherwise. |
| 70 | +func assertErrorForCode(code string) error { |
| 71 | + c := strings.TrimSpace(code) |
| 72 | + if c == "" { |
| 73 | + return fmt.Errorf("missing http status code") |
| 74 | + } |
| 75 | + |
| 76 | + // Require a numeric HTTP status code. |
| 77 | + n, err := strconv.Atoi(c) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("invalid http status %s", c) |
| 80 | + } |
| 81 | + |
| 82 | + // Validate range: HTTP status codes are 100–599. |
| 83 | + if n < 100 || n > 599 { |
| 84 | + return fmt.Errorf("invalid http status %d", n) |
| 85 | + } |
| 86 | + |
| 87 | + // Return success for codes in the range: [200, 399], 200 <= n <= 399. |
| 88 | + if http.StatusOK <= n && n < http.StatusBadRequest { |
| 89 | + return nil |
| 90 | + } |
| 91 | + |
| 92 | + return fmt.Errorf("http status %d", n) |
| 93 | +} |
0 commit comments