Skip to content

Commit

Permalink
Merge pull request #1398 from marquiz/devel/metrics
Browse files Browse the repository at this point in the history
Refactor metrics
  • Loading branch information
k8s-ci-robot authored Oct 9, 2023
2 parents 83c7096 + 5171ae0 commit 899939b
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 101 deletions.
37 changes: 0 additions & 37 deletions pkg/nfd-master/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ limitations under the License.
package nfdmaster

import (
"fmt"
"net/http"

"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"
)

Expand All @@ -40,8 +35,6 @@ const (
)

var (
srv *http.Server

buildInfo = prometheus.NewGauge(prometheus.GaugeOpts{
Name: buildInfoQuery,
Help: "Version from which Node Feature Discovery was built.",
Expand Down Expand Up @@ -94,33 +87,3 @@ var (
func registerVersion(version string) {
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()
}
}
14 changes: 12 additions & 2 deletions pkg/nfd-master/nfd-master.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,19 @@ func (m *nfdMaster) Run() error {

// Register to metrics server
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())
defer stopMetricsServer()
defer m.Stop()
}

// Run gRPC server
Expand Down
28 changes: 0 additions & 28 deletions pkg/nfd-topology-updater/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ limitations under the License.
package nfdtopologyupdater

import (
"fmt"
"net/http"

"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"
)

Expand All @@ -33,8 +28,6 @@ const (
)

var (
srv *http.Server

buildInfo = prometheus.NewGauge(prometheus.GaugeOpts{
Name: buildInfoQuery,
Help: "Version from which Node Feature Discovery was built.",
Expand All @@ -52,24 +45,3 @@ var (
func registerVersion(version string) {
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()
}
}
7 changes: 5 additions & 2 deletions pkg/nfd-topology-updater/nfd-topology-updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,12 @@ func (w *nfdTopologyUpdater) Run() error {

// Register to metrics server
if w.args.MetricsPort > 0 {
go runMetricsServer(w.args.MetricsPort)
m := utils.CreateMetricsServer(w.args.MetricsPort,
buildInfo,
scanErrors)
go m.Run()
registerVersion(version.Get())
defer stopMetricsServer()
defer m.Stop()
}

var resScan resourcemonitor.ResourcesScanner
Expand Down
30 changes: 0 additions & 30 deletions pkg/nfd-worker/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,17 @@ limitations under the License.
package nfdworker

import (
"fmt"
"net/http"

"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"
)

// 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 (
buildInfoQuery = "nfd_worker_build_info"
featureDiscoveryDurationQuery = "nfd_feature_discovery_duration_seconds"
)

var (
srv *http.Server

featureDiscoveryDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: featureDiscoveryDurationQuery,
Expand All @@ -57,25 +49,3 @@ var (
func registerVersion(version string) {
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()
}
}
7 changes: 5 additions & 2 deletions pkg/nfd-worker/nfd-worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,12 @@ func (w *nfdWorker) Run() error {

// Register to metrics server
if w.args.MetricsPort > 0 {
go runMetricsServer(w.args.MetricsPort)
m := utils.CreateMetricsServer(w.args.MetricsPort,
buildInfo,
featureDiscoveryDuration)
go m.Run()
registerVersion(version.Get())
defer stopMetricsServer()
defer m.Stop()
}

err = w.runFeatureDiscovery()
Expand Down
54 changes: 54 additions & 0 deletions pkg/utils/metrics.go
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 899939b

Please sign in to comment.