Fix build

This commit is contained in:
Manuel de Brito Fontes 2017-03-12 12:27:05 -03:00
parent 7ba389c1d0
commit e702c55820
10 changed files with 440 additions and 299 deletions

View file

@ -21,7 +21,7 @@ build: clean
-ldflags "-s -w -X ${PKG}/pkg/version.RELEASE=${RELEASE} -X ${PKG}/pkg/version.COMMIT=${COMMIT} -X ${PKG}/pkg/version.REPO=${REPO_INFO}" \ -ldflags "-s -w -X ${PKG}/pkg/version.RELEASE=${RELEASE} -X ${PKG}/pkg/version.COMMIT=${COMMIT} -X ${PKG}/pkg/version.REPO=${REPO_INFO}" \
-o rootfs/nginx-ingress-controller ${PKG}/pkg/cmd/controller -o rootfs/nginx-ingress-controller ${PKG}/pkg/cmd/controller
container: build container:
$(DOCKER) build --pull -t $(PREFIX):$(RELEASE) rootfs $(DOCKER) build --pull -t $(PREFIX):$(RELEASE) rootfs
push: container push: container

View file

@ -23,31 +23,73 @@ import (
"k8s.io/ingress/controllers/nginx/pkg/metric/collector" "k8s.io/ingress/controllers/nginx/pkg/metric/collector"
) )
const (
ngxStatusPath = "/internal_nginx_status"
ngxVtsPath = "/nginx_status/format/json"
)
func (n *NGINXController) setupMonitor(sm statusModule) { func (n *NGINXController) setupMonitor(sm statusModule) {
csm := n.statusModule csm := n.statusModule
if csm != sm { if csm != sm {
prometheus glog.Infof("changing prometheus collector from %v to %v", csm, sm)
n.stats.stop(csm)
n.stats.start(sm)
n.statusModule = sm n.statusModule = sm
} }
} }
type statsCollector struct { type statsCollector struct {
process prometheus.Collector process prometheus.Collector
basic prometheus.Collector basic collector.Stopable
vts prometheus.Collector vts collector.Stopable
namespace string
watchClass string
} }
func newStatsCollector() (*statsCollector, error) { func (s *statsCollector) stop(sm statusModule) {
pc, err := collector.NewNamedProcess(true, collector.BinaryNameMatcher{"nginx", n.cmdArgs}) switch sm {
case defaultStatusModule:
s.basic.Stop()
prometheus.Unregister(s.basic)
break
case vtsStatusModule:
s.vts.Stop()
prometheus.Unregister(s.vts)
break
}
}
func (s *statsCollector) start(sm statusModule) {
switch sm {
case defaultStatusModule:
s.basic = collector.NewNginxStatus(s.namespace, s.watchClass, ngxHealthPort, ngxStatusPath)
prometheus.Register(s.basic)
break
case vtsStatusModule:
s.vts = collector.NewNGINXVTSCollector(s.namespace, s.watchClass, ngxHealthPort, ngxVtsPath)
prometheus.Register(s.vts)
break
}
}
func newStatsCollector(ns, class, binary string) *statsCollector {
glog.Infof("starting new nginx stats collector for Ingress controller running in namespace %v (class %v)", ns, class)
pc, err := collector.NewNamedProcess(true, collector.BinaryNameMatcher{
Name: "nginx",
Binary: binary,
})
if err != nil { if err != nil {
return nil, err glog.Fatalf("unexpected error registering nginx collector: %v", err)
} }
err = prometheus.Register(pc) err = prometheus.Register(pc)
if err != nil { if err != nil {
glog.Fatalf("unexpected error registering nginx collector: %v", err) glog.Fatalf("unexpected error registering nginx collector: %v", err)
} }
return nil, &statsCollector{ return &statsCollector{
process: pc, namespace: ns,
watchClass: class,
process: pc,
} }
} }

View file

