Skip to content

Commit

Permalink
koord-manager: mv slo-controller metrics to util (#2257)
Browse files Browse the repository at this point in the history
Signed-off-by: 佑祎 <[email protected]>
  • Loading branch information
zwzhang0107 authored Nov 11, 2024
1 parent 81dfad9 commit ae5ae59
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 38 deletions.
11 changes: 5 additions & 6 deletions cmd/koord-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ import (
"github.com/koordinator-sh/koordinator/cmd/koord-manager/options"
extclient "github.com/koordinator-sh/koordinator/pkg/client"
"github.com/koordinator-sh/koordinator/pkg/features"
"github.com/koordinator-sh/koordinator/pkg/slo-controller/metrics"
utilclient "github.com/koordinator-sh/koordinator/pkg/util/client"
utilfeature "github.com/koordinator-sh/koordinator/pkg/util/feature"
"github.com/koordinator-sh/koordinator/pkg/util/fieldindex"
metricsutil "github.com/koordinator-sh/koordinator/pkg/util/metrics"
_ "github.com/koordinator-sh/koordinator/pkg/util/metrics/leadership"
kmmetrics "github.com/koordinator-sh/koordinator/pkg/util/metrics/koordmanager"
"github.com/koordinator-sh/koordinator/pkg/util/sloconfig"
"github.com/koordinator-sh/koordinator/pkg/webhook"
// +kubebuilder:scaffold:imports
Expand Down Expand Up @@ -212,10 +211,10 @@ func installMetricsHandler(mgr *ctrl.Options) {
mgr.Metrics.ExtraHandlers = map[string]http.Handler{}
}
for path, handler := range map[string]http.Handler{
metrics.InternalHTTPPath: promhttp.HandlerFor(metrics.InternalRegistry, promhttp.HandlerOpts{}),
metrics.ExternalHTTPPath: promhttp.HandlerFor(metrics.ExternalRegistry, promhttp.HandlerOpts{}),
metrics.DefaultHTTPPath: promhttp.HandlerFor(
metricsutil.MergedGatherFunc(metrics.InternalRegistry, metrics.ExternalRegistry, ctrlmetrics.Registry), promhttp.HandlerOpts{}),
kmmetrics.InternalHTTPPath: promhttp.HandlerFor(kmmetrics.InternalRegistry, promhttp.HandlerOpts{}),
kmmetrics.ExternalHTTPPath: promhttp.HandlerFor(kmmetrics.ExternalRegistry, promhttp.HandlerOpts{}),
kmmetrics.DefaultHTTPPath: promhttp.HandlerFor(
metricsutil.MergedGatherFunc(kmmetrics.InternalRegistry, kmmetrics.ExternalRegistry, ctrlmetrics.Registry), promhttp.HandlerOpts{}),
} {
mgr.Metrics.ExtraHandlers[path] = handler
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/slo-controller/metrics/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ limitations under the License.

package metrics

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

"github.com/koordinator-sh/koordinator/pkg/util/metrics/koordmanager"
)

func init() {
InternalMustRegister(CommonCollectors...)
koordmanager.InternalMustRegister(CommonCollectors...)
}

var (
Expand Down
24 changes: 0 additions & 24 deletions pkg/slo-controller/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package metrics
import (
"github.com/prometheus/client_golang/prometheus"
corev1 "k8s.io/api/core/v1"
"k8s.io/component-base/metrics/legacyregistry"
_ "k8s.io/component-base/metrics/prometheus/clientgo" // load restclient and workqueue metrics
)

Expand All @@ -42,29 +41,6 @@ const (
UnitInteger = "integer"
)

const (
// DefaultHTTPPath use /all-metrics since /metrics is occupied by controller manager default registry
DefaultHTTPPath = "/all-metrics"
ExternalHTTPPath = "/external-metrics"
InternalHTTPPath = "/internal-metrics"
)

var (
// ExternalRegistry register metrics for users
ExternalRegistry = prometheus.NewRegistry()

// InternalRegistry only register metrics of koord-manager itself for performance and functional monitor
InternalRegistry = legacyregistry.DefaultGatherer
)

func ExternalMustRegister(cs ...prometheus.Collector) {
ExternalRegistry.MustRegister(cs...)
}

func InternalMustRegister(cs ...prometheus.Collector) {
legacyregistry.RawMustRegister(cs...)
}

func genNodeLabels(node *corev1.Node) prometheus.Labels {
ls := prometheus.Labels{}
if node != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/slo-controller/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/koordinator-sh/koordinator/apis/extension"
"github.com/koordinator-sh/koordinator/pkg/util/metrics/koordmanager"
)

func TestMustRegister(t *testing.T) {
Expand All @@ -35,7 +36,7 @@ func TestMustRegister(t *testing.T) {
Help: "test counter",
}, []string{StatusKey, ReasonKey})
assert.NotPanics(t, func() {
InternalMustRegister(testMetricVec)
koordmanager.InternalMustRegister(testMetricVec)
})

testExternalMetricVec := prometheus.NewCounterVec(prometheus.CounterOpts{
Expand All @@ -44,7 +45,7 @@ func TestMustRegister(t *testing.T) {
Help: "test counter",
}, []string{StatusKey, ReasonKey})
assert.NotPanics(t, func() {
ExternalMustRegister(testExternalMetricVec)
koordmanager.ExternalMustRegister(testExternalMetricVec)
})
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/slo-controller/metrics/node_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
corev1 "k8s.io/api/core/v1"

"github.com/koordinator-sh/koordinator/pkg/util/metrics"
"github.com/koordinator-sh/koordinator/pkg/util/metrics/koordmanager"
)

func init() {
InternalMustRegister(NodeResourceCollectors...)
koordmanager.InternalMustRegister(NodeResourceCollectors...)
}

var (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package leadership
package koordmanager

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"k8s.io/client-go/tools/leaderelection"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

var leaderMetric = promauto.NewGauge(
Expand All @@ -31,7 +30,7 @@ var leaderMetric = promauto.NewGauge(
)

func init() {
metrics.Registry.MustRegister(leaderMetric)
InternalMustRegister(leaderMetric)
leaderelection.SetProvider(newMetricProvider())
}

Expand Down
45 changes: 45 additions & 0 deletions pkg/util/metrics/koordmanager/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2022 The Koordinator Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package koordmanager

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

const (
// DefaultHTTPPath use /all-metrics since /metrics is occupied by controller manager default registry
DefaultHTTPPath = "/all-metrics"
ExternalHTTPPath = "/external-metrics"
InternalHTTPPath = "/internal-metrics"
)

var (
// ExternalRegistry register metrics for users
ExternalRegistry = prometheus.NewRegistry()

// InternalRegistry only register metrics of koord-manager itself for performance and functional monitor
InternalRegistry = legacyregistry.DefaultGatherer
)

func ExternalMustRegister(cs ...prometheus.Collector) {
ExternalRegistry.MustRegister(cs...)
}

func InternalMustRegister(cs ...prometheus.Collector) {
legacyregistry.RawMustRegister(cs...)
}

0 comments on commit ae5ae59

Please sign in to comment.