Skip to content

Commit 3c0fba7

Browse files
stttsjacobsee
authored andcommitted
UPSTREAM: <carry>: apiserver: add system_client=kube-{apiserver,cm,s} to apiserver_request_total
UPSTREAM: <carry>: apiserver: add cluster-policy-controller to system client in apiserver_request_total OpenShift-Rebase-Source: d86823d UPSTREAM: <carry>: apiserver: add system_client=kube-{apiserver,cm,s} to apiserver_request_total Fix TestOpenAPIRequestMetrics unit test. UPSTREAM: <carry>: Add group label to apiSelfRequestCounter metric as it was changed upstream.
1 parent 6803b75 commit 3c0fba7

File tree

6 files changed

+37
-30
lines changed

6 files changed

+37
-30
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3636
utilsets "k8s.io/apimachinery/pkg/util/sets"
3737
"k8s.io/apiserver/pkg/audit"
38-
"k8s.io/apiserver/pkg/authentication/user"
3938
"k8s.io/apiserver/pkg/endpoints/request"
4039
"k8s.io/apiserver/pkg/endpoints/responsewriter"
4140
compbasemetrics "k8s.io/component-base/metrics"
@@ -82,7 +81,7 @@ var (
8281
Help: "Counter of apiserver requests broken out for each verb, dry run value, group, version, resource, scope, component, and HTTP response code.",
8382
StabilityLevel: compbasemetrics.STABLE,
8483
},
85-
[]string{"verb", "dry_run", "group", "version", "resource", "subresource", "scope", "component", "code"},
84+
[]string{"verb", "dry_run", "group", "version", "resource", "subresource", "scope", "component", "code", "system_client"},
8685
)
8786
longRunningRequestsGauge = compbasemetrics.NewGaugeVec(
8887
&compbasemetrics.GaugeOpts{
@@ -519,9 +518,9 @@ func RecordDroppedRequest(req *http.Request, requestInfo *request.RequestInfo, c
519518
reportedVerb := cleanVerb(CanonicalVerb(strings.ToUpper(req.Method), scope), "", req, requestInfo)
520519

521520
if requestInfo.IsResourceRequest {
522-
requestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, dryRun, requestInfo.APIGroup, requestInfo.APIVersion, requestInfo.Resource, requestInfo.Subresource, scope, component, codeToString(http.StatusTooManyRequests)).Inc()
521+
requestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, dryRun, requestInfo.APIGroup, requestInfo.APIVersion, requestInfo.Resource, requestInfo.Subresource, scope, component, codeToString(http.StatusTooManyRequests), "").Inc()
523522
} else {
524-
requestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, dryRun, "", "", "", requestInfo.Subresource, scope, component, codeToString(http.StatusTooManyRequests)).Inc()
523+
requestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, dryRun, "", "", "", requestInfo.Subresource, scope, component, codeToString(http.StatusTooManyRequests), "").Inc()
525524
}
526525
}
527526