@ -48,8 +48,6 @@ type statusModule string
const ( const (
ngxHealthPort = 18080 ngxHealthPort = 18080
ngxHealthPath = "/healthz" ngxHealthPath = "/healthz"
ngxStatusPath = "/internal_nginx_status"
ngxVtsPath = "/nginx_status/format/json"
defaultStatusModule statusModule = "default" defaultStatusModule statusModule = "default"
vtsStatusModule statusModule = "vts" vtsStatusModule statusModule = "vts"
@ -70,7 +68,7 @@ func newNGINXController() ingress.Controller {
if ngx == "" { if ngx == "" {
ngx = binary ngx = binary
} }
n := NGINXController{ n := &NGINXController{
binary: ngx, binary: ngx,
configmap: &api.ConfigMap{}, configmap: &api.ConfigMap{},
} }
@ -102,7 +100,7 @@ Error loading new template : %v
go n.Start() go n.Start()
return ingress.Controller(&n) return ingress.Controller(n)
} }
// NGINXController ... // NGINXController ...
@ -117,11 +115,15 @@ type NGINXController struct {
cmdArgs []string cmdArgs []string
watchClass string
namespace string
stats *statsCollector
statusModule statusModule statusModule statusModule
} }
// Start start a new NGINX master process running in foreground. // Start start a new NGINX master process running in foreground.
func (n NGINXController) Start() { func (n *NGINXController) Start() {
glog.Info("starting NGINX process...") glog.Info("starting NGINX process...")
done := make(chan error, 1) done := make(chan error, 1)
@ -170,15 +172,6 @@ func (n *NGINXController) start(cmd *exec.Cmd, done chan error) {
n.cmdArgs = cmd.Args n.cmdArgs = cmd.Args
cfg := ngx_template.ReadConfig(n.configmap.Data)
n.statusModule = defaultStatusModule
if cfg.EnableVtsStatus {
n.statusModule = vtsStatusModule
n.setupMonitor(vtsStatusModule)
} else {
n.setupMonitor(defaultStatusModule)
}
go func() { go func() {
done <- cmd.Wait() done <- cmd.Wait()
}() }()
@ -264,12 +257,20 @@ func (n NGINXController) Info() *ingress.BackendInfo {
} }
// OverrideFlags customize NGINX controller flags // OverrideFlags customize NGINX controller flags
func (n NGINXController) OverrideFlags(flags *pflag.FlagSet) { func (n *NGINXController) OverrideFlags(flags *pflag.FlagSet) {
ig, err := flags.GetString("ingress-class") ic, _ := flags.GetString("ingress-class")
if err == nil && ig != "" && ig != defIngressClass { wc, _ := flags.GetString("watch-namespace")
glog.Warningf("only Ingress with class %v will be processed by this ingress controller", ig)
if ic == "" {
ic = defIngressClass
} }
flags.Set("ingress-class", defIngressClass)
if ic != defIngressClass {
glog.Warningf("only Ingress with class %v will be processed by this ingress controller", ic)
}
flags.Set("ingress-class", ic)
n.stats = newStatsCollector(ic, wc, n.binary)
} }
// DefaultIngressClass just return the default ingress class // DefaultIngressClass just return the default ingress class
@ -336,7 +337,11 @@ func (n *NGINXController) OnUpdate(ingressCfg ingress.Configuration) ([]byte, er
cfg := ngx_template.ReadConfig(n.configmap.Data) cfg := ngx_template.ReadConfig(n.configmap.Data)
// we need to check if the status module configuration changed // we need to check if the status module configuration changed
n.setupMonitor() if cfg.EnableVtsStatus {
n.setupMonitor(vtsStatusModule)
} else {
n.setupMonitor(defaultStatusModule)
}
// NGINX cannot resize the has tables used to store server names. // NGINX cannot resize the has tables used to store server names.
// For this reason we check if the defined size defined is correct // For this reason we check if the defined size defined is correct

View file

@ -17,72 +17,103 @@ limitations under the License.
package collector package collector
import ( import (
"fmt"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
var (
activeDesc = prometheus.NewDesc(
"nginx_active_connections",
"total number of active connections",
nil, nil)
acceptedDesc = prometheus.NewDesc(
"nginx_accepted_connections",
"total number of accepted client connections",
nil, nil)
handledDesc = prometheus.NewDesc(
"nginx_handled_connections",
"total number of handled connections",
nil, nil)
requestsDesc = prometheus.NewDesc(
"nginx_total_requests",
"total number of client requests",
nil, nil)
readingDesc = prometheus.NewDesc(
"nginx_current_reading_connections",
"current number of connections where nginx is reading the request header",
nil, nil)
writingDesc = prometheus.NewDesc(
"nginx_current_writing_connections",
"current number of connections where nginx is writing the response back to the client",
nil, nil)
waitingDesc = prometheus.NewDesc(
"nginx_current_waiting_connections",
"current number of idle client connections waiting for a request",
nil, nil)
)
type ( type (
nginxStatusCollector struct { nginxStatusCollector struct {
scrapeChan chan scrapeRequest scrapeChan chan scrapeRequest
ngxHealthPort int
ngxVtsPath string
data *nginxStatusData
}
nginxStatusData struct {
active *prometheus.Desc
accepted *prometheus.Desc
handled *prometheus.Desc
requests *prometheus.Desc
reading *prometheus.Desc
writing *prometheus.Desc
waiting *prometheus.Desc
} }
) )
func NewNginxStatus() (prometheus.Collector, error) { func buildNS(namespace, class string) string {
if namespace == "" {
namespace = "all"
}
if class == "" {
class = "all"
}
return fmt.Sprintf("%v_%v", namespace, class)
}
// NewNginxStatus returns a new prometheus collector the default nginx status module
func NewNginxStatus(namespace, class string, ngxHealthPort int, ngxVtsPath string) Stopable {
p := nginxStatusCollector{ p := nginxStatusCollector{
scrapeChan: make(chan scrapeRequest), scrapeChan: make(chan scrapeRequest),
ngxHealthPort: ngxHealthPort,
ngxVtsPath: ngxVtsPath,
}
ns := buildNS(namespace, class)
p.data = &nginxStatusData{
active: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "active_connections"),
"total number of active connections",
nil, nil),
accepted: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "accepted_connections"),
"total number of accepted client connections",
nil, nil),
handled: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "handled_connections"),
"total number of handled connections",
nil, nil),
requests: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "total_requests"),
"total number of client requests",
nil, nil),
reading: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "current_reading_connections"),
"current number of connections where nginx is reading the request header",
nil, nil),
writing: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "current_writing_connections"),
"current number of connections where nginx is writing the response back to the client",
nil, nil),
waiting: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "current_waiting_connections"),
"current number of idle client connections waiting for a request",
nil, nil),
} }
go p.start() go p.start()
return p, nil return p
} }
// Describe implements prometheus.Collector. // Describe implements prometheus.Collector.
func (p nginxStatusCollector) Describe(ch chan<- *prometheus.Desc) { func (p nginxStatusCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- activeDesc ch <- p.data.active
ch <- acceptedDesc ch <- p.data.accepted
ch <- handledDesc ch <- p.data.handled
ch <- requestsDesc ch <- p.data.requests
ch <- readingDesc ch <- p.data.reading
ch <- writingDesc ch <- p.data.writing
ch <- waitingDesc ch <- p.data.waiting
} }
// Collect implements prometheus.Collector. // Collect implements prometheus.Collector.
@ -106,25 +137,24 @@ func (p nginxStatusCollector) Stop() {
// nginxStatusCollector scrap the nginx status // nginxStatusCollector scrap the nginx status
func (p nginxStatusCollector) scrape(ch chan<- prometheus.Metric) { func (p nginxStatusCollector) scrape(ch chan<- prometheus.Metric) {
s, err := getNginxStatus() s, err := getNginxStatus(p.ngxHealthPort, p.ngxVtsPath)
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
} }
ch <- prometheus.MustNewConstMetric(activeDesc, ch <- prometheus.MustNewConstMetric(p.data.active,
prometheus.GaugeValue, float64(s.Active)) prometheus.GaugeValue, float64(s.Active))
ch <- prometheus.MustNewConstMetric(acceptedDesc, ch <- prometheus.MustNewConstMetric(p.data.accepted,
prometheus.GaugeValue, float64(s.Accepted)) prometheus.GaugeValue, float64(s.Accepted))
ch <- prometheus.MustNewConstMetric(handledDesc, ch <- prometheus.MustNewConstMetric(p.data.handled,
prometheus.GaugeValue, float64(s.Handled)) prometheus.GaugeValue, float64(s.Handled))
ch <- prometheus.MustNewConstMetric(requestsDesc, ch <- prometheus.MustNewConstMetric(p.data.requests,
prometheus.GaugeValue, float64(s.Requests)) prometheus.GaugeValue, float64(s.Requests))
ch <- prometheus.MustNewConstMetric(readingDesc, ch <- prometheus.MustNewConstMetric(p.data.reading,
prometheus.GaugeValue, float64(s.Reading)) prometheus.GaugeValue, float64(s.Reading))
ch <- prometheus.MustNewConstMetric(writingDesc, ch <- prometheus.MustNewConstMetric(p.data.writing,
prometheus.GaugeValue, float64(s.Writing)) prometheus.GaugeValue, float64(s.Writing))
ch <- prometheus.MustNewConstMetric(waitingDesc, ch <- prometheus.MustNewConstMetric(p.data.waiting,
prometheus.GaugeValue, float64(s.Waiting)) prometheus.GaugeValue, float64(s.Waiting))
} }

