diff --git a/internal/ingress/controller/metric/collector/nginx.go b/internal/ingress/controller/metric/collector/nginx.go index aeb834602..e76da9e8c 100644 --- a/internal/ingress/controller/metric/collector/nginx.go +++ b/internal/ingress/controller/metric/collector/nginx.go @@ -17,107 +17,107 @@ limitations under the License. package collector import ( - "github.com/golang/glog" + "github.com/golang/glog" - "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus" ) type ( - nginxStatusCollector struct { - scrapeChan chan scrapeRequest - ngxHealthPort int - ngxVtsPath string - data *nginxStatusData - watchNamespace string - ingressClass string - } + nginxStatusCollector struct { + scrapeChan chan scrapeRequest + ngxHealthPort int + ngxVtsPath string + data *nginxStatusData + watchNamespace string + ingressClass string + } - nginxStatusData struct { - connections_total *prometheus.Desc - requests_total *prometheus.Desc - connections *prometheus.Desc - } + nginxStatusData struct { + connections_total *prometheus.Desc + requests_total *prometheus.Desc + connections *prometheus.Desc + } ) // NewNginxStatus returns a new prometheus collector the default nginx status module func NewNginxStatus(watchNamespace, ingressClass string, ngxHealthPort int, ngxVtsPath string) Stopable { - p := nginxStatusCollector{ - scrapeChan: make(chan scrapeRequest), - ngxHealthPort: ngxHealthPort, - ngxVtsPath: ngxVtsPath, - watchNamespace: watchNamespace, - ingressClass: ingressClass, - } + p := nginxStatusCollector{ + scrapeChan: make(chan scrapeRequest), + ngxHealthPort: ngxHealthPort, + ngxVtsPath: ngxVtsPath, + watchNamespace: watchNamespace, + ingressClass: ingressClass, + } - p.data = &nginxStatusData{ - connections_total: prometheus.NewDesc( - prometheus.BuildFQName(ns, "", "connections_total"), - "total number of connections with state {active, accepted, handled}", - []string{"ingress_class", "namespace", "state"}, nil), + p.data = &nginxStatusData{ + connections_total: prometheus.NewDesc( + prometheus.BuildFQName(ns, "", "connections_total"), + "total number of connections with state {active, accepted, handled}", + []string{"ingress_class", "namespace", "state"}, nil), - requests_total: prometheus.NewDesc( - prometheus.BuildFQName(ns, "", "requests_total"), - "total number of client requests", - []string{"ingress_class", "namespace"}, nil), + requests_total: prometheus.NewDesc( + prometheus.BuildFQName(ns, "", "requests_total"), + "total number of client requests", + []string{"ingress_class", "namespace"}, nil), - connections: prometheus.NewDesc( - prometheus.BuildFQName(ns, "", "connections"), - "current number of client connections with state {reading, writing, waiting}", - []string{"ingress_class", "namespace", "state"}, nil), - } + connections: prometheus.NewDesc( + prometheus.BuildFQName(ns, "", "connections"), + "current number of client connections with state {reading, writing, waiting}", + []string{"ingress_class", "namespace", "state"}, nil), + } - go p.start() + go p.start() - return p + return p } // Describe implements prometheus.Collector. func (p nginxStatusCollector) Describe(ch chan<- *prometheus.Desc) { - ch <- p.data.connections_total - ch <- p.data.requests_total - ch <- p.data.connections + ch <- p.data.connections_total + ch <- p.data.requests_total + ch <- p.data.connections } // Collect implements prometheus.Collector. func (p nginxStatusCollector) Collect(ch chan<- prometheus.Metric) { - req := scrapeRequest{results: ch, done: make(chan struct{})} - p.scrapeChan <- req - <-req.done + req := scrapeRequest{results: ch, done: make(chan struct{})} + p.scrapeChan <- req + <-req.done } func (p nginxStatusCollector) start() { - for req := range p.scrapeChan { - ch := req.results - p.scrape(ch) - req.done <- struct{}{} - } + for req := range p.scrapeChan { + ch := req.results + p.scrape(ch) + req.done <- struct{}{} + } } func (p nginxStatusCollector) Stop() { - close(p.scrapeChan) + close(p.scrapeChan) } // nginxStatusCollector scrape the nginx status func (p nginxStatusCollector) scrape(ch chan<- prometheus.Metric) { - s, err := getNginxStatus(p.ngxHealthPort, p.ngxVtsPath) - if err != nil { - glog.Warningf("unexpected error obtaining nginx status info: %v", err) - return - } + s, err := getNginxStatus(p.ngxHealthPort, p.ngxVtsPath) + if err != nil { + glog.Warningf("unexpected error obtaining nginx status info: %v", err) + return + } - ch <- prometheus.MustNewConstMetric(p.data.connections_total, - prometheus.CounterValue, float64(s.Active), p.ingressClass, p.watchNamespace, "active") - ch <- prometheus.MustNewConstMetric(p.data.connections_total, - prometheus.CounterValue, float64(s.Accepted), p.ingressClass, p.watchNamespace, "accepted") - ch <- prometheus.MustNewConstMetric(p.data.connections_total, - prometheus.CounterValue, float64(s.Handled), p.ingressClass, p.watchNamespace, "handled") - ch <- prometheus.MustNewConstMetric(p.data.requests_total, - prometheus.CounterValue, float64(s.Requests), p.ingressClass, p.watchNamespace) - ch <- prometheus.MustNewConstMetric(p.data.connections, - prometheus.GaugeValue, float64(s.Reading), p.ingressClass, p.watchNamespace, "reading") - ch <- prometheus.MustNewConstMetric(p.data.connections, - prometheus.GaugeValue, float64(s.Writing), p.ingressClass, p.watchNamespace, "writing") - ch <- prometheus.MustNewConstMetric(p.data.connections, - prometheus.GaugeValue, float64(s.Waiting), p.ingressClass, p.watchNamespace, "waiting") + ch <- prometheus.MustNewConstMetric(p.data.connections_total, + prometheus.CounterValue, float64(s.Active), p.ingressClass, p.watchNamespace, "active") + ch <- prometheus.MustNewConstMetric(p.data.connections_total, + prometheus.CounterValue, float64(s.Accepted), p.ingressClass, p.watchNamespace, "accepted") + ch <- prometheus.MustNewConstMetric(p.data.connections_total, + prometheus.CounterValue, float64(s.Handled), p.ingressClass, p.watchNamespace, "handled") + ch <- prometheus.MustNewConstMetric(p.data.requests_total, + prometheus.CounterValue, float64(s.Requests), p.ingressClass, p.watchNamespace) + ch <- prometheus.MustNewConstMetric(p.data.connections, + prometheus.GaugeValue, float64(s.Reading), p.ingressClass, p.watchNamespace, "reading") + ch <- prometheus.MustNewConstMetric(p.data.connections, + prometheus.GaugeValue, float64(s.Writing), p.ingressClass, p.watchNamespace, "writing") + ch <- prometheus.MustNewConstMetric(p.data.connections, + prometheus.GaugeValue, float64(s.Waiting), p.ingressClass, p.watchNamespace, "waiting") }