|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "net/http" |
| 6 | + "strconv" |
| 7 | + "sync" |
| 8 | + |
| 9 | + "github.com/cloudnativelabs/kube-router/app/options" |
| 10 | + "github.com/golang/glog" |
| 11 | + "github.com/prometheus/client_golang/prometheus" |
| 12 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 13 | + "golang.org/x/net/context" |
| 14 | + "k8s.io/client-go/kubernetes" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + serviceTotalConn = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 19 | + Namespace: namespace, |
| 20 | + Name: "service_total_connections", |
| 21 | + Help: "Total incoming conntections made", |
| 22 | + }, []string{"namespace", "service_name", "service_vip", "protocol", "port"}) |
| 23 | + servicePacketsIn = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 24 | + Namespace: namespace, |
| 25 | + Name: "service_packets_in", |
| 26 | + Help: "Total incoming packets", |
| 27 | + }, []string{"namespace", "service_name", "service_vip", "protocol", "port"}) |
| 28 | + servicePacketsOut = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 29 | + Namespace: namespace, |
| 30 | + Name: "service_packets_out", |
| 31 | + Help: "Total outoging packets", |
| 32 | + }, []string{"namespace", "service_name", "service_vip", "protocol", "port"}) |
| 33 | + serviceBytesIn = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 34 | + Namespace: namespace, |
| 35 | + Name: "service_bytes_in", |
| 36 | + Help: "Total incoming bytes", |
| 37 | + }, []string{"namespace", "service_name", "service_vip", "protocol", "port"}) |
| 38 | + serviceBytesOut = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 39 | + Namespace: namespace, |
| 40 | + Name: "service_bytes_out", |
| 41 | + Help: "Total outgoing bytes", |
| 42 | + }, []string{"namespace", "service_name", "service_vip", "protocol", "port"}) |
| 43 | + servicePpsIn = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 44 | + Namespace: namespace, |
| 45 | + Name: "service_pps_in", |
| 46 | + Help: "Incoming packets per second", |
| 47 | + }, []string{"namespace", "service_name", "service_vip", "protocol", "port"}) |
| 48 | + servicePpsOut = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 49 | + Namespace: namespace, |
| 50 | + Name: "service_pps_out", |
| 51 | + Help: "Outoging packets per second", |
| 52 | + }, []string{"namespace", "service_name", "service_vip", "protocol", "port"}) |
| 53 | + serviceCPS = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 54 | + Namespace: namespace, |
| 55 | + Name: "service_cps", |
| 56 | + Help: "Service connections per second", |
| 57 | + }, []string{"namespace", "service_name", "service_vip", "protocol", "port"}) |
| 58 | + serviceBpsIn = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 59 | + Namespace: namespace, |
| 60 | + Name: "service_bps_in", |
| 61 | + Help: "Incoming bytes per second", |
| 62 | + }, []string{"namespace", "service_name", "service_vip", "protocol", "port"}) |
| 63 | + serviceBpsOut = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 64 | + Namespace: namespace, |
| 65 | + Name: "service_bps_out", |
| 66 | + Help: "Outoging bytes per second", |
| 67 | + }, []string{"namespace", "service_name", "service_vip", "protocol", "port"}) |
| 68 | + controllerIpvsServices = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 69 | + Namespace: namespace, |
| 70 | + Name: "controller_ipvs_services", |
| 71 | + Help: "Number of ipvs services in the instance", |
| 72 | + }, []string{}) |
| 73 | + controllerIptablesSyncTime = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 74 | + Namespace: namespace, |
| 75 | + Name: "controller_iptables_sync_time", |
| 76 | + Help: "Time it took for controller to sync iptables", |
| 77 | + }, []string{}) |
| 78 | + controllerPublishMetricsTime = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 79 | + Namespace: namespace, |
| 80 | + Name: "controller_publish_metrics_time", |
| 81 | + Help: "Time it took to publish metrics", |
| 82 | + }, []string{}) |
| 83 | + controllerIpvsServicesSyncTime = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 84 | + Namespace: namespace, |
| 85 | + Name: "controller_ipvs_services_sync_time", |
| 86 | + Help: "Time it took for controller to sync ipvs services", |
| 87 | + }, []string{}) |
| 88 | + controllerBPGpeers = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 89 | + Namespace: namespace, |
| 90 | + Name: "controller_bgp_peers", |
| 91 | + Help: "BGP peers in the runtime configuration", |
| 92 | + }, []string{}) |
| 93 | + controllerBGPInternalPeersSyncTime = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 94 | + Namespace: namespace, |
| 95 | + Name: "controller_bgp_internal_peers_sync_time", |
| 96 | + Help: "Time it took to sync internal bgp peers", |
| 97 | + }, []string{}) |
| 98 | + controllerBGPadvertisementsReceived = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 99 | + Namespace: namespace, |
| 100 | + Name: "controller_bgp_advertisements_received", |
| 101 | + Help: "Time it took to sync internal bgp peers", |
| 102 | + }, []string{}) |
| 103 | + controllerIpvsMetricsExportTime = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 104 | + Namespace: namespace, |
| 105 | + Name: "controller_ipvs_metrics_export_time", |
| 106 | + Help: "Time it took to export metrics", |
| 107 | + }, []string{}) |
| 108 | +) |
| 109 | + |
| 110 | +// MetricsController Holds settings for the metrics controller |
| 111 | +type MetricsController struct { |
| 112 | + endpointsMap endpointsInfoMap |
| 113 | + MetricsPath string |
| 114 | + MetricsPort uint16 |
| 115 | + mu sync.Mutex |
| 116 | + nodeIP net.IP |
| 117 | + serviceMap serviceInfoMap |
| 118 | +} |
| 119 | + |
| 120 | +// Run prometheus metrics controller |
| 121 | +func (mc *MetricsController) Run(stopCh <-chan struct{}, wg *sync.WaitGroup) error { |
| 122 | + defer wg.Done() |
| 123 | + glog.Info("Starting metrics controller") |
| 124 | + |
| 125 | + // register metrics for this controller |
| 126 | + prometheus.MustRegister(controllerIpvsMetricsExportTime) |
| 127 | + |
| 128 | + srv := &http.Server{Addr: ":" + strconv.Itoa(int(mc.MetricsPort)), Handler: http.DefaultServeMux} |
| 129 | + |
| 130 | + // add prometheus handler on metrics path |
| 131 | + http.Handle(mc.MetricsPath, promhttp.Handler()) |
| 132 | + |
| 133 | + go func() { |
| 134 | + if err := srv.ListenAndServe(); err != nil { |
| 135 | + // cannot panic, because this probably is an intentional close |
| 136 | + glog.Errorf("Metrics controller error: %s", err) |
| 137 | + } |
| 138 | + }() |
| 139 | + |
| 140 | + <-stopCh |
| 141 | + glog.Infof("Shutting down metrics controller") |
| 142 | + if err := srv.Shutdown(context.Background()); err != nil { |
| 143 | + glog.Errorf("could not shutdown: %v", err) |
| 144 | + } |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +// NewMetricsController returns new MetricController object |
| 149 | +func NewMetricsController(clientset *kubernetes.Clientset, config *options.KubeRouterConfig) (*MetricsController, error) { |
| 150 | + mc := MetricsController{} |
| 151 | + mc.MetricsPath = config.MetricsPath |
| 152 | + mc.MetricsPort = config.MetricsPort |
| 153 | + return &mc, nil |
| 154 | +} |
0 commit comments