mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
Fix metrics reporting for the deprecated watch path
This commit is contained in:
parent
2d08fd4f56
commit
d9d41b70f6
@ -372,7 +372,7 @@ func RecordRequestAbort(req *http.Request, requestInfo *request.RequestInfo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
scope := CleanScope(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
|
resource := requestInfo.Resource
|
||||||
subresource := requestInfo.Subresource
|
subresource := requestInfo.Subresource
|
||||||
group := requestInfo.APIGroup
|
group := requestInfo.APIGroup
|
||||||
@ -395,7 +395,7 @@ func RecordRequestTermination(req *http.Request, requestInfo *request.RequestInf
|
|||||||
// InstrumentRouteFunc which is registered in installer.go with predefined
|
// InstrumentRouteFunc which is registered in installer.go with predefined
|
||||||
// list of verbs (different than those translated to RequestInfo).
|
// list of verbs (different than those translated to RequestInfo).
|
||||||
// However, we need to tweak it e.g. to differentiate GET from LIST.
|
// 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 {
|
if requestInfo.IsResourceRequest {
|
||||||
requestTerminationsTotal.WithContext(req.Context()).WithLabelValues(reportedVerb, requestInfo.APIGroup, requestInfo.APIVersion, requestInfo.Resource, requestInfo.Subresource, scope, component, codeToString(code)).Inc()
|
requestTerminationsTotal.WithContext(req.Context()).WithLabelValues(reportedVerb, requestInfo.APIGroup, requestInfo.APIVersion, requestInfo.Resource, requestInfo.Subresource, scope, component, codeToString(code)).Inc()
|
||||||
@ -417,7 +417,7 @@ func RecordLongRunning(req *http.Request, requestInfo *request.RequestInfo, comp
|
|||||||
// InstrumentRouteFunc which is registered in installer.go with predefined
|
// InstrumentRouteFunc which is registered in installer.go with predefined
|
||||||
// list of verbs (different than those translated to RequestInfo).
|
// list of verbs (different than those translated to RequestInfo).
|
||||||
// However, we need to tweak it e.g. to differentiate GET from LIST.
|
// 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 {
|
if requestInfo.IsResourceRequest {
|
||||||
g = longRunningRequestGauge.WithContext(req.Context()).WithLabelValues(reportedVerb, requestInfo.APIGroup, requestInfo.APIVersion, requestInfo.Resource, requestInfo.Subresource, scope, component)
|
g = longRunningRequestGauge.WithContext(req.Context()).WithLabelValues(reportedVerb, requestInfo.APIGroup, requestInfo.APIVersion, requestInfo.Resource, requestInfo.Subresource, scope, component)
|
||||||
@ -436,7 +436,7 @@ func MonitorRequest(req *http.Request, verb, group, version, resource, subresour
|
|||||||
// InstrumentRouteFunc which is registered in installer.go with predefined
|
// InstrumentRouteFunc which is registered in installer.go with predefined
|
||||||
// list of verbs (different than those translated to RequestInfo).
|
// list of verbs (different than those translated to RequestInfo).
|
||||||
// However, we need to tweak it e.g. to differentiate GET from LIST.
|
// 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)
|
dryRun := cleanDryRun(req.URL)
|
||||||
elapsedSeconds := elapsed.Seconds()
|
elapsedSeconds := elapsed.Seconds()
|
||||||
@ -566,8 +566,15 @@ func CleanVerb(verb string, request *http.Request) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// cleanVerb additionally ensures that unknown verbs don't clog up the metrics.
|
// cleanVerb additionally ensures that unknown verbs don't clog up the metrics.
|
||||||
func cleanVerb(verb string, request *http.Request) string {
|
func cleanVerb(verb, suggestedVerb string, request *http.Request) string {
|
||||||
reportedVerb := CleanVerb(verb, request)
|
reportedVerb := CleanVerb(verb, request)
|
||||||
|
// 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 validRequestMethods.Has(reportedVerb) {
|
if validRequestMethods.Has(reportedVerb) {
|
||||||
return reportedVerb
|
return reportedVerb
|
||||||
}
|
}
|
||||||
|
@ -26,10 +26,11 @@ import (
|
|||||||
|
|
||||||
func TestCleanVerb(t *testing.T) {
|
func TestCleanVerb(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
initialVerb string
|
initialVerb string
|
||||||
request *http.Request
|
suggestedVerb string
|
||||||
expectedVerb string
|
request *http.Request
|
||||||
|
expectedVerb string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
desc: "An empty string should be designated as unknown",
|
desc: "An empty string should be designated as unknown",
|
||||||
@ -63,6 +64,28 @@ func TestCleanVerb(t *testing.T) {
|
|||||||
},
|
},
|
||||||
expectedVerb: "LIST",
|
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",
|
desc: "WATCHLIST should be transformed to WATCH",
|
||||||
initialVerb: "WATCHLIST",
|
initialVerb: "WATCHLIST",
|
||||||
@ -104,7 +127,7 @@ func TestCleanVerb(t *testing.T) {
|
|||||||
if tt.request != nil {
|
if tt.request != nil {
|
||||||
req = tt.request
|
req = tt.request
|
||||||
}
|
}
|
||||||
cleansedVerb := cleanVerb(tt.initialVerb, req)
|
cleansedVerb := cleanVerb(tt.initialVerb, tt.suggestedVerb, req)
|
||||||
if cleansedVerb != tt.expectedVerb {
|
if cleansedVerb != tt.expectedVerb {
|
||||||
t.Errorf("Got %s, but expected %s", cleansedVerb, tt.expectedVerb)
|
t.Errorf("Got %s, but expected %s", cleansedVerb, tt.expectedVerb)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user