Skip to content

Commit

Permalink
migrate scheduler metrics endpoint to metrics stability framework
Browse files Browse the repository at this point in the history
  • Loading branch information
logicalhan committed Aug 28, 2019
1 parent f0be447 commit 8da448d
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 144 deletions.
3 changes: 1 addition & 2 deletions cmd/kube-scheduler/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ go_library(
importpath = "k8s.io/kubernetes/cmd/kube-scheduler",
deps = [
"//cmd/kube-scheduler/app:go_default_library",
"//pkg/util/prometheusclientgo:go_default_library",
"//pkg/version/prometheus:go_default_library",
"//staging/src/k8s.io/component-base/cli/flag:go_default_library",
"//staging/src/k8s.io/component-base/logs:go_default_library",
"//staging/src/k8s.io/component-base/metrics/prometheus/clientgo:go_default_library",
"//vendor/github.com/spf13/pflag:go_default_library",
],
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kube-scheduler/app/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ go_library(
"//staging/src/k8s.io/client-go/tools/leaderelection:go_default_library",
"//staging/src/k8s.io/component-base/cli/flag:go_default_library",
"//staging/src/k8s.io/component-base/cli/globalflag:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus:go_default_library",
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
"//vendor/github.com/spf13/cobra:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
],
Expand Down
10 changes: 5 additions & 5 deletions cmd/kube-scheduler/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"os"
goruntime "runtime"

"github.com/spf13/cobra"

utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/apiserver/pkg/authorization/authorizer"
Expand All @@ -39,6 +41,8 @@ import (
"k8s.io/client-go/tools/leaderelection"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/cli/globalflag"
"k8s.io/component-base/metrics/legacyregistry"
"k8s.io/klog"
schedulerserverconfig "k8s.io/kubernetes/cmd/kube-scheduler/app/config"
"k8s.io/kubernetes/cmd/kube-scheduler/app/options"
"k8s.io/kubernetes/pkg/api/legacyscheme"
Expand All @@ -51,10 +55,6 @@ import (
utilflag "k8s.io/kubernetes/pkg/util/flag"
"k8s.io/kubernetes/pkg/version"
"k8s.io/kubernetes/pkg/version/verflag"

"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/cobra"
"k8s.io/klog"
)

// Option configures a framework.Registry.
Expand Down Expand Up @@ -294,7 +294,7 @@ func buildHandlerChain(handler http.Handler, authn authenticator.Request, authz

func installMetricHandler(pathRecorderMux *mux.PathRecorderMux) {
configz.InstallHandler(pathRecorderMux)
defaultMetricsHandler := prometheus.Handler().ServeHTTP
defaultMetricsHandler := legacyregistry.Handler().ServeHTTP
pathRecorderMux.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
if req.Method == "DELETE" {
metrics.Reset()
Expand Down
3 changes: 1 addition & 2 deletions cmd/kube-scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import (

cliflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/logs"
_ "k8s.io/component-base/metrics/prometheus/clientgo"
"k8s.io/kubernetes/cmd/kube-scheduler/app"
_ "k8s.io/kubernetes/pkg/util/prometheusclientgo" // load all the prometheus client-go plugins
_ "k8s.io/kubernetes/pkg/version/prometheus" // for version metric registration
)

func main() {
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/volume/scheduling/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ go_library(
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
"//staging/src/k8s.io/client-go/listers/storage/v1:go_default_library",
"//staging/src/k8s.io/client-go/tools/cache:go_default_library",
"//staging/src/k8s.io/component-base/metrics:go_default_library",
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
],
Expand Down
44 changes: 25 additions & 19 deletions pkg/controller/volume/scheduling/scheduler_bind_cache_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,43 @@ package scheduling

import (
"github.com/prometheus/client_golang/prometheus"

"k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
)

// VolumeSchedulerSubsystem - subsystem name used by scheduler
const VolumeSchedulerSubsystem = "scheduler_volume"

var (
// VolumeBindingRequestSchedulerBinderCache tracks the number of volume binder cache operations.
VolumeBindingRequestSchedulerBinderCache = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: VolumeSchedulerSubsystem,
Name: "binder_cache_requests_total",
Help: "Total number for request volume binding cache",
VolumeBindingRequestSchedulerBinderCache = metrics.NewCounterVec(
&metrics.CounterOpts{
Subsystem: VolumeSchedulerSubsystem,
Name: "binder_cache_requests_total",
Help: "Total number for request volume binding cache",
StabilityLevel: metrics.ALPHA,
},
[]string{"operation"},
)
// VolumeSchedulingStageLatency tracks the latency of volume scheduling operations.
VolumeSchedulingStageLatency = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: VolumeSchedulerSubsystem,
Name: "scheduling_duration_seconds",
Help: "Volume scheduling stage latency",
Buckets: prometheus.ExponentialBuckets(1000, 2, 15),
VolumeSchedulingStageLatency = metrics.NewHistogramVec(
&metrics.HistogramOpts{
Subsystem: VolumeSchedulerSubsystem,
Name: "scheduling_duration_seconds",
Help: "Volume scheduling stage latency",
Buckets: prometheus.ExponentialBuckets(1000, 2, 15),
StabilityLevel: metrics.ALPHA,
},
[]string{"operation"},
)
// VolumeSchedulingStageFailed tracks the number of failed volume scheduling operations.
VolumeSchedulingStageFailed = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: VolumeSchedulerSubsystem,
Name: "scheduling_stage_error_total",
Help: "Volume scheduling stage error count",
VolumeSchedulingStageFailed = metrics.NewCounterVec(
&metrics.CounterOpts{
Subsystem: VolumeSchedulerSubsystem,
Name: "scheduling_stage_error_total",
Help: "Volume scheduling stage error count",
StabilityLevel: metrics.ALPHA,
},
[]string{"operation"},
)
Expand All @@ -57,7 +63,7 @@ var (
// RegisterVolumeSchedulingMetrics is used for scheduler, because the volume binding cache is a library
// used by scheduler process.
func RegisterVolumeSchedulingMetrics() {
prometheus.MustRegister(VolumeBindingRequestSchedulerBinderCache)
prometheus.MustRegister(VolumeSchedulingStageLatency)
prometheus.MustRegister(VolumeSchedulingStageFailed)
legacyregistry.MustRegister(VolumeBindingRequestSchedulerBinderCache)
legacyregistry.MustRegister(VolumeSchedulingStageLatency)
legacyregistry.MustRegister(VolumeSchedulingStageFailed)
}
13 changes: 7 additions & 6 deletions pkg/scheduler/internal/queue/scheduling_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,7 @@ func TestPodTimestamp(t *testing.T) {
func TestPendingPodsMetric(t *testing.T) {
total := 50
timestamp := time.Now()
metrics.Register()
var pInfos = make([]*framework.PodInfo, 0, total)
for i := 1; i <= total; i++ {
p := &framework.PodInfo{
Expand Down Expand Up @@ -1312,9 +1313,9 @@ func TestPendingPodsMetric(t *testing.T) {
}

resetMetrics := func() {
metrics.ActivePods.Set(0)
metrics.BackoffPods.Set(0)
metrics.UnschedulablePods.Set(0)
metrics.ActivePods().Set(0)
metrics.BackoffPods().Set(0)
metrics.UnschedulablePods().Set(0)
}

for _, test := range tests {
Expand All @@ -1329,23 +1330,23 @@ func TestPendingPodsMetric(t *testing.T) {

var activeNum, backoffNum, unschedulableNum float64
metricProto := &dto.Metric{}
if err := metrics.ActivePods.Write(metricProto); err != nil {
if err := metrics.ActivePods().Write(metricProto); err != nil {
t.Errorf("error writing ActivePods metric: %v", err)
}
activeNum = metricProto.Gauge.GetValue()
if int64(activeNum) != test.expected[0] {
t.Errorf("ActivePods: Expected %v, got %v", test.expected[0], activeNum)
}

if err := metrics.BackoffPods.Write(metricProto); err != nil {
if err := metrics.BackoffPods().Write(metricProto); err != nil {
t.Errorf("error writing BackoffPods metric: %v", err)
}
backoffNum = metricProto.Gauge.GetValue()
if int64(backoffNum) != test.expected[1] {
t.Errorf("BackoffPods: Expected %v, got %v", test.expected[1], backoffNum)
}

if err := metrics.UnschedulablePods.Write(metricProto); err != nil {
if err := metrics.UnschedulablePods().Write(metricProto); err != nil {
t.Errorf("error writing UnschedulablePods metric: %v", err)
}
unschedulableNum = metricProto.Gauge.GetValue()
Expand Down
2 changes: 2 additions & 0 deletions pkg/scheduler/metrics/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ go_library(
importpath = "k8s.io/kubernetes/pkg/scheduler/metrics",
deps = [
"//pkg/controller/volume/scheduling:go_default_library",
"//staging/src/k8s.io/component-base/metrics:go_default_library",
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus:go_default_library",
],
)
Expand Down
10 changes: 5 additions & 5 deletions pkg/scheduler/metrics/metric_recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package metrics

import (
"github.com/prometheus/client_golang/prometheus"
"k8s.io/component-base/metrics"
)

// MetricRecorder represents a metric recorder which takes action when the
Expand All @@ -32,27 +32,27 @@ var _ MetricRecorder = &PendingPodsRecorder{}

// PendingPodsRecorder is an implementation of MetricRecorder
type PendingPodsRecorder struct {
recorder prometheus.Gauge
recorder metrics.GaugeMetric
}

// NewActivePodsRecorder returns ActivePods in a Prometheus metric fashion
func NewActivePodsRecorder() *PendingPodsRecorder {
return &PendingPodsRecorder{
recorder: ActivePods,
recorder: ActivePods(),
}
}

// NewUnschedulablePodsRecorder returns UnschedulablePods in a Prometheus metric fashion
func NewUnschedulablePodsRecorder() *PendingPodsRecorder {
return &PendingPodsRecorder{
recorder: UnschedulablePods,
recorder: UnschedulablePods(),
}
}

// NewBackoffPodsRecorder returns BackoffPods in a Prometheus metric fashion
func NewBackoffPodsRecorder() *PendingPodsRecorder {
return &PendingPodsRecorder{
recorder: BackoffPods,
recorder: BackoffPods(),
}
}

Expand Down
Loading

0 comments on commit 8da448d

Please sign in to comment.