mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Merge pull request #88609 from yue9944882/chore/follow-up-metrics
Preserve legacy inflight metrics and fixes registration
This commit is contained in:
commit
d115206309
@ -29,7 +29,6 @@ require (
|
|||||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822
|
||||||
github.com/pkg/errors v0.8.1
|
github.com/pkg/errors v0.8.1
|
||||||
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 // indirect
|
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 // indirect
|
||||||
github.com/prometheus/client_golang v1.0.0
|
|
||||||
github.com/prometheus/client_model v0.2.0
|
github.com/prometheus/client_model v0.2.0
|
||||||
github.com/sirupsen/logrus v1.4.2 // indirect
|
github.com/sirupsen/logrus v1.4.2 // indirect
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
|
@ -20,11 +20,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync/atomic"
|
||||||
// TODO: decide whether to also generate the old metrics, which
|
|
||||||
// categorize according to mutating vs readonly.
|
|
||||||
|
|
||||||
// "k8s.io/apiserver/pkg/endpoints/metrics"
|
|
||||||
|
|
||||||
fcv1a1 "k8s.io/api/flowcontrol/v1alpha1"
|
fcv1a1 "k8s.io/api/flowcontrol/v1alpha1"
|
||||||
apitypes "k8s.io/apimachinery/pkg/types"
|
apitypes "k8s.io/apimachinery/pkg/types"
|
||||||
@ -38,8 +34,8 @@ type priorityAndFairnessKeyType int
|
|||||||
const priorityAndFairnessKey priorityAndFairnessKeyType = iota
|
const priorityAndFairnessKey priorityAndFairnessKeyType = iota
|
||||||
|
|
||||||
const (
|
const (
|
||||||
responseHeaderMatchedPriorityLevelConfigurationUID = "X-Kubernetes-PF-PriorityLevelUID"
|
responseHeaderMatchedPriorityLevelConfigurationUID = "X-Kubernetes-PF-PriorityLevel-UID"
|
||||||
responseHeaderMatchedFlowSchemaUID = "X-Kubernetes-PF-FlowSchemaUID"
|
responseHeaderMatchedFlowSchemaUID = "X-Kubernetes-PF-FlowSchema-UID"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PriorityAndFairnessClassification identifies the results of
|
// PriorityAndFairnessClassification identifies the results of
|
||||||
@ -57,6 +53,8 @@ func GetClassification(ctx context.Context) *PriorityAndFairnessClassification {
|
|||||||
return ctx.Value(priorityAndFairnessKey).(*PriorityAndFairnessClassification)
|
return ctx.Value(priorityAndFairnessKey).(*PriorityAndFairnessClassification)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var atomicMutatingLen, atomicNonMutatingLen int32
|
||||||
|
|
||||||
// WithPriorityAndFairness limits the number of in-flight
|
// WithPriorityAndFairness limits the number of in-flight
|
||||||
// requests in a fine-grained way.
|
// requests in a fine-grained way.
|
||||||
func WithPriorityAndFairness(
|
func WithPriorityAndFairness(
|
||||||
@ -68,7 +66,7 @@ func WithPriorityAndFairness(
|
|||||||
klog.Warningf("priority and fairness support not found, skipping")
|
klog.Warningf("priority and fairness support not found, skipping")
|
||||||
return handler
|
return handler
|
||||||
}
|
}
|
||||||
|
startOnce.Do(startRecordingUsage)
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
requestInfo, ok := apirequest.RequestInfoFrom(ctx)
|
requestInfo, ok := apirequest.RequestInfoFrom(ctx)
|
||||||
@ -97,8 +95,25 @@ func WithPriorityAndFairness(
|
|||||||
PriorityLevelName: pl.Name,
|
PriorityLevelName: pl.Name,
|
||||||
PriorityLevelUID: pl.UID}
|
PriorityLevelUID: pl.UID}
|
||||||
}
|
}
|
||||||
|
|
||||||
var served bool
|
var served bool
|
||||||
|
isMutatingRequest := !nonMutatingRequestVerbs.Has(requestInfo.Verb)
|
||||||
execute := func() {
|
execute := func() {
|
||||||
|
var mutatingLen, readOnlyLen int
|
||||||
|
if isMutatingRequest {
|
||||||
|
mutatingLen = int(atomic.AddInt32(&atomicMutatingLen, 1))
|
||||||
|
} else {
|
||||||
|
readOnlyLen = int(atomic.AddInt32(&atomicNonMutatingLen, 1))
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if isMutatingRequest {
|
||||||
|
atomic.AddInt32(&atomicMutatingLen, -11)
|
||||||
|
watermark.recordMutating(mutatingLen)
|
||||||
|
} else {
|
||||||
|
atomic.AddInt32(&atomicNonMutatingLen, -1)
|
||||||
|
watermark.recordReadOnly(readOnlyLen)
|
||||||
|
}
|
||||||
|
}()
|
||||||
served = true
|
served = true
|
||||||
innerCtx := context.WithValue(ctx, priorityAndFairnessKey, classification)
|
innerCtx := context.WithValue(ctx, priorityAndFairnessKey, classification)
|
||||||
innerReq := r.Clone(innerCtx)
|
innerReq := r.Clone(innerCtx)
|
||||||
|
@ -26,6 +26,7 @@ go_library(
|
|||||||
"//staging/src/k8s.io/apiserver/pkg/endpoints/metrics:go_default_library",
|
"//staging/src/k8s.io/apiserver/pkg/endpoints/metrics:go_default_library",
|
||||||
"//staging/src/k8s.io/apiserver/pkg/server/mux:go_default_library",
|
"//staging/src/k8s.io/apiserver/pkg/server/mux:go_default_library",
|
||||||
"//staging/src/k8s.io/apiserver/pkg/storage/etcd3/metrics:go_default_library",
|
"//staging/src/k8s.io/apiserver/pkg/storage/etcd3/metrics:go_default_library",
|
||||||
|
"//staging/src/k8s.io/apiserver/pkg/util/flowcontrol/metrics:go_default_library",
|
||||||
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
|
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
|
||||||
"//vendor/github.com/emicklei/go-restful:go_default_library",
|
"//vendor/github.com/emicklei/go-restful:go_default_library",
|
||||||
"//vendor/github.com/go-openapi/spec:go_default_library",
|
"//vendor/github.com/go-openapi/spec:go_default_library",
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
apimetrics "k8s.io/apiserver/pkg/endpoints/metrics"
|
apimetrics "k8s.io/apiserver/pkg/endpoints/metrics"
|
||||||
"k8s.io/apiserver/pkg/server/mux"
|
"k8s.io/apiserver/pkg/server/mux"
|
||||||
etcd3metrics "k8s.io/apiserver/pkg/storage/etcd3/metrics"
|
etcd3metrics "k8s.io/apiserver/pkg/storage/etcd3/metrics"
|
||||||
|
flowcontrolmetrics "k8s.io/apiserver/pkg/util/flowcontrol/metrics"
|
||||||
"k8s.io/component-base/metrics/legacyregistry"
|
"k8s.io/component-base/metrics/legacyregistry"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -58,4 +59,5 @@ func (m MetricsWithReset) Install(c *mux.PathRecorderMux) {
|
|||||||
func register() {
|
func register() {
|
||||||
apimetrics.Register()
|
apimetrics.Register()
|
||||||
etcd3metrics.Register()
|
etcd3metrics.Register()
|
||||||
|
flowcontrolmetrics.Register()
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,10 @@ go_library(
|
|||||||
importmap = "k8s.io/kubernetes/vendor/k8s.io/apiserver/pkg/util/flowcontrol/metrics",
|
importmap = "k8s.io/kubernetes/vendor/k8s.io/apiserver/pkg/util/flowcontrol/metrics",
|
||||||
importpath = "k8s.io/apiserver/pkg/util/flowcontrol/metrics",
|
importpath = "k8s.io/apiserver/pkg/util/flowcontrol/metrics",
|
||||||
visibility = ["//visibility:public"],
|
visibility = ["//visibility:public"],
|
||||||
deps = ["//vendor/github.com/prometheus/client_golang/prometheus:go_default_library"],
|
deps = [
|
||||||
|
"//staging/src/k8s.io/component-base/metrics:go_default_library",
|
||||||
|
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
|
||||||
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
filegroup(
|
filegroup(
|
||||||
|
@ -17,9 +17,11 @@ limitations under the License.
|
|||||||
package metrics
|
package metrics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
compbasemetrics "k8s.io/component-base/metrics"
|
||||||
|
"k8s.io/component-base/metrics/legacyregistry"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -37,28 +39,29 @@ var (
|
|||||||
requestDurationSecondsBuckets = []float64{0, 0.005, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 30}
|
requestDurationSecondsBuckets = []float64{0, 0.005, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 30}
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
var registerMetrics sync.Once
|
||||||
prometheus.MustRegister(apiserverRejectedRequests)
|
|
||||||
prometheus.MustRegister(apiserverCurrentInqueueRequests)
|
// Register all metrics.
|
||||||
prometheus.MustRegister(apiserverRequestQueueLength)
|
func Register() {
|
||||||
prometheus.MustRegister(apiserverRequestConcurrencyLimit)
|
registerMetrics.Do(func() {
|
||||||
prometheus.MustRegister(apiserverCurrentExecutingRequests)
|
for _, metric := range metrics {
|
||||||
prometheus.MustRegister(apiserverRequestWaitingSeconds)
|
legacyregistry.MustRegister(metric)
|
||||||
prometheus.MustRegister(apiserverRequestExecutionSeconds)
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
apiserverRejectedRequests = prometheus.NewCounterVec(
|
apiserverRejectedRequestsTotal = compbasemetrics.NewCounterVec(
|
||||||
prometheus.CounterOpts{
|
&compbasemetrics.CounterOpts{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
Subsystem: subsystem,
|
Subsystem: subsystem,
|
||||||
Name: "rejected_requests",
|
Name: "rejected_requests_total",
|
||||||
Help: "Number of rejected requests by api priority and fairness system",
|
Help: "Number of rejected requests by api priority and fairness system",
|
||||||
},
|
},
|
||||||
[]string{priorityLevel, "reason"},
|
[]string{priorityLevel, "reason"},
|
||||||
)
|
)
|
||||||
apiserverCurrentInqueueRequests = prometheus.NewGaugeVec(
|
apiserverCurrentInqueueRequests = compbasemetrics.NewGaugeVec(
|
||||||
prometheus.GaugeOpts{
|
&compbasemetrics.GaugeOpts{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
Subsystem: subsystem,
|
Subsystem: subsystem,
|
||||||
Name: "current_inqueue_requests",
|
Name: "current_inqueue_requests",
|
||||||
@ -66,8 +69,8 @@ var (
|
|||||||
},
|
},
|
||||||
[]string{priorityLevel},
|
[]string{priorityLevel},
|
||||||
)
|
)
|
||||||
apiserverRequestQueueLength = prometheus.NewHistogramVec(
|
apiserverRequestQueueLength = compbasemetrics.NewHistogramVec(
|
||||||
prometheus.HistogramOpts{
|
&compbasemetrics.HistogramOpts{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
Subsystem: subsystem,
|
Subsystem: subsystem,
|
||||||
Name: "request_queue_length",
|
Name: "request_queue_length",
|
||||||
@ -76,8 +79,8 @@ var (
|
|||||||
},
|
},
|
||||||
[]string{priorityLevel},
|
[]string{priorityLevel},
|
||||||
)
|
)
|
||||||
apiserverRequestConcurrencyLimit = prometheus.NewGaugeVec(
|
apiserverRequestConcurrencyLimit = compbasemetrics.NewGaugeVec(
|
||||||
prometheus.GaugeOpts{
|
&compbasemetrics.GaugeOpts{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
Subsystem: subsystem,
|
Subsystem: subsystem,
|
||||||
Name: "request_concurrency_limit",
|
Name: "request_concurrency_limit",
|
||||||
@ -85,8 +88,8 @@ var (
|
|||||||
},
|
},
|
||||||
[]string{priorityLevel},
|
[]string{priorityLevel},
|
||||||
)
|
)
|
||||||
apiserverCurrentExecutingRequests = prometheus.NewGaugeVec(
|
apiserverCurrentExecutingRequests = compbasemetrics.NewGaugeVec(
|
||||||
prometheus.GaugeOpts{
|
&compbasemetrics.GaugeOpts{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
Subsystem: subsystem,
|
Subsystem: subsystem,
|
||||||
Name: "current_executing_requests",
|
Name: "current_executing_requests",
|
||||||
@ -94,8 +97,8 @@ var (
|
|||||||
},
|
},
|
||||||
[]string{priorityLevel},
|
[]string{priorityLevel},
|
||||||
)
|
)
|
||||||
apiserverRequestWaitingSeconds = prometheus.NewHistogramVec(
|
apiserverRequestWaitingSeconds = compbasemetrics.NewHistogramVec(
|
||||||
prometheus.HistogramOpts{
|
&compbasemetrics.HistogramOpts{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
Subsystem: subsystem,
|
Subsystem: subsystem,
|
||||||
Name: "request_wait_duration_seconds",
|
Name: "request_wait_duration_seconds",
|
||||||
@ -104,8 +107,8 @@ var (
|
|||||||
},
|
},
|
||||||
[]string{priorityLevel, flowSchema, "execute"},
|
[]string{priorityLevel, flowSchema, "execute"},
|
||||||
)
|
)
|
||||||
apiserverRequestExecutionSeconds = prometheus.NewHistogramVec(
|
apiserverRequestExecutionSeconds = compbasemetrics.NewHistogramVec(
|
||||||
prometheus.HistogramOpts{
|
&compbasemetrics.HistogramOpts{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
Subsystem: subsystem,
|
Subsystem: subsystem,
|
||||||
Name: "request_execution_seconds",
|
Name: "request_execution_seconds",
|
||||||
@ -114,6 +117,15 @@ var (
|
|||||||
},
|
},
|
||||||
[]string{priorityLevel, flowSchema},
|
[]string{priorityLevel, flowSchema},
|
||||||
)
|
)
|
||||||
|
metrics = []compbasemetrics.Registerable{
|
||||||
|
apiserverRejectedRequestsTotal,
|
||||||
|
apiserverCurrentInqueueRequests,
|
||||||
|
apiserverRequestQueueLength,
|
||||||
|
apiserverRequestConcurrencyLimit,
|
||||||
|
apiserverCurrentExecutingRequests,
|
||||||
|
apiserverRequestWaitingSeconds,
|
||||||
|
apiserverRequestExecutionSeconds,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// UpdateFlowControlRequestsInQueue updates the value for the # of requests in the specified queues in flow control
|
// UpdateFlowControlRequestsInQueue updates the value for the # of requests in the specified queues in flow control
|
||||||
@ -133,7 +145,7 @@ func UpdateSharedConcurrencyLimit(priorityLevel string, limit int) {
|
|||||||
|
|
||||||
// AddReject increments the # of rejected requests for flow control
|
// AddReject increments the # of rejected requests for flow control
|
||||||
func AddReject(priorityLevel string, reason string) {
|
func AddReject(priorityLevel string, reason string) {
|
||||||
apiserverRejectedRequests.WithLabelValues(priorityLevel, reason).Add(1)
|
apiserverRejectedRequestsTotal.WithLabelValues(priorityLevel, reason).Add(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ObserveQueueLength observes the queue length for flow control
|
// ObserveQueueLength observes the queue length for flow control
|
||||||
|
Loading…
Reference in New Issue
Block a user