View file

@ -25,63 +25,42 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
// BinaryNameMatcher ...
type BinaryNameMatcher struct { type BinaryNameMatcher struct {
name string Name string
args []string Binary string
} }
// MatchAndName returns false if the match failed, otherwise
// true and the resulting name.
func (em BinaryNameMatcher) MatchAndName(nacl common.NameAndCmdline) (bool, string) { func (em BinaryNameMatcher) MatchAndName(nacl common.NameAndCmdline) (bool, string) {
if len(nacl.Cmdline) == 0 { if len(nacl.Cmdline) == 0 {
return false, "" return false, ""
} }
cmd := filepath.Base(nacl.Cmdline[0]) cmd := filepath.Base(em.Binary)
return em.name == cmd, "" return em.Name == cmd, ""
} }
var ( type namedProcessData struct {
numprocsDesc = prometheus.NewDesc( numProcs *prometheus.Desc
"nginx_num_procs", cpuSecs *prometheus.Desc
"number of processes", readBytes *prometheus.Desc
nil, nil) writeBytes *prometheus.Desc
memResidentbytes *prometheus.Desc
cpuSecsDesc = prometheus.NewDesc( memVirtualbytes *prometheus.Desc
"nginx_cpu_seconds_total", startTime *prometheus.Desc
"Cpu usage in seconds", }
nil, nil)
readBytesDesc = prometheus.NewDesc(
"nginx_read_bytes_total",
"number of bytes read",
nil, nil)
writeBytesDesc = prometheus.NewDesc(
"nginx_write_bytes_total",
"number of bytes written",
nil, nil)
memResidentbytesDesc = prometheus.NewDesc(
"nginx_resident_memory_bytes",
"number of bytes of memory in use",
nil, nil)
memVirtualbytesDesc = prometheus.NewDesc(
"nginx_virtual_memory_bytes",
"number of bytes of memory in use",
nil, nil)
startTimeDesc = prometheus.NewDesc(
"nginx_oldest_start_time_seconds",
"start time in seconds since 1970/01/01",
nil, nil)
)
type namedProcess struct { type namedProcess struct {
scrapeChan chan scrapeRequest
*proc.Grouper *proc.Grouper
fs *proc.FS
scrapeChan chan scrapeRequest
fs *proc.FS
data namedProcessData
} }
func NewNamedProcessCollector(children bool, mn common.MatchNamer) (prometheus.Collector, error) { // NewNamedProcess returns a new prometheus collector for the nginx process
func NewNamedProcess(children bool, mn common.MatchNamer) (prometheus.Collector, error) {
fs, err := proc.NewFS("/proc") fs, err := proc.NewFS("/proc")
if err != nil { if err != nil {
return nil, err return nil, err
@ -96,6 +75,43 @@ func NewNamedProcessCollector(children bool, mn common.MatchNamer) (prometheus.C
return nil, err return nil, err
} }
p.data = namedProcessData{
numProcs: prometheus.NewDesc(
"num_procs",
"number of processes",
nil, nil),
cpuSecs: prometheus.NewDesc(
"cpu_seconds_total",
"Cpu usage in seconds",
nil, nil),
readBytes: prometheus.NewDesc(
"read_bytes_total",
"number of bytes read",
nil, nil),
writeBytes: prometheus.NewDesc(
"write_bytes_total",
"number of bytes written",
nil, nil),
memResidentbytes: prometheus.NewDesc(
"resident_memory_bytes",
"number of bytes of memory in use",
nil, nil),
memVirtualbytes: prometheus.NewDesc(
"virtual_memory_bytes",
"number of bytes of memory in use",
nil, nil),
startTime: prometheus.NewDesc(
"oldest_start_time_seconds",
"start time in seconds since 1970/01/01",
nil, nil),
}
go p.start() go p.start()
return p, nil return p, nil
@ -103,13 +119,13 @@ func NewNamedProcessCollector(children bool, mn common.MatchNamer) (prometheus.C
// Describe implements prometheus.Collector. // Describe implements prometheus.Collector.
func (p namedProcess) Describe(ch chan<- *prometheus.Desc) { func (p namedProcess) Describe(ch chan<- *prometheus.Desc) {
ch <- cpuSecsDesc ch <- p.data.cpuSecs
ch <- numprocsDesc ch <- p.data.numProcs
ch <- readBytesDesc ch <- p.data.readBytes
ch <- writeBytesDesc ch <- p.data.writeBytes
ch <- memResidentbytesDesc ch <- p.data.memResidentbytes
ch <- memVirtualbytesDesc ch <- p.data.memVirtualbytes
ch <- startTimeDesc ch <- p.data.startTime
} }
// Collect implements prometheus.Collector. // Collect implements prometheus.Collector.
@ -138,20 +154,20 @@ func (p namedProcess) scrape(ch chan<- prometheus.Metric) {
return return
} }
for gname, gcounts := range p.Groups() { for _, gcounts := range p.Groups() {
ch <- prometheus.MustNewConstMetric(numprocsDesc, ch <- prometheus.MustNewConstMetric(p.data.numProcs,
prometheus.GaugeValue, float64(gcounts.Procs)) prometheus.GaugeValue, float64(gcounts.Procs))
ch <- prometheus.MustNewConstMetric(memResidentbytesDesc, ch <- prometheus.MustNewConstMetric(p.data.memResidentbytes,
prometheus.GaugeValue, float64(gcounts.Memresident)) prometheus.GaugeValue, float64(gcounts.Memresident))
ch <- prometheus.MustNewConstMetric(memVirtualbytesDesc, ch <- prometheus.MustNewConstMetric(p.data.memVirtualbytes,
prometheus.GaugeValue, float64(gcounts.Memvirtual)) prometheus.GaugeValue, float64(gcounts.Memvirtual))
ch <- prometheus.MustNewConstMetric(startTimeDesc, ch <- prometheus.MustNewConstMetric(p.data.startTime,
prometheus.GaugeValue, float64(gcounts.OldestStartTime.Unix())) prometheus.GaugeValue, float64(gcounts.OldestStartTime.Unix()))
ch <- prometheus.MustNewConstMetric(cpuSecsDesc, ch <- prometheus.MustNewConstMetric(p.data.cpuSecs,
prometheus.CounterValue, gcounts.Cpu) prometheus.CounterValue, gcounts.Cpu)
ch <- prometheus.MustNewConstMetric(readBytesDesc, ch <- prometheus.MustNewConstMetric(p.data.readBytes,
prometheus.CounterValue, float64(gcounts.ReadBytes)) prometheus.CounterValue, float64(gcounts.ReadBytes))
ch <- prometheus.MustNewConstMetric(writeBytesDesc, ch <- prometheus.MustNewConstMetric(p.data.writeBytes,
prometheus.CounterValue, float64(gcounts.WriteBytes)) prometheus.CounterValue, float64(gcounts.WriteBytes))
} }
} }

View file

@ -18,6 +18,12 @@ package collector
import "github.com/prometheus/client_golang/prometheus" import "github.com/prometheus/client_golang/prometheus"
// Stopable defines a prometheus collector that can be stopped
type Stopable interface {
prometheus.Collector
Stop()
}
type scrapeRequest struct { type scrapeRequest struct {
results chan<- prometheus.Metric results chan<- prometheus.Metric
done chan struct{} done chan struct{}

View file

@ -73,14 +73,14 @@ type serverZone struct {
InBytes float64 `json:"inBytes"` InBytes float64 `json:"inBytes"`
OutBytes float64 `json:"outBytes"` OutBytes float64 `json:"outBytes"`
Responses response `json:"responses"` Responses response `json:"responses"`
Cache cache `json:"responses"` Cache cache `json:"cache"`
} }
type filterZone struct { type filterZone struct {
RequestCounter float64 `json:"requestCounter"` RequestCounter float64 `json:"requestCounter"`
InBytes float64 `json:"inBytes"` InBytes float64 `json:"inBytes"`
OutBytes float64 `json:"outBytes"` OutBytes float64 `json:"outBytes"`
Cache cache `json:"responses"` Cache cache `json:"cache"`
Responses response `json:"responses"` Responses response `json:"responses"`
} }
@ -127,8 +127,10 @@ type connections struct {
Requests float64 `json:"requests"` Requests float64 `json:"requests"`
} }
// BoolToFloat64 ...
type BoolToFloat64 float64 type BoolToFloat64 float64
// UnmarshalJSON ...
func (bit BoolToFloat64) UnmarshalJSON(data []byte) error { func (bit BoolToFloat64) UnmarshalJSON(data []byte) error {
asString := string(data) asString := string(data)
if asString == "1" || asString == "true" { if asString == "1" || asString == "true" {
@ -141,7 +143,7 @@ func (bit BoolToFloat64) UnmarshalJSON(data []byte) error {
return nil return nil
} }
func getNginxStatus() (*basicStatus, error) { func getNginxStatus(ngxHealthPort int, ngxStatusPath string) (*basicStatus, error) {
url := fmt.Sprintf("http://localhost:%v%v", ngxHealthPort, ngxStatusPath) url := fmt.Sprintf("http://localhost:%v%v", ngxHealthPort, ngxStatusPath)
glog.V(3).Infof("start scrapping url: %v", url) glog.V(3).Infof("start scrapping url: %v", url)
@ -172,7 +174,7 @@ func httpBody(url string) ([]byte, error) {
return data, nil return data, nil
} }
func getNginxVtsMetrics() (*vts, error) { func getNginxVtsMetrics(ngxHealthPort int, ngxVtsPath string) (*vts, error) {
url := fmt.Sprintf("http://localhost:%v%v", ngxHealthPort, ngxVtsPath) url := fmt.Sprintf("http://localhost:%v%v", ngxHealthPort, ngxVtsPath)
glog.V(3).Infof("start scrapping url: %v", url) glog.V(3).Infof("start scrapping url: %v", url)

View file

@ -17,32 +17,34 @@ limitations under the License.
package collector package collector
import ( import (
"reflect"
"testing" "testing"
"github.com/kylelemons/godebug/pretty"
) )
func TestParseStatus(t *testing.T) { func TestParseStatus(t *testing.T) {
tests := []struct { tests := []struct {
in string in string
out *nginxStatus out *basicStatus
}{ }{
{`Active connections: 43 {`Active connections: 43
server accepts handled requests server accepts handled requests
7368 7368 10993 7368 7368 10993
Reading: 0 Writing: 5 Waiting: 38`, Reading: 0 Writing: 5 Waiting: 38`,
&nginxStatus{43, 7368, 7368, 10993, 0, 5, 38}, &basicStatus{43, 7368, 7368, 10993, 0, 5, 38},
}, },
{`Active connections: 0 {`Active connections: 0
server accepts handled requests server accepts handled requests
1 7 0 1 7 0
Reading: A Writing: B Waiting: 38`, Reading: A Writing: B Waiting: 38`,
&nginxStatus{0, 1, 7, 0, 0, 0, 38}, &basicStatus{0, 1, 7, 0, 0, 0, 38},
}, },
} }
for _, test := range tests { for _, test := range tests {
r := parse(test.in) r := parse(test.in)
if !reflect.DeepEqual(r, test.out) { if diff := pretty.Compare(r, test.out); diff != "" {
t.Logf("%v", diff)
t.Fatalf("expected %v but returned %v", test.out, r) t.Fatalf("expected %v but returned %v", test.out, r)
} }
} }

View file

@ -23,136 +23,168 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
var ( const system = "nginx"
vtsBytesDesc = prometheus.NewDesc(
"nginx_vts_bytes_total",
"Nginx bytes count",
[]string{"server_zone", "direction"}, nil)
vtsCacheDesc = prometheus.NewDesc( type (
"nginx_vts_cache_total", vtsCollector struct {
"Nginx cache count", scrapeChan chan scrapeRequest
[]string{"server_zone", "type"}, nil) ngxHealthPort int
ngxVtsPath string
vtsConnectionsDesc = prometheus.NewDesc( data *vtsData
"nginx_vts_connections_total",
"Nginx connections count",
[]string{"type"}, nil)
vtsResponseDesc = prometheus.NewDesc(
"nginx_vts_responses_total",
"The number of responses with status codes 1xx, 2xx, 3xx, 4xx, and 5xx.",
[]string{"server_zone", "status_code"}, nil)
vtsRequestDesc = prometheus.NewDesc(
"nginx_vts_requests_total",
"The total number of requested client connections.",
[]string{"server_zone"}, nil)
vtsFilterZoneBytesDesc = prometheus.NewDesc(
"nginx_vts_filterzone_bytes_total",
"Nginx bytes count",
[]string{"server_zone", "country", "direction"}, nil)
vtsFilterZoneResponseDesc = prometheus.NewDesc(
"nginx_vts_filterzone_responses_total",
"The number of responses with status codes 1xx, 2xx, 3xx, 4xx, and 5xx.",
[]string{"server_zone", "country", "status_code"}, nil)
vtsFilterZoneCacheDesc = prometheus.NewDesc(
"nginx_vts_filterzone_cache_total",
"Nginx cache count",
[]string{"server_zone", "country", "type"}, nil)
vtsUpstreamBackupDesc = prometheus.NewDesc(
"nginx_vts_upstream_backup",
"Current backup setting of the server.",
[]string{"upstream", "server"}, nil)
vtsUpstreamBytesDesc = prometheus.NewDesc(
"nginx_vts_upstream_bytes_total",
"The total number of bytes sent to this server.",
[]string{"upstream", "server", "direction"}, nil)
vtsUpstreamDownDesc = prometheus.NewDesc(
"nginx_vts_upstream_down_total",
"Current down setting of the server.",
[]string{"upstream", "server"}, nil)
vtsUpstreamFailTimeoutDesc = prometheus.NewDesc(
"nginx_vts_upstream_fail_timeout",
"Current fail_timeout setting of the server.",
[]string{"upstream", "server"}, nil)
vtsUpstreamMaxFailsDesc = prometheus.NewDesc(
"nginx_vts_upstream_maxfails",
"Current max_fails setting of the server.",
[]string{"upstream", "server"}, nil)
vtsUpstreamResponsesDesc = prometheus.NewDesc(
"nginx_vts_upstream_responses_total",
"The number of upstream responses with status codes 1xx, 2xx, 3xx, 4xx, and 5xx.",
[]string{"upstream", "server", "status_code"}, nil)
vtsUpstreamRequestDesc = prometheus.NewDesc(
"nginx_vts_upstream_requests_total",
"The total number of client connections forwarded to this server.",
[]string{"upstream", "server"}, nil)
vtsUpstreamResponseMsecDesc = prometheus.NewDesc(
"nginx_vts_upstream_response_msecs_avg",
"The average of only upstream response processing times in milliseconds.",
[]string{"upstream", "server"}, nil)
vtsUpstreamWeightDesc = prometheus.NewDesc(
"nginx_vts_upstream_weight",
"Current upstream weight setting of the server.",
[]string{"upstream", "server"}, nil)
)
type vtsCollector struct {
scrapeChan chan scrapeRequest
} }
func NewNGINXVTSCollector() (prometheus.Collector, error) { vtsData struct {
bytes *prometheus.Desc
cache *prometheus.Desc
connections *prometheus.Desc
response *prometheus.Desc
request *prometheus.Desc
filterZoneBytes *prometheus.Desc
filterZoneResponse *prometheus.Desc
filterZoneCache *prometheus.Desc
upstreamBackup *prometheus.Desc
upstreamBytes *prometheus.Desc
upstreamDown *prometheus.Desc
upstreamFailTimeout *prometheus.Desc
upstreamMaxFails *prometheus.Desc
upstreamResponses *prometheus.Desc
upstreamRequest *prometheus.Desc
upstreamResponseMsec *prometheus.Desc
upstreamWeight *prometheus.Desc
}
)
// NewNGINXVTSCollector returns a new prometheus collector for the VTS module
func NewNGINXVTSCollector(namespace, class string, ngxHealthPort int, ngxVtsPath string) Stopable {
p := vtsCollector{ p := vtsCollector{
scrapeChan: make(chan scrapeRequest), scrapeChan: make(chan scrapeRequest),
ngxHealthPort: ngxHealthPort,
ngxVtsPath: ngxVtsPath,
}
ns := buildNS(namespace, class)
p.data = &vtsData{
bytes: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "bytes_total"),
"Nginx bytes count",
[]string{"server_zone", "direction"}, nil),
cache: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "cache_total"),
"Nginx cache count",
[]string{"server_zone", "type"}, nil),
connections: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "connections_total"),
"Nginx connections count",
[]string{"type"}, nil),
response: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "responses_total"),
"The number of responses with status codes 1xx, 2xx, 3xx, 4xx, and 5xx.",
[]string{"server_zone", "status_code"}, nil),
request: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "requests_total"),
"The total number of requested client connections.",
[]string{"server_zone"}, nil),
filterZoneBytes: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "filterzone_bytes_total"),
"Nginx bytes count",
[]string{"server_zone", "country", "direction"}, nil),
filterZoneResponse: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "filterzone_responses_total"),
"The number of responses with status codes 1xx, 2xx, 3xx, 4xx, and 5xx.",
[]string{"server_zone", "country", "status_code"}, nil),
filterZoneCache: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "filterzone_cache_total"),
"Nginx cache count",
[]string{"server_zone", "country", "type"}, nil),
upstreamBackup: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "upstream_backup"),
"Current backup setting of the server.",
[]string{"upstream", "server"}, nil),
upstreamBytes: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "upstream_bytes_total"),
"The total number of bytes sent to this server.",
[]string{"upstream", "server", "direction"}, nil),
upstreamDown: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "vts_upstream_down_total"),
"Current down setting of the server.",
[]string{"upstream", "server"}, nil),
upstreamFailTimeout: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "upstream_fail_timeout"),
"Current fail_timeout setting of the server.",
[]string{"upstream", "server"}, nil),
upstreamMaxFails: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "upstream_maxfails"),
"Current max_fails setting of the server.",
[]string{"upstream", "server"}, nil),
upstreamResponses: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "upstream_responses_total"),
"The number of upstream responses with status codes 1xx, 2xx, 3xx, 4xx, and 5xx.",
[]string{"upstream", "server", "status_code"}, nil),
upstreamRequest: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "upstream_requests_total"),
"The total number of client connections forwarded to this server.",
[]string{"upstream", "server"}, nil),
upstreamResponseMsec: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "upstream_response_msecs_avg"),
"The average of only upstream response processing times in milliseconds.",
[]string{"upstream", "server"}, nil),
upstreamWeight: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "upstream_weight"),
"Current upstream weight setting of the server.",
[]string{"upstream", "server"}, nil),
} }
go p.start() go p.start()
return p, nil return p
} }
// Describe implements prometheus.Collector. // Describe implements prometheus.Collector.
func (p *vtsCollector) Describe(ch chan<- *prometheus.Desc) { func (p vtsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- vtsBytesDesc ch <- p.data.bytes
ch <- vtsCacheDesc ch <- p.data.cache
ch <- vtsConnectionsDesc ch <- p.data.connections
ch <- vtsRequestDesc ch <- p.data.request
ch <- vtsResponseDesc ch <- p.data.response
ch <- vtsUpstreamBackupDesc ch <- p.data.upstreamBackup
ch <- vtsUpstreamBytesDesc ch <- p.data.upstreamBytes
ch <- vtsUpstreamDownDesc ch <- p.data.upstreamDown
ch <- vtsUpstreamFailTimeoutDesc ch <- p.data.upstreamFailTimeout
ch <- vtsUpstreamMaxFailsDesc ch <- p.data.upstreamMaxFails
ch <- vtsUpstreamRequestDesc ch <- p.data.upstreamRequest
ch <- vtsUpstreamResponseMsecDesc ch <- p.data.upstreamResponseMsec
ch <- vtsUpstreamResponsesDesc ch <- p.data.upstreamResponses
ch <- vtsUpstreamWeightDesc ch <- p.data.upstreamWeight
ch <- vtsFilterZoneBytesDesc ch <- p.data.filterZoneBytes
ch <- vtsFilterZoneCacheDesc ch <- p.data.filterZoneCache
ch <- vtsFilterZoneResponseDesc ch <- p.data.filterZoneResponse
} }
// Collect implements prometheus.Collector. // Collect implements prometheus.Collector.
func (p *vtsCollector) Collect(ch chan<- prometheus.Metric) { func (p vtsCollector) Collect(ch chan<- prometheus.Metric) {
req := scrapeRequest{results: ch, done: make(chan struct{})} req := scrapeRequest{results: ch, done: make(chan struct{})}
p.scrapeChan <- req p.scrapeChan <- req
<-req.done <-req.done
} }
func (p *vtsCollector) start() { func (p vtsCollector) start() {
for req := range p.scrapeChan { for req := range p.scrapeChan {
ch := req.results ch := req.results
p.scrapeVts(ch) p.scrapeVts(ch)
@ -160,65 +192,65 @@ func (p *vtsCollector) start() {
} }
} }
func (p *vtsCollector) Stop() { func (p vtsCollector) Stop() {
close(p.scrapeChan) close(p.scrapeChan)
} }
// 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() nginxMetrics, err := getNginxVtsMetrics(p.ngxHealthPort, p.ngxVtsPath)
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
} }
reflectMetrics(&nginxMetrics.Connections, vtsConnectionsDesc, ch) reflectMetrics(&nginxMetrics.Connections, p.data.connections, ch)
for name, zones := range nginxMetrics.UpstreamZones { for name, zones := range nginxMetrics.UpstreamZones {
for pos, value := range zones { for pos, value := range zones {
reflectMetrics(&zones[pos].Responses, vtsUpstreamResponsesDesc, ch, name, value.Server) reflectMetrics(&zones[pos].Responses, p.data.upstreamResponses, ch, name, value.Server)
ch <- prometheus.MustNewConstMetric(vtsUpstreamRequestDesc, ch <- prometheus.MustNewConstMetric(p.data.upstreamRequest,
prometheus.CounterValue, float64(zones[pos].RequestCounter), name, value.Server) prometheus.CounterValue, zones[pos].RequestCounter, name, value.Server)
ch <- prometheus.MustNewConstMetric(vtsUpstreamDownDesc, ch <- prometheus.MustNewConstMetric(p.data.upstreamDown,
prometheus.CounterValue, float64(zones[pos].Down), name, value.Server) prometheus.CounterValue, float64(zones[pos].Down), name, value.Server)
ch <- prometheus.MustNewConstMetric(vtsUpstreamWeightDesc, ch <- prometheus.MustNewConstMetric(p.data.upstreamWeight,
prometheus.CounterValue, float64(zones[pos].Weight), name, value.Server) prometheus.CounterValue, zones[pos].Weight, name, value.Server)
ch <- prometheus.MustNewConstMetric(vtsUpstreamResponseMsecDesc, ch <- prometheus.MustNewConstMetric(p.data.upstreamResponseMsec,
prometheus.CounterValue, float64(zones[pos].ResponseMsec), name, value.Server) prometheus.CounterValue, zones[pos].ResponseMsec, name, value.Server)
ch <- prometheus.MustNewConstMetric(vtsUpstreamBackupDesc, ch <- prometheus.MustNewConstMetric(p.data.upstreamBackup,
prometheus.CounterValue, float64(zones[pos].Backup), name, value.Server) prometheus.CounterValue, float64(zones[pos].Backup), name, value.Server)
ch <- prometheus.MustNewConstMetric(vtsUpstreamFailTimeoutDesc, ch <- prometheus.MustNewConstMetric(p.data.upstreamFailTimeout,
prometheus.CounterValue, float64(zones[pos].FailTimeout), name, value.Server) prometheus.CounterValue, zones[pos].FailTimeout, name, value.Server)
ch <- prometheus.MustNewConstMetric(vtsUpstreamMaxFailsDesc, ch <- prometheus.MustNewConstMetric(p.data.upstreamMaxFails,
prometheus.CounterValue, float64(zones[pos].MaxFails), name, value.Server) prometheus.CounterValue, zones[pos].MaxFails, name, value.Server)
ch <- prometheus.MustNewConstMetric(vtsUpstreamBytesDesc, ch <- prometheus.MustNewConstMetric(p.data.upstreamBytes,
prometheus.CounterValue, float64(zones[pos].InBytes), name, value.Server, "in") prometheus.CounterValue, zones[pos].InBytes, name, value.Server, "in")
ch <- prometheus.MustNewConstMetric(vtsUpstreamBytesDesc, ch <- prometheus.MustNewConstMetric(p.data.upstreamBytes,
prometheus.CounterValue, float64(zones[pos].OutBytes), name, value.Server, "out") prometheus.CounterValue, zones[pos].OutBytes, name, value.Server, "out")
} }
} }
for name, zone := range nginxMetrics.ServerZones { for name, zone := range nginxMetrics.ServerZones {
reflectMetrics(&zone.Responses, vtsResponseDesc, ch, name) reflectMetrics(&zone.Responses, p.data.response, ch, name)
reflectMetrics(&zone.Cache, vtsCacheDesc, ch, name) reflectMetrics(&zone.Cache, p.data.cache, ch, name)
ch <- prometheus.MustNewConstMetric(vtsRequestDesc, ch <- prometheus.MustNewConstMetric(p.data.request,
prometheus.CounterValue, float64(zone.RequestCounter), name) prometheus.CounterValue, zone.RequestCounter, name)
ch <- prometheus.MustNewConstMetric(vtsBytesDesc, ch <- prometheus.MustNewConstMetric(p.data.bytes,
prometheus.CounterValue, float64(zone.InBytes), name, "in") prometheus.CounterValue, zone.InBytes, name, "in")
ch <- prometheus.MustNewConstMetric(vtsBytesDesc, ch <- prometheus.MustNewConstMetric(p.data.bytes,
prometheus.CounterValue, float64(zone.OutBytes), name, "out") prometheus.CounterValue, zone.OutBytes, name, "out")
} }
for serverZone, countries := range nginxMetrics.FilterZones { for serverZone, countries := range nginxMetrics.FilterZones {
for country, zone := range countries { for country, zone := range countries {
reflectMetrics(&zone.Responses, vtsFilterZoneResponseDesc, ch, serverZone, country) reflectMetrics(&zone.Responses, p.data.filterZoneResponse, ch, serverZone, country)
reflectMetrics(&zone.Cache, vtsFilterZoneCacheDesc, ch, serverZone, country) reflectMetrics(&zone.Cache, p.data.filterZoneCache, ch, serverZone, country)
ch <- prometheus.MustNewConstMetric(vtsFilterZoneBytesDesc, ch <- prometheus.MustNewConstMetric(p.data.filterZoneBytes,
prometheus.CounterValue, float64(zone.InBytes), serverZone, country, "in") prometheus.CounterValue, float64(zone.InBytes), serverZone, country, "in")
ch <- prometheus.MustNewConstMetric(vtsFilterZoneBytesDesc, ch <- prometheus.MustNewConstMetric(p.data.filterZoneBytes,
prometheus.CounterValue, float64(zone.OutBytes), serverZone, country, "out") prometheus.CounterValue, float64(zone.OutBytes), serverZone, country, "out")
} }
} }
@ -229,9 +261,9 @@ func reflectMetrics(value interface{}, desc *prometheus.Desc, ch chan<- promethe
for i := 0; i < val.NumField(); i++ { for i := 0; i < val.NumField(); i++ {
tag := val.Type().Field(i).Tag tag := val.Type().Field(i).Tag
labels := append(labels, tag.Get("json")) l := append(labels, tag.Get("json"))
ch <- prometheus.MustNewConstMetric(desc, ch <- prometheus.MustNewConstMetric(desc,
prometheus.CounterValue, float64(val.Field(i).Interface().(float64)), prometheus.CounterValue, float64(val.Field(i).Interface().(float64)),
labels...) l...)
} }
} }

