Skip to content

Commit

Permalink
Make metrics registerer/gathererer replacable
Browse files Browse the repository at this point in the history
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
  • Loading branch information
brandond authored and aauren committed Oct 7, 2023
1 parent e6f668c commit aa107d6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
5 changes: 2 additions & 3 deletions pkg/controllers/netpol/network_policy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/cloudnativelabs/kube-router/v2/pkg/options"
"github.com/cloudnativelabs/kube-router/v2/pkg/utils"
"github.com/coreos/go-iptables/iptables"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/klog/v2"

v1core "k8s.io/api/core/v1"
Expand Down Expand Up @@ -845,8 +844,8 @@ func NewNetworkPolicyController(clientset kubernetes.Interface,

if config.MetricsEnabled {
// Register the metrics for this controller
prometheus.MustRegister(metrics.ControllerIptablesSyncTime)
prometheus.MustRegister(metrics.ControllerPolicyChainsSyncTime)
metrics.DefaultRegisterer.MustRegister(metrics.ControllerIptablesSyncTime)
metrics.DefaultRegisterer.MustRegister(metrics.ControllerPolicyChainsSyncTime)
npc.MetricsEnabled = true
}

Expand Down
25 changes: 12 additions & 13 deletions pkg/controllers/proxy/network_services_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/coreos/go-iptables/iptables"
"github.com/docker/docker/client"
"github.com/moby/ipvs"
"github.com/prometheus/client_golang/prometheus"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
api "k8s.io/api/core/v1"
Expand Down Expand Up @@ -2285,18 +2284,18 @@ func NewNetworkServicesController(clientset kubernetes.Interface,

if config.MetricsEnabled {
// Register the metrics for this controller
prometheus.MustRegister(metrics.ControllerIpvsServices)
prometheus.MustRegister(metrics.ControllerIpvsServicesSyncTime)
prometheus.MustRegister(metrics.ServiceBpsIn)
prometheus.MustRegister(metrics.ServiceBpsOut)
prometheus.MustRegister(metrics.ServiceBytesIn)
prometheus.MustRegister(metrics.ServiceBytesOut)
prometheus.MustRegister(metrics.ServiceCPS)
prometheus.MustRegister(metrics.ServicePacketsIn)
prometheus.MustRegister(metrics.ServicePacketsOut)
prometheus.MustRegister(metrics.ServicePpsIn)
prometheus.MustRegister(metrics.ServicePpsOut)
prometheus.MustRegister(metrics.ServiceTotalConn)
metrics.DefaultRegisterer.MustRegister(metrics.ControllerIpvsServices)
metrics.DefaultRegisterer.MustRegister(metrics.ControllerIpvsServicesSyncTime)
metrics.DefaultRegisterer.MustRegister(metrics.ServiceBpsIn)
metrics.DefaultRegisterer.MustRegister(metrics.ServiceBpsOut)
metrics.DefaultRegisterer.MustRegister(metrics.ServiceBytesIn)
metrics.DefaultRegisterer.MustRegister(metrics.ServiceBytesOut)
metrics.DefaultRegisterer.MustRegister(metrics.ServiceCPS)
metrics.DefaultRegisterer.MustRegister(metrics.ServicePacketsIn)
metrics.DefaultRegisterer.MustRegister(metrics.ServicePacketsOut)
metrics.DefaultRegisterer.MustRegister(metrics.ServicePpsIn)
metrics.DefaultRegisterer.MustRegister(metrics.ServicePpsOut)
metrics.DefaultRegisterer.MustRegister(metrics.ServiceTotalConn)
nsc.MetricsEnabled = true
}

Expand Down
11 changes: 5 additions & 6 deletions pkg/controllers/routing/network_routes_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/coreos/go-iptables/iptables"
gobgpapi "github.com/osrg/gobgp/v3/api"
gobgp "github.com/osrg/gobgp/v3/pkg/server"
"github.com/prometheus/client_golang/prometheus"
"github.com/vishvananda/netlink"
v1core "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -1407,11 +1406,11 @@ func NewNetworkRoutingController(clientset kubernetes.Interface,
nrc := NetworkRoutingController{ipsetMutex: ipsetMutex}
if kubeRouterConfig.MetricsEnabled {
// Register the metrics for this controller
prometheus.MustRegister(metrics.ControllerBGPadvertisementsReceived)
prometheus.MustRegister(metrics.ControllerBGPadvertisementsSent)
prometheus.MustRegister(metrics.ControllerBGPInternalPeersSyncTime)
prometheus.MustRegister(metrics.ControllerBPGpeers)
prometheus.MustRegister(metrics.ControllerRoutesSyncTime)
metrics.DefaultRegisterer.MustRegister(metrics.ControllerBGPadvertisementsReceived)
metrics.DefaultRegisterer.MustRegister(metrics.ControllerBGPadvertisementsSent)
metrics.DefaultRegisterer.MustRegister(metrics.ControllerBGPInternalPeersSyncTime)
metrics.DefaultRegisterer.MustRegister(metrics.ControllerBPGpeers)
metrics.DefaultRegisterer.MustRegister(metrics.ControllerRoutesSyncTime)
nrc.MetricsEnabled = true
}

Expand Down
21 changes: 18 additions & 3 deletions pkg/metrics/metrics_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ const (
namespace = "kube_router"
)

var (
// DefaultRegisterer and DefaultGatherer are the implementations of the
// prometheus Registerer and Gatherer interfaces that all metrics operations
// will use. They are variables so that packages that embed this library can
// replace them at runtime, instead of having to pass around specific
// registries.
DefaultRegisterer = prometheus.DefaultRegisterer
DefaultGatherer = prometheus.DefaultGatherer
)

var (
// BuildInfo Expose version and other build information
BuildInfo = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Expand Down Expand Up @@ -159,6 +169,11 @@ type Controller struct {
MetricsPort uint16
}

// Handler returns a http.Handler for the default registerer and gatherer
func Handler() http.Handler {
return promhttp.InstrumentMetricHandler(DefaultRegisterer, promhttp.HandlerFor(DefaultGatherer, promhttp.HandlerOpts{}))
}

// Run prometheus metrics controller
func (mc *Controller) Run(healthChan chan<- *healthcheck.ControllerHeartbeat, stopCh <-chan struct{},
wg *sync.WaitGroup) {
Expand All @@ -168,16 +183,16 @@ func (mc *Controller) Run(healthChan chan<- *healthcheck.ControllerHeartbeat, st

// register metrics for this controller
BuildInfo.WithLabelValues(runtime.Version(), version.Version).Set(1)
prometheus.MustRegister(BuildInfo)
prometheus.MustRegister(ControllerIpvsMetricsExportTime)
DefaultRegisterer.MustRegister(BuildInfo)
DefaultRegisterer.MustRegister(ControllerIpvsMetricsExportTime)

srv := &http.Server{
Addr: ":" + strconv.Itoa(int(mc.MetricsPort)),
Handler: http.DefaultServeMux,
ReadHeaderTimeout: 5 * time.Second}

// add prometheus handler on metrics path
http.Handle(mc.MetricsPath, promhttp.Handler())
http.Handle(mc.MetricsPath, Handler())

go func() {
if err := srv.ListenAndServe(); err != nil {
Expand Down

0 comments on commit aa107d6

Please sign in to comment.