Skip to content

Commit

Permalink
Merge pull request #1282 from aledbf/fix-stats
Browse files Browse the repository at this point in the history
Fix nginx stats
  • Loading branch information
nicksardo authored Aug 31, 2017
2 parents abc53cc + a5432bf commit cda42f9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 33 deletions.
13 changes: 7 additions & 6 deletions controllers/nginx/pkg/cmd/controller/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

const (
ngxStatusPath = "/internal_nginx_status"
ngxStatusPath = "/nginx_status"
ngxVtsPath = "/nginx_status/format/json"
)

Expand All @@ -46,7 +46,7 @@ type statsCollector struct {
namespace string
watchClass string

healthPort int
port int
}

func (s *statsCollector) stop(sm statusModule) {
Expand All @@ -63,18 +63,19 @@ func (s *statsCollector) stop(sm statusModule) {
func (s *statsCollector) start(sm statusModule) {
switch sm {
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)
break
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)
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("collector extracting information from port %v", port)
pc, err := collector.NewNamedProcess(true, collector.BinaryNameMatcher{
Name: "nginx",
Binary: binary,
Expand All @@ -91,6 +92,6 @@ func newStatsCollector(ns, class, binary string, hz int) *statsCollector {
namespace: ns,
watchClass: class,
process: pc,
healthPort: hz,
port: port,
}
}
6 changes: 5 additions & 1 deletion controllers/nginx/pkg/cmd/controller/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,11 @@ func (n *NGINXController) OverrideFlags(flags *pflag.FlagSet) {
}

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 !isPortAvailable(n.ports.SSLProxy) {
Expand Down
8 changes: 4 additions & 4 deletions controllers/nginx/pkg/metric/collector/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ func (bit BoolToFloat64) UnmarshalJSON(data []byte) error {
return nil
}

func getNginxStatus(ngxHealthPort int, ngxStatusPath string) (*basicStatus, error) {
url := fmt.Sprintf("http://localhost:%v%v", ngxHealthPort, ngxStatusPath)
func getNginxStatus(port int, path string) (*basicStatus, error) {
url := fmt.Sprintf("http://localhost:%v%v", port, path)
glog.V(3).Infof("start scrapping url: %v", url)

data, err := httpBody(url)
Expand Down Expand Up @@ -174,8 +174,8 @@ func httpBody(url string) ([]byte, error) {
return data, nil
}

func getNginxVtsMetrics(ngxHealthPort int, ngxVtsPath string) (*vts, error) {
url := fmt.Sprintf("http://localhost:%v%v", ngxHealthPort, ngxVtsPath)
func getNginxVtsMetrics(port int, path string) (*vts, error) {
url := fmt.Sprintf("http://localhost:%v%v", port, path)
glog.V(3).Infof("start scrapping url: %v", url)

data, err := httpBody(url)
Expand Down
12 changes: 6 additions & 6 deletions controllers/nginx/pkg/metric/collector/vts.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const ns = "nginx"
type (
vtsCollector struct {
scrapeChan chan scrapeRequest
ngxHealthPort int
ngxVtsPath string
port int
path string
data *vtsData
watchNamespace string
ingressClass string
Expand Down Expand Up @@ -57,12 +57,12 @@ type (
)

// 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{
scrapeChan: make(chan scrapeRequest),
ngxHealthPort: ngxHealthPort,
ngxVtsPath: ngxVtsPath,
port: port,
path: path,
watchNamespace: watchNamespace,
ingressClass: ingressClass,
}
Expand Down Expand Up @@ -201,7 +201,7 @@ func (p vtsCollector) Stop() {

// scrapeVts scrape nginx vts metrics
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 {
glog.Warningf("unexpected error obtaining nginx status info: %v", err)
return
Expand Down
17 changes: 1 addition & 16 deletions controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ http {
# Changing this value requires a change in:
# https://github.com/kubernetes/ingress/blob/master/controllers/nginx/pkg/cmd/controller/nginx.go
listen 127.0.0.1:{{ $all.ListenPorts.Status }} default_server reuseport backlog={{ $all.BacklogSize }};
{{ if $IsIPV6Enabled }}listen [::1]:{{ $all.ListenPorts.Status }} default_server reuseport backlog={{ $all.BacklogSize }};{{ end }}
{{ if $IsIPV6Enabled }}listen [::]:{{ $all.ListenPorts.Status }} default_server reuseport backlog={{ $all.BacklogSize }};{{ end }}
set $proxy_upstream_name "-";

location {{ $healthzURI }} {
Expand All @@ -407,21 +407,6 @@ http {
{{ 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_Format $http_accept;
fastcgi_param HTTP_X_Original_URI $request_uri;
Expand Down

0 comments on commit cda42f9

Please sign in to comment.