Skip to content

Commit 74b0476

Browse files
authored
Merge pull request kubernetes#93523 from wojtek-t/fix_metrics_reporting
Fix verbs reporting in kube-apiserver metrics
2 parents 6a11d1f + 1406317 commit 74b0476

File tree

1 file changed

+20
-17
lines changed
  • staging/src/k8s.io/apiserver/pkg/endpoints/metrics

1 file changed

+20
-17
lines changed

staging/src/k8s.io/apiserver/pkg/endpoints/metrics/metrics.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -299,20 +299,17 @@ func RecordRequestTermination(req *http.Request, requestInfo *request.RequestInf
299299
requestInfo = &request.RequestInfo{Verb: req.Method, Path: req.URL.Path}
300300
}
301301
scope := CleanScope(requestInfo)
302-
// We don't use verb from <requestInfo>, as for the healthy path
303-
// MonitorRequest is called from InstrumentRouteFunc which is registered
304-
// in installer.go with predefined list of verbs (different than those
305-
// translated to RequestInfo).
302+
303+
// We don't use verb from <requestInfo>, as this may be propagated from
304+
// InstrumentRouteFunc which is registered in installer.go with predefined
305+
// list of verbs (different than those translated to RequestInfo).
306306
// However, we need to tweak it e.g. to differentiate GET from LIST.
307-
verb := canonicalVerb(strings.ToUpper(req.Method), scope)
308-
// set verbs to a bounded set of known and expected verbs
309-
if !validRequestMethods.Has(verb) {
310-
verb = OtherRequestMethod
311-
}
307+
reportedVerb := cleanVerb(canonicalVerb(strings.ToUpper(req.Method), scope), req)
308+
312309
if requestInfo.IsResourceRequest {
313-
requestTerminationsTotal.WithLabelValues(cleanVerb(verb, req), requestInfo.APIGroup, requestInfo.APIVersion, requestInfo.Resource, requestInfo.Subresource, scope, component, codeToString(code)).Inc()
310+
requestTerminationsTotal.WithLabelValues(reportedVerb, requestInfo.APIGroup, requestInfo.APIVersion, requestInfo.Resource, requestInfo.Subresource, scope, component, codeToString(code)).Inc()
314311
} else {
315-
requestTerminationsTotal.WithLabelValues(cleanVerb(verb, req), "", "", "", requestInfo.Path, scope, component, codeToString(code)).Inc()
312+
requestTerminationsTotal.WithLabelValues(reportedVerb, "", "", "", requestInfo.Path, scope, component, codeToString(code)).Inc()
316313
}
317314
}
318315

@@ -324,12 +321,13 @@ func RecordLongRunning(req *http.Request, requestInfo *request.RequestInfo, comp
324321
}
325322
var g compbasemetrics.GaugeMetric
326323
scope := CleanScope(requestInfo)
327-
// We don't use verb from <requestInfo>, as for the healthy path
328-
// MonitorRequest is called from InstrumentRouteFunc which is registered
329-
// in installer.go with predefined list of verbs (different than those
330-
// translated to RequestInfo).
324+
325+
// We don't use verb from <requestInfo>, as this may be propagated from
326+
// InstrumentRouteFunc which is registered in installer.go with predefined
327+
// list of verbs (different than those translated to RequestInfo).
331328
// However, we need to tweak it e.g. to differentiate GET from LIST.
332329
reportedVerb := cleanVerb(canonicalVerb(strings.ToUpper(req.Method), scope), req)
330+
333331
if requestInfo.IsResourceRequest {
334332
g = longRunningRequestGauge.WithLabelValues(reportedVerb, requestInfo.APIGroup, requestInfo.APIVersion, requestInfo.Resource, requestInfo.Subresource, scope, component)
335333
} else {
@@ -343,7 +341,12 @@ func RecordLongRunning(req *http.Request, requestInfo *request.RequestInfo, comp
343341
// MonitorRequest handles standard transformations for client and the reported verb and then invokes Monitor to record
344342
// a request. verb must be uppercase to be backwards compatible with existing monitoring tooling.
345343
func MonitorRequest(req *http.Request, verb, group, version, resource, subresource, scope, component string, deprecated bool, removedRelease string, contentType string, httpCode, respSize int, elapsed time.Duration) {
346-
reportedVerb := cleanVerb(verb, req)
344+
// We don't use verb from <requestInfo>, as this may be propagated from
345+
// InstrumentRouteFunc which is registered in installer.go with predefined
346+
// list of verbs (different than those translated to RequestInfo).
347+
// However, we need to tweak it e.g. to differentiate GET from LIST.
348+
reportedVerb := cleanVerb(canonicalVerb(strings.ToUpper(req.Method), scope), req)
349+
347350
dryRun := cleanDryRun(req.URL)
348351
elapsedSeconds := elapsed.Seconds()
349352
cleanContentType := cleanContentType(contentType)
@@ -440,7 +443,7 @@ func CleanScope(requestInfo *request.RequestInfo) string {
440443
func canonicalVerb(verb string, scope string) string {
441444
switch verb {
442445
case "GET", "HEAD":
443-
if scope != "resource" {
446+
if scope != "resource" && scope != "" {
444447
return "LIST"
445448
}
446449
return "GET"

0 commit comments

Comments
 (0)