View file

@ -240,6 +240,8 @@ http {
{{ if not (empty $authPath) }} {{ if not (empty $authPath) }}
location = {{ $authPath }} { location = {{ $authPath }} {
internal; internal;
set $proxy_upstream_name "internal";
{{ if not $location.ExternalAuth.SendBody }} {{ if not $location.ExternalAuth.SendBody }}
proxy_pass_request_body off; proxy_pass_request_body off;
proxy_set_header Content-Length ""; proxy_set_header Content-Length "";
@ -402,6 +404,8 @@ http {
} }
location /nginx_status { location /nginx_status {
set $proxy_upstream_name "internal";
{{ if $cfg.EnableVtsStatus }} {{ if $cfg.EnableVtsStatus }}
vhost_traffic_status_display; vhost_traffic_status_display;
vhost_traffic_status_display_format html; vhost_traffic_status_display_format html;
@ -415,6 +419,8 @@ http {
# using prometheus. # using prometheus.
# TODO: enable extraction for vts module. # TODO: enable extraction for vts module.
location /internal_nginx_status { location /internal_nginx_status {
set $proxy_upstream_name "internal";
allow 127.0.0.1; allow 127.0.0.1;
{{ if not $cfg.DisableIpv6 }}allow ::1;{{ end }} {{ if not $cfg.DisableIpv6 }}allow ::1;{{ end }}
deny all; deny all;