mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2025-03-31 04:04:51 +00:00
Refactor metrics
Move common boilerplate code under pkg/utils.
This commit is contained in:
parent
83c7096bbe
commit
5171ae0f90
7 changed files with 76 additions and 101 deletions
|
@ -17,12 +17,7 @@ limitations under the License.
|
||||||
package nfdmaster
|
package nfdmaster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
||||||
"k8s.io/klog/v2"
|
|
||||||
"sigs.k8s.io/node-feature-discovery/pkg/version"
|
"sigs.k8s.io/node-feature-discovery/pkg/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -40,8 +35,6 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
srv *http.Server
|
|
||||||
|
|
||||||
buildInfo = prometheus.NewGauge(prometheus.GaugeOpts{
|
buildInfo = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||||
Name: buildInfoQuery,
|
Name: buildInfoQuery,
|
||||||
Help: "Version from which Node Feature Discovery was built.",
|
Help: "Version from which Node Feature Discovery was built.",
|
||||||
|
@ -94,33 +87,3 @@ var (
|
||||||
func registerVersion(version string) {
|
func registerVersion(version string) {
|
||||||
buildInfo.SetToCurrentTime()
|
buildInfo.SetToCurrentTime()
|
||||||
}
|
}
|
||||||
|
|
||||||
// runMetricsServer starts a http server to expose metrics
|
|
||||||
func runMetricsServer(port int) {
|
|
||||||
r := prometheus.NewRegistry()
|
|
||||||
r.MustRegister(
|
|
||||||
buildInfo,
|
|
||||||
nodeUpdateRequests,
|
|
||||||
nodeUpdates,
|
|
||||||
nodeUpdateFailures,
|
|
||||||
nodeLabelsRejected,
|
|
||||||
nodeERsRejected,
|
|
||||||
nodeTaintsRejected,
|
|
||||||
nfrProcessingTime,
|
|
||||||
nfrProcessingErrors)
|
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
|
||||||
mux.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
|
|
||||||
|
|
||||||
klog.InfoS("metrics server starting", "port", port)
|
|
||||||
srv = &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: mux}
|
|
||||||
klog.InfoS("metrics server stopped", "exitCode", srv.ListenAndServe())
|
|
||||||
}
|
|
||||||
|
|
||||||
// stopMetricsServer stops the metrics server
|
|
||||||
func stopMetricsServer() {
|
|
||||||
if srv != nil {
|
|
||||||
klog.InfoS("stopping metrics server", "port", srv.Addr)
|
|
||||||
srv.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -250,9 +250,19 @@ func (m *nfdMaster) Run() error {
|
||||||
|
|
||||||
// Register to metrics server
|
// Register to metrics server
|
||||||
if m.args.MetricsPort > 0 {
|
if m.args.MetricsPort > 0 {
|
||||||
go runMetricsServer(m.args.MetricsPort)
|
m := utils.CreateMetricsServer(m.args.MetricsPort,
|
||||||
|
buildInfo,
|
||||||
|
nodeUpdateRequests,
|
||||||
|
nodeUpdates,
|
||||||
|
nodeUpdateFailures,
|
||||||
|
nodeLabelsRejected,
|
||||||
|
nodeERsRejected,
|
||||||
|
nodeTaintsRejected,
|
||||||
|
nfrProcessingTime,
|
||||||
|
nfrProcessingErrors)
|
||||||
|
go m.Run()
|
||||||
registerVersion(version.Get())
|
registerVersion(version.Get())
|
||||||
defer stopMetricsServer()
|
defer m.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run gRPC server
|
// Run gRPC server
|
||||||
|
|
|
@ -17,12 +17,7 @@ limitations under the License.
|
||||||
package nfdtopologyupdater
|
package nfdtopologyupdater
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
||||||
"k8s.io/klog/v2"
|
|
||||||
"sigs.k8s.io/node-feature-discovery/pkg/version"
|
"sigs.k8s.io/node-feature-discovery/pkg/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -33,8 +28,6 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
srv *http.Server
|
|
||||||
|
|
||||||
buildInfo = prometheus.NewGauge(prometheus.GaugeOpts{
|
buildInfo = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||||
Name: buildInfoQuery,
|
Name: buildInfoQuery,
|
||||||
Help: "Version from which Node Feature Discovery was built.",
|
Help: "Version from which Node Feature Discovery was built.",
|
||||||
|
@ -52,24 +45,3 @@ var (
|
||||||
func registerVersion(version string) {
|
func registerVersion(version string) {
|
||||||
buildInfo.SetToCurrentTime()
|
buildInfo.SetToCurrentTime()
|
||||||
}
|
}
|
||||||
|
|
||||||
// runMetricsServer starts a http server to expose metrics
|
|
||||||
func runMetricsServer(port int) {
|
|
||||||
r := prometheus.NewRegistry()
|
|
||||||
r.MustRegister(buildInfo,
|
|
||||||
scanErrors)
|
|
||||||
mux := http.NewServeMux()
|
|
||||||
mux.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
|
|
||||||
|
|
||||||
klog.InfoS("metrics server starting", "port", port)
|
|
||||||
srv = &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: mux}
|
|
||||||
klog.InfoS("metrics server stopped", "exitCode", srv.ListenAndServe())
|
|
||||||
}
|
|
||||||
|
|
||||||
// stopMetricsServer stops the metrics server
|
|
||||||
func stopMetricsServer() {
|
|
||||||
if srv != nil {
|
|
||||||
klog.InfoS("stopping metrics server", "port", srv.Addr)
|
|
||||||
srv.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -144,9 +144,12 @@ func (w *nfdTopologyUpdater) Run() error {
|
||||||
|
|
||||||
// Register to metrics server
|
// Register to metrics server
|
||||||
if w.args.MetricsPort > 0 {
|
if w.args.MetricsPort > 0 {
|
||||||
go runMetricsServer(w.args.MetricsPort)
|
m := utils.CreateMetricsServer(w.args.MetricsPort,
|
||||||
|
buildInfo,
|
||||||
|
scanErrors)
|
||||||
|
go m.Run()
|
||||||
registerVersion(version.Get())
|
registerVersion(version.Get())
|
||||||
defer stopMetricsServer()
|
defer m.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
var resScan resourcemonitor.ResourcesScanner
|
var resScan resourcemonitor.ResourcesScanner
|
||||||
|
|
|
@ -17,16 +17,10 @@ limitations under the License.
|
||||||
package nfdworker
|
package nfdworker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
||||||
"k8s.io/klog/v2"
|
|
||||||
"sigs.k8s.io/node-feature-discovery/pkg/version"
|
"sigs.k8s.io/node-feature-discovery/pkg/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
// When adding metric names, see https://prometheus.io/docs/practices/naming/#metric-names
|
|
||||||
// When adding metric names, see https://prometheus.io/docs/practices/naming/#metric-names
|
// When adding metric names, see https://prometheus.io/docs/practices/naming/#metric-names
|
||||||
const (
|
const (
|
||||||
buildInfoQuery = "nfd_worker_build_info"
|
buildInfoQuery = "nfd_worker_build_info"
|
||||||
|
@ -34,8 +28,6 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
srv *http.Server
|
|
||||||
|
|
||||||
featureDiscoveryDuration = prometheus.NewHistogramVec(
|
featureDiscoveryDuration = prometheus.NewHistogramVec(
|
||||||
prometheus.HistogramOpts{
|
prometheus.HistogramOpts{
|
||||||
Name: featureDiscoveryDurationQuery,
|
Name: featureDiscoveryDurationQuery,
|
||||||
|
@ -57,25 +49,3 @@ var (
|
||||||
func registerVersion(version string) {
|
func registerVersion(version string) {
|
||||||
buildInfo.SetToCurrentTime()
|
buildInfo.SetToCurrentTime()
|
||||||
}
|
}
|
||||||
|
|
||||||
// runMetricsServer starts a http server to expose metrics
|
|
||||||
func runMetricsServer(port int) {
|
|
||||||
r := prometheus.NewRegistry()
|
|
||||||
r.MustRegister(featureDiscoveryDuration)
|
|
||||||
r.MustRegister(buildInfo)
|
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
|
||||||
mux.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
|
|
||||||
|
|
||||||
klog.InfoS("metrics server starting", "port", port)
|
|
||||||
srv = &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: mux}
|
|
||||||
klog.InfoS("metrics server stopped", "exit code", srv.ListenAndServe())
|
|
||||||
}
|
|
||||||
|
|
||||||
// stopMetricsServer stops the metrics server
|
|
||||||
func stopMetricsServer() {
|
|
||||||
if srv != nil {
|
|
||||||
klog.InfoS("stopping metrics server", "port", srv.Addr)
|
|
||||||
srv.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -243,9 +243,12 @@ func (w *nfdWorker) Run() error {
|
||||||
|
|
||||||
// Register to metrics server
|
// Register to metrics server
|
||||||
if w.args.MetricsPort > 0 {
|
if w.args.MetricsPort > 0 {
|
||||||
go runMetricsServer(w.args.MetricsPort)
|
m := utils.CreateMetricsServer(w.args.MetricsPort,
|
||||||
|
buildInfo,
|
||||||
|
featureDiscoveryDuration)
|
||||||
|
go m.Run()
|
||||||
registerVersion(version.Get())
|
registerVersion(version.Get())
|
||||||
defer stopMetricsServer()
|
defer m.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
err = w.runFeatureDiscovery()
|
err = w.runFeatureDiscovery()
|
||||||
|
|
54
pkg/utils/metrics.go
Normal file
54
pkg/utils/metrics.go
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
/*
|
||||||
|
Copyright 2023 The Kubernetes 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 utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
|
"k8s.io/klog/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MetricsServer struct {
|
||||||
|
srv *http.Server
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunMetricsServer starts a new http server to expose metrics.
|
||||||
|
func CreateMetricsServer(port int, cs ...prometheus.Collector) *MetricsServer {
|
||||||
|
r := prometheus.NewRegistry()
|
||||||
|
r.MustRegister(cs...)
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
|
||||||
|
|
||||||
|
return &MetricsServer{srv: &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: mux}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run runs the metrics server.
|
||||||
|
func (s *MetricsServer) Run() {
|
||||||
|
klog.InfoS("metrics server starting", "port", s.srv.Addr)
|
||||||
|
klog.InfoS("metrics server stopped", "exitCode", s.srv.ListenAndServe())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop stops the metrics server.
|
||||||
|
func (s *MetricsServer) Stop() {
|
||||||
|
if s.srv != nil {
|
||||||
|
klog.InfoS("stopping metrics server", "port", s.srv.Addr)
|
||||||
|
s.srv.Close()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue