Skip to content

Commit

Permalink
Refactor nginx status and udp collectors
Browse files Browse the repository at this point in the history
  • Loading branch information
fmejia97 committed Jun 13, 2018
1 parent ad69d87 commit 9fb664b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
8 changes: 2 additions & 6 deletions cmd/nginx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,18 @@ func main() {
mux := http.NewServeMux()
go registerHandlers(conf.EnableProfiling, conf.ListenPorts.Health, ngx, mux)

nginxStatusCollector := collector.NewNGINXStatusCollector(conf.Namespace, class.IngressClass, conf.ListenPorts.Status)
err = collector.InitNGINXStatusCollector(conf.Namespace, class.IngressClass, conf.ListenPorts.Status)

if err != nil {
glog.Fatalf("Error generating metric collector: %v", err)
}

go nginxStatusCollector.Run()

udpCollector, err := collector.NewUDPCollector(conf.Namespace, class.IngressClass, 8000)
err = collector.InitUDPCollector(conf.Namespace, class.IngressClass, 8000)

if err != nil {
glog.Fatalf("Error generating UDP collector: %v", err)
}

go udpCollector.Run()

ngx.Start()
}

Expand Down
16 changes: 11 additions & 5 deletions internal/ingress/metric/collector/nginx_status_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ type (
}
)

// NewNGINXStatusCollector returns a new prometheus collector the default nginx status module
func NewNGINXStatusCollector(watchNamespace, ingressClass string, ngxHealthPort int) Stopable {
// InitNGINXStatusCollector returns a new prometheus collector the default nginx status module
func InitNGINXStatusCollector(watchNamespace, ingressClass string, ngxHealthPort int) error {
const ns string = "nginx"
const ngxStatusPath = "/nginx_status"
p := nginxStatusCollector{
Expand All @@ -94,14 +94,20 @@ func NewNGINXStatusCollector(watchNamespace, ingressClass string, ngxHealthPort
[]string{"ingress_class", "namespace"}, nil),

connections: prometheus.NewDesc(
prometheus.BuildFQName(ns, "", "connnections"),
prometheus.BuildFQName(ns, "", "connections"),
"current number of client connections with state {reading, writing, waiting}",
[]string{"ingress_class", "namespace", "state"}, nil),
}

prometheus.Register(p)
err := prometheus.Register(p)

return p
if err != nil {
return fmt.Errorf("error while registering nginx status collector : %v", err)
}

go p.Run()

return nil
}

// Describe implements prometheus.Collector.
Expand Down
1 change: 0 additions & 1 deletion internal/ingress/metric/collector/process_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type scrapeRequest struct {
type Stopable interface {
prometheus.Collector
Stop()
Run()
}

// BinaryNameMatcher ...
Expand Down
10 changes: 6 additions & 4 deletions internal/ingress/metric/collector/udp_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ type UDPCollector struct {
port int
}

// NewUDPCollector creates a new UDPCollector instance
func NewUDPCollector(ns string, class string, port int) (*UDPCollector, error) {
// InitUDPCollector creates a new UDPCollector instance
func InitUDPCollector(ns string, class string, port int) error {
sc := UDPCollector{}

listener, err := newUDPListener(port)

if err != nil {
return nil, err
return err
}

sc.listener = listener
Expand Down Expand Up @@ -158,7 +158,9 @@ func NewUDPCollector(ns string, class string, port int) (*UDPCollector, error) {
prometheus.MustRegister(sc.collectorSuccess)
prometheus.MustRegister(sc.collectorSuccessTime)

return &sc, nil
go sc.Run()

return nil
}

func (sc *UDPCollector) handleMessage(msg []byte) {
Expand Down

0 comments on commit 9fb664b

Please sign in to comment.