Fix nginx stats

This commit is contained in:
Manuel de Brito Fontes 2017-08-31 14:19:32 -03:00
parent abc53ccfc5
commit 57a18d11bd
5 changed files with 22 additions and 32 deletions

View file

@ -24,7 +24,7 @@ import (
) )
const ( const (
ngxStatusPath = "/internal_nginx_status" ngxStatusPath = "/nginx_status"
ngxVtsPath = "/nginx_status/format/json" ngxVtsPath = "/nginx_status/format/json"
) )
@ -46,7 +46,7 @@ type statsCollector struct {
namespace string namespace string
watchClass string watchClass string
healthPort int port int
} }
func (s *statsCollector) stop(sm statusModule) { func (s *statsCollector) stop(sm statusModule) {
@ -63,18 +63,19 @@ func (s *statsCollector) stop(sm statusModule) {
func (s *statsCollector) start(sm statusModule) { func (s *statsCollector) start(sm statusModule) {
switch sm { switch sm {
case defaultStatusModule: case defaultStatusModule:
s.basic = collector.NewNginxStatus(s.namespace, s.watchClass, s.healthPort, ngxStatusPath) s.basic = collector.NewNginxStatus(s.namespace, s.watchClass, s.port, ngxStatusPath)
prometheus.Register(s.basic) prometheus.Register(s.basic)
break break
case vtsStatusModule: case vtsStatusModule:
s.vts = collector.NewNGINXVTSCollector(s.namespace, s.watchClass, s.healthPort, ngxVtsPath) s.vts = collector.NewNGINXVTSCollector(s.namespace, s.watchClass, s.port, ngxVtsPath)
prometheus.Register(s.vts) prometheus.Register(s.vts)
break break
} }
} }
func newStatsCollector(ns, class, binary string, hz int) *statsCollector { func newStatsCollector(ns, class, binary string, port int) *statsCollector {
glog.Infof("starting new nginx stats collector for Ingress controller running in namespace %v (class %v)", ns, class) glog.Infof("starting new nginx stats collector for Ingress controller running in namespace %v (class %v)", ns, class)
glog.Infof("collector extracting information from port %v", port)
pc, err := collector.NewNamedProcess(true, collector.BinaryNameMatcher{ pc, err := collector.NewNamedProcess(true, collector.BinaryNameMatcher{
Name: "nginx", Name: "nginx",
Binary: binary, Binary: binary,
@ -91,6 +92,6 @@ func newStatsCollector(ns, class, binary string, hz int) *statsCollector {
namespace: ns, namespace: ns,
watchClass: class, watchClass: class,
process: pc, process: pc,
healthPort: hz, port: port,
} }
} }

View file

@ -372,7 +372,11 @@ func (n *NGINXController) OverrideFlags(flags *pflag.FlagSet) {
} }
flags.Set("ingress-class", ic) flags.Set("ingress-class", ic)
n.stats = newStatsCollector(wc, ic, n.binary, n.ports.Health)
h, _ := flags.GetInt("healthz-port")
n.ports.Health = h
n.stats = newStatsCollector(wc, ic, n.binary, n.ports.Status)
if n.isSSLPassthroughEnabled { if n.isSSLPassthroughEnabled {
if !isPortAvailable(n.ports.SSLProxy) { if !isPortAvailable(n.ports.SSLProxy) {

View file

@ -143,8 +143,8 @@ func (bit BoolToFloat64) UnmarshalJSON(data []byte) error {
return nil return nil
} }
func getNginxStatus(ngxHealthPort int, ngxStatusPath string) (*basicStatus, error) { func getNginxStatus(port int, path string) (*basicStatus, error) {
url := fmt.Sprintf("http://localhost:%v%v", ngxHealthPort, ngxStatusPath) url := fmt.Sprintf("http://localhost:%v%v", port, path)
glog.V(3).Infof("start scrapping url: %v", url) glog.V(3).Infof("start scrapping url: %v", url)
data, err := httpBody(url) data, err := httpBody(url)
@ -174,8 +174,8 @@ func httpBody(url string) ([]byte, error) {
return data, nil return data, nil
} }
func getNginxVtsMetrics(ngxHealthPort int, ngxVtsPath string) (*vts, error) { func getNginxVtsMetrics(port int, path string) (*vts, error) {
url := fmt.Sprintf("http://localhost:%v%v", ngxHealthPort, ngxVtsPath) url := fmt.Sprintf("http://localhost:%v%v", port, path)
glog.V(3).Infof("start scrapping url: %v", url) glog.V(3).Infof("start scrapping url: %v", url)
data, err := httpBody(url) data, err := httpBody(url)

View file

@ -28,8 +28,8 @@ const ns = "nginx"
type ( type (
vtsCollector struct { vtsCollector struct {
scrapeChan chan scrapeRequest scrapeChan chan scrapeRequest
ngxHealthPort int port int
ngxVtsPath string path string
data *vtsData data *vtsData
watchNamespace string watchNamespace string
ingressClass string ingressClass string
@ -57,12 +57,12 @@ type (
) )
// NewNGINXVTSCollector returns a new prometheus collector for the VTS module // NewNGINXVTSCollector returns a new prometheus collector for the VTS module
func NewNGINXVTSCollector(watchNamespace, ingressClass string, ngxHealthPort int, ngxVtsPath string) Stopable { func NewNGINXVTSCollector(watchNamespace, ingressClass string, port int, path string) Stopable {
p := vtsCollector{ p := vtsCollector{
scrapeChan: make(chan scrapeRequest), scrapeChan: make(chan scrapeRequest),
ngxHealthPort: ngxHealthPort, port: port,
ngxVtsPath: ngxVtsPath, path: path,
watchNamespace: watchNamespace, watchNamespace: watchNamespace,
ingressClass: ingressClass, ingressClass: ingressClass,
} }
@ -201,7 +201,7 @@ func (p vtsCollector) Stop() {
// scrapeVts scrape nginx vts metrics // scrapeVts scrape nginx vts metrics
func (p vtsCollector) scrapeVts(ch chan<- prometheus.Metric) { func (p vtsCollector) scrapeVts(ch chan<- prometheus.Metric) {
nginxMetrics, err := getNginxVtsMetrics(p.ngxHealthPort, p.ngxVtsPath) nginxMetrics, err := getNginxVtsMetrics(p.port, p.path)
if err != nil { if err != nil {
glog.Warningf("unexpected error obtaining nginx status info: %v", err) glog.Warningf("unexpected error obtaining nginx status info: %v", err)
return return

View file

@ -407,21 +407,6 @@ http {
{{ end }} {{ end }}
} }
# this location is used to extract nginx metrics
# using prometheus.
# TODO: enable extraction for vts module.
location /internal_nginx_status {
set $proxy_upstream_name "internal";
allow 127.0.0.1;
{{ if not $cfg.DisableIpv6 }}allow ::1;{{ end }}
deny all;
access_log off;
stub_status on;
}
fastcgi_param HTTP_X_Code 404; fastcgi_param HTTP_X_Code 404;
fastcgi_param HTTP_X_Format $http_accept; fastcgi_param HTTP_X_Format $http_accept;
fastcgi_param HTTP_X_Original_URI $request_uri; fastcgi_param HTTP_X_Original_URI $request_uri;