From ae5ae59f14e32d49cb23602c1826d3e5f9704747 Mon Sep 17 00:00:00 2001 From: zwzhang Date: Mon, 11 Nov 2024 18:10:47 +0800 Subject: [PATCH] koord-manager: mv slo-controller metrics to util (#2257) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 佑祎 --- cmd/koord-manager/main.go | 11 +++-- pkg/slo-controller/metrics/common.go | 8 +++- pkg/slo-controller/metrics/metrics.go | 24 ---------- pkg/slo-controller/metrics/metrics_test.go | 5 ++- pkg/slo-controller/metrics/node_resource.go | 3 +- .../{leadership => koordmanager}/leader.go | 5 +-- pkg/util/metrics/koordmanager/registry.go | 45 +++++++++++++++++++ 7 files changed, 63 insertions(+), 38 deletions(-) rename pkg/util/metrics/{leadership => koordmanager}/leader.go (92%) create mode 100644 pkg/util/metrics/koordmanager/registry.go diff --git a/cmd/koord-manager/main.go b/cmd/koord-manager/main.go index 4383ba4fc..176438c76 100644 --- a/cmd/koord-manager/main.go +++ b/cmd/koord-manager/main.go @@ -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 @@ -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 } diff --git a/pkg/slo-controller/metrics/common.go b/pkg/slo-controller/metrics/common.go index d21b5091b..12a903805 100644 --- a/pkg/slo-controller/metrics/common.go +++ b/pkg/slo-controller/metrics/common.go @@ -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 ( diff --git a/pkg/slo-controller/metrics/metrics.go b/pkg/slo-controller/metrics/metrics.go index b9359f207..5ac599d42 100644 --- a/pkg/slo-controller/metrics/metrics.go +++ b/pkg/slo-controller/metrics/metrics.go @@ -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 ) @@ -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 { diff --git a/pkg/slo-controller/metrics/metrics_test.go b/pkg/slo-controller/metrics/metrics_test.go index f320dc8e2..af9edc1bc 100644 --- a/pkg/slo-controller/metrics/metrics_test.go +++ b/pkg/slo-controller/metrics/metrics_test.go @@ -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) { @@ -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{ @@ -44,7 +45,7 @@ func TestMustRegister(t *testing.T) { Help: "test counter", }, []string{StatusKey, ReasonKey}) assert.NotPanics(t, func() { - ExternalMustRegister(testExternalMetricVec) + koordmanager.ExternalMustRegister(testExternalMetricVec) }) } diff --git a/pkg/slo-controller/metrics/node_resource.go b/pkg/slo-controller/metrics/node_resource.go index e49e3da3f..31d4b737a 100644 --- a/pkg/slo-controller/metrics/node_resource.go +++ b/pkg/slo-controller/metrics/node_resource.go @@ -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 ( diff --git a/pkg/util/metrics/leadership/leader.go b/pkg/util/metrics/koordmanager/leader.go similarity index 92% rename from pkg/util/metrics/leadership/leader.go rename to pkg/util/metrics/koordmanager/leader.go index 23ab3707f..7f3df236f 100644 --- a/pkg/util/metrics/leadership/leader.go +++ b/pkg/util/metrics/koordmanager/leader.go @@ -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( @@ -31,7 +30,7 @@ var leaderMetric = promauto.NewGauge( ) func init() { - metrics.Registry.MustRegister(leaderMetric) + InternalMustRegister(leaderMetric) leaderelection.SetProvider(newMetricProvider()) } diff --git a/pkg/util/metrics/koordmanager/registry.go b/pkg/util/metrics/koordmanager/registry.go new file mode 100644 index 000000000..6e64ed371 --- /dev/null +++ b/pkg/util/metrics/koordmanager/registry.go @@ -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...) +}