Skip to content

Commit

Permalink
Fix metrics reporting for the deprecated watch path
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtek-t committed Aug 6, 2021
1 parent 67ab286 commit 77bc9d6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
17 changes: 12 additions & 5 deletions staging/src/k8s.io/apiserver/pkg/endpoints/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func RecordRequestAbort(req *http.Request, requestInfo *request.RequestInfo) {
}

scope := CleanScope(requestInfo)
reportedVerb := cleanVerb(canonicalVerb(strings.ToUpper(req.Method), scope), req)
reportedVerb := cleanVerb(canonicalVerb(strings.ToUpper(req.Method), scope), "", req)
resource := requestInfo.Resource
subresource := requestInfo.Subresource
group := requestInfo.APIGroup
Expand All @@ -358,7 +358,7 @@ func RecordRequestTermination(req *http.Request, requestInfo *request.RequestInf
// InstrumentRouteFunc which is registered in installer.go with predefined
// list of verbs (different than those translated to RequestInfo).
// However, we need to tweak it e.g. to differentiate GET from LIST.
reportedVerb := cleanVerb(canonicalVerb(strings.ToUpper(req.Method), scope), req)
reportedVerb := cleanVerb(canonicalVerb(strings.ToUpper(req.Method), scope), "", req)

if requestInfo.IsResourceRequest {
requestTerminationsTotal.WithLabelValues(reportedVerb, requestInfo.APIGroup, requestInfo.APIVersion, requestInfo.Resource, requestInfo.Subresource, scope, component, codeToString(code)).Inc()
Expand All @@ -380,7 +380,7 @@ func RecordLongRunning(req *http.Request, requestInfo *request.RequestInfo, comp
// InstrumentRouteFunc which is registered in installer.go with predefined
// list of verbs (different than those translated to RequestInfo).
// However, we need to tweak it e.g. to differentiate GET from LIST.
reportedVerb := cleanVerb(canonicalVerb(strings.ToUpper(req.Method), scope), req)
reportedVerb := cleanVerb(canonicalVerb(strings.ToUpper(req.Method), scope), "", req)

if requestInfo.IsResourceRequest {
g = longRunningRequestGauge.WithLabelValues(reportedVerb, requestInfo.APIGroup, requestInfo.APIVersion, requestInfo.Resource, requestInfo.Subresource, scope, component)
Expand All @@ -399,7 +399,7 @@ func MonitorRequest(req *http.Request, verb, group, version, resource, subresour
// InstrumentRouteFunc which is registered in installer.go with predefined
// list of verbs (different than those translated to RequestInfo).
// However, we need to tweak it e.g. to differentiate GET from LIST.
reportedVerb := cleanVerb(canonicalVerb(strings.ToUpper(req.Method), scope), req)
reportedVerb := cleanVerb(canonicalVerb(strings.ToUpper(req.Method), scope), verb, req)

dryRun := cleanDryRun(req.URL)
elapsedSeconds := elapsed.Seconds()
Expand Down Expand Up @@ -517,8 +517,15 @@ func canonicalVerb(verb string, scope string) string {
}
}

func cleanVerb(verb string, request *http.Request) string {
func cleanVerb(verb, suggestedVerb string, request *http.Request) string {
reportedVerb := verb
// CanonicalVerb (being an input for this function) doesn't handle correctly the
// deprecated path pattern for watch of:
// GET /api/{version}/watch/{resource}
// We correct it manually based on the pass verb from the installer.
if suggestedVerb == "WATCH" || suggestedVerb == "WATCHLIST" {
reportedVerb = "WATCH"
}
if verb == "LIST" {
// see apimachinery/pkg/runtime/conversion.go Convert_Slice_string_To_bool
if values := request.URL.Query()["watch"]; len(values) > 0 {
Expand Down
33 changes: 28 additions & 5 deletions staging/src/k8s.io/apiserver/pkg/endpoints/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import (

func TestCleanVerb(t *testing.T) {
testCases := []struct {
desc string
initialVerb string
request *http.Request
expectedVerb string
desc string
initialVerb string
suggestedVerb string
request *http.Request
expectedVerb string
}{
{
desc: "An empty string should be designated as unknown",
Expand Down Expand Up @@ -62,6 +63,28 @@ func TestCleanVerb(t *testing.T) {
},
expectedVerb: "LIST",
},
{
desc: "LIST is transformed to WATCH for the old pattern watch",
initialVerb: "LIST",
suggestedVerb: "WATCH",
request: &http.Request{
URL: &url.URL{
RawQuery: "/api/v1/watch/pods",
},
},
expectedVerb: "WATCH",
},
{
desc: "LIST is transformed to WATCH for the old pattern watchlist",
initialVerb: "LIST",
suggestedVerb: "WATCHLIST",
request: &http.Request{
URL: &url.URL{
RawQuery: "/api/v1/watch/pods",
},
},
expectedVerb: "WATCH",
},
{
desc: "WATCHLIST should be transformed to WATCH",
initialVerb: "WATCHLIST",
Expand Down Expand Up @@ -103,7 +126,7 @@ func TestCleanVerb(t *testing.T) {
if tt.request != nil {
req = tt.request
}
cleansedVerb := cleanVerb(tt.initialVerb, req)
cleansedVerb := cleanVerb(tt.initialVerb, tt.suggestedVerb, req)
if cleansedVerb != tt.expectedVerb {
t.Errorf("Got %s, but expected %s", cleansedVerb, tt.expectedVerb)
}
Expand Down

0 comments on commit 77bc9d6

Please sign in to comment.