@@ -600,12 +599,19 @@ func MonitorRequest(req *http.Request, verb, group, version, resource, subresour
600599

601600
dryRun := cleanDryRun(req.URL)
602601
elapsedSeconds := elapsed.Seconds()
603-
requestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, dryRun, group, version, resource, subresource, scope, component, codeToString(httpCode)).Inc()
604-
// MonitorRequest happens after authentication, so we can trust the username given by the request
605-
info, ok := request.UserFrom(req.Context())
606-
if ok && info.GetName() == user.APIServerUser {
607-
apiSelfRequestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, group, resource, subresource).Inc()
602+
603+
systemClient := ""
604+
if uas := strings.SplitN(req.UserAgent(), "/", 2); len(uas) > 1 {
605+
switch uas[0] {
606+
case "kube-apiserver":
607+
apiSelfRequestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, group, resource, subresource).Inc()
608+
fallthrough
609+
case "kube-controller-manager", "kube-scheduler", "cluster-policy-controller":
610+
systemClient = uas[0]
611+
}
608612
}
613+
requestCounter.WithContext(req.Context()).WithLabelValues(reportedVerb, dryRun, group, version, resource, subresource, scope, component, codeToString(httpCode), systemClient).Inc()
614+
609615
if deprecated {
610616
deprecatedRequestGauge.WithContext(req.Context()).WithLabelValues(group, version, resource, subresource, removedRelease).Set(1)
611617
audit.AddAuditAnnotation(req.Context(), deprecatedAnnotationKey, "true")

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ func TestRecordDroppedRequests(t *testing.T) {
398398
want: `
399399
# HELP apiserver_request_total [STABLE] Counter of apiserver requests broken out for each verb, dry run value, group, version, resource, scope, component, and HTTP response code.
400400
# TYPE apiserver_request_total counter
401-
apiserver_request_total{code="429",component="apiserver",dry_run="",group="",resource="pods",scope="cluster",subresource="",verb="LIST",version="v1"} 1
401+
apiserver_request_total{code="429",component="apiserver",dry_run="",group="",resource="pods",scope="cluster",subresource="",system_client="",verb="LIST",version="v1"} 1
402402
`,
403403
},
404404
{
@@ -420,7 +420,7 @@ func TestRecordDroppedRequests(t *testing.T) {
420420
want: `
421421
# HELP apiserver_request_total [STABLE] Counter of apiserver requests broken out for each verb, dry run value, group, version, resource, scope, component, and HTTP response code.
422422
# TYPE apiserver_request_total counter
423-
apiserver_request_total{code="429",component="apiserver",dry_run="",group="",resource="pods",scope="resource",subresource="",verb="POST",version="v1"} 1
423+
apiserver_request_total{code="429",component="apiserver",dry_run="",group="",resource="pods",scope="resource",subresource="",system_client="",verb="POST",version="v1"} 1
424424
`,
425425
},
426426
{
@@ -445,7 +445,7 @@ func TestRecordDroppedRequests(t *testing.T) {
445445
want: `
446446
# HELP apiserver_request_total [STABLE] Counter of apiserver requests broken out for each verb, dry run value, group, version, resource, scope, component, and HTTP response code.
447447
# TYPE apiserver_request_total counter
448-
apiserver_request_total{code="429",component="apiserver",dry_run="All",group="batch",resource="jobs",scope="resource",subresource="status",verb="PATCH",version="v1"} 1
448+
apiserver_request_total{code="429",component="apiserver",dry_run="All",group="batch",resource="jobs",scope="resource",subresource="status",system_client="",verb="PATCH",version="v1"} 1
449449
`,
450450
},
451451
}

staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ func TestMetrics(t *testing.T) {
255255
expected := strings.NewReader(`
256256
# HELP apiserver_request_total [STABLE] Counter of apiserver requests broken out for each verb, dry run value, group, version, resource, scope, component, and HTTP response code.
257257
# TYPE apiserver_request_total counter
258-
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="/healthz",verb="GET",version=""} 1
259-
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="/livez",verb="GET",version=""} 1
260-
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="/readyz",verb="GET",version=""} 1
258+
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="/healthz",system_client="",verb="GET",version=""} 1
259+
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="/livez",system_client="",verb="GET",version=""} 1
260+
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="/readyz",system_client="",verb="GET",version=""} 1
261261
`)
262262
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, expected, "apiserver_request_total"); err != nil {
263263
t.Error(err)

staging/src/k8s.io/kube-aggregator/pkg/controllers/openapiv3/aggregator/aggregator_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func TestOpenAPIRequestMetrics(t *testing.T) {
278278
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(`
279279
# HELP apiserver_request_total [STABLE] Counter of apiserver requests broken out for each verb, dry run value, group, version, resource, scope, component, and HTTP response code.
280280
# TYPE apiserver_request_total counter
281-
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="openapi/v3",verb="GET",version=""} 1
281+
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="openapi/v3",system_client="",verb="GET",version=""} 1
282282
`), "apiserver_request_total"); err != nil {
283283
t.Fatal(err)
284284
}
@@ -289,8 +289,8 @@ apiserver_request_total{code="200",component="",dry_run="",group="",resource="",
289289
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(`
290290
# HELP apiserver_request_total [STABLE] Counter of apiserver requests broken out for each verb, dry run value, group, version, resource, scope, component, and HTTP response code.
291291
# TYPE apiserver_request_total counter
292-
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="openapi/v3",verb="GET",version=""} 1
293-
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="openapi/v3/",verb="GET",version=""} 1
292+
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="openapi/v3",system_client="",verb="GET",version=""} 1
293+
apiserver_request_total{code="200",component="",dry_run="",group="",resource="",scope="",subresource="openapi/v3/",system_client="",verb="GET",version=""} 1
294294
`), "apiserver_request_total"); err != nil {
295295
t.Fatal(err)
296296
}

test/instrumentation/testdata/stable-metrics-list.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@
374374
- resource
375375
- scope
376376
- subresource
377+
- system_client
377378
- verb
378379
- version
379380
- name: requested_deprecated_apis

test/integration/metrics/metrics_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -420,42 +420,42 @@ func TestAPIServerMetricsPods(t *testing.T) {
420420
executor: func() {
421421
callOrDie(c.Create(context.TODO(), makePod("foo"), metav1.CreateOptions{}))
422422
},
423-
want: `apiserver_request_total{code="201", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", verb="POST", version="v1"}`,
423+
want: `apiserver_request_total{code="201", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", system_client="", verb="POST", version="v1"}`,
424424
},
425425
{
426426
name: "update pod",
427427
executor: func() {
428428
callOrDie(c.Update(context.TODO(), makePod("bar"), metav1.UpdateOptions{}))
429429
},
430-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", verb="PUT", version="v1"}`,
430+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", system_client="", verb="PUT", version="v1"}`,
431431
},
432432
{
433433
name: "update pod status",
434434
executor: func() {
435435
callOrDie(c.UpdateStatus(context.TODO(), makePod("bar"), metav1.UpdateOptions{}))
436436
},
437-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="status", verb="PUT", version="v1"}`,
437+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="status", system_client="", verb="PUT", version="v1"}`,
438438
},
439439
{
440440
name: "get pod",
441441
executor: func() {
442442
callOrDie(c.Get(context.TODO(), "foo", metav1.GetOptions{}))
443443
},
444-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", verb="GET", version="v1"}`,
444+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", system_client="", verb="GET", version="v1"}`,
445445
},
446446
{
447447
name: "list pod",
448448
executor: func() {
449449
callOrDie(c.List(context.TODO(), metav1.ListOptions{}))
450450
},
451-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="namespace", subresource="", verb="LIST", version="v1"}`,
451+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="namespace", subresource="", system_client="", verb="LIST", version="v1"}`,
452452
},
453453
{
454454
name: "delete pod",
455455
executor: func() {
456456
callOrDie(nil, c.Delete(context.TODO(), "foo", metav1.DeleteOptions{}))
457457
},
458-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", verb="DELETE", version="v1"}`,
458+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="pods", scope="resource", subresource="", system_client="", verb="DELETE", version="v1"}`,
459459
},
460460
} {
461461
t.Run(tc.name, func(t *testing.T) {
@@ -528,42 +528,42 @@ func TestAPIServerMetricsNamespaces(t *testing.T) {
528528
executor: func() {
529529
callOrDie(c.Create(context.TODO(), makeNamespace("foo"), metav1.CreateOptions{}))
530530
},
531-
want: `apiserver_request_total{code="201", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", verb="POST", version="v1"}`,
531+
want: `apiserver_request_total{code="201", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", system_client="", verb="POST", version="v1"}`,
532532
},
533533
{
534534
name: "update namespace",
535535
executor: func() {
536536
callOrDie(c.Update(context.TODO(), makeNamespace("bar"), metav1.UpdateOptions{}))
537537
},
538-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", verb="PUT", version="v1"}`,
538+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", system_client="", verb="PUT", version="v1"}`,
539539
},
540540
{
541541
name: "update namespace status",
542542
executor: func() {
543543
callOrDie(c.UpdateStatus(context.TODO(), makeNamespace("bar"), metav1.UpdateOptions{}))
544544
},
545-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="status", verb="PUT", version="v1"}`,
545+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="status", system_client="", verb="PUT", version="v1"}`,
546546
},
547547
{
548548
name: "get namespace",
549549
executor: func() {
550550
callOrDie(c.Get(context.TODO(), "foo", metav1.GetOptions{}))
551551
},
552-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", verb="GET", version="v1"}`,
552+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", system_client="", verb="GET", version="v1"}`,
553553
},
554554
{
555555
name: "list namespace",
556556
executor: func() {
557557
callOrDie(c.List(context.TODO(), metav1.ListOptions{}))
558558
},
559-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="cluster", subresource="", verb="LIST", version="v1"}`,
559+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="cluster", subresource="", system_client="", verb="LIST", version="v1"}`,
560560
},
561561
{
562562
name: "delete namespace",
563563
executor: func() {
564564
callOrDie(nil, c.Delete(context.TODO(), "foo", metav1.DeleteOptions{}))
565565
},
566-
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", verb="DELETE", version="v1"}`,
566+
want: `apiserver_request_total{code="200", component="apiserver", dry_run="", group="", resource="namespaces", scope="resource", subresource="", system_client="", verb="DELETE", version="v1"}`,
567567
},
568568
} {
569569
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)