Divide stats collector in sub-collectors.

In order to avoid timeouts in case of many clients,
it should be possible to configure which stats should
be collected.

Implements the following sub-collectors:
 * topics
 * channels
 * clients
This commit is contained in:
Thomas Nitsche
2015-12-21 18:13:03 +01:00
parent 37b0428070
commit 470c5a0ae2
6 changed files with 242 additions and 196 deletions

86
main.go
View File

@@ -3,14 +3,14 @@ package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/url"
"strings"
"github.com/lovoo/nsq_exporter/collector"
"github.com/tsne/nsq_exporter/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
// Version of nsq_exporter. Set at build time.
@@ -19,12 +19,19 @@ const Version = "0.0.0.dev"
var (
listenAddress = flag.String("web.listen", ":9117", "Address on which to expose metrics and web interface.")
metricsPath = flag.String("web.path", "/metrics", "Path under which to expose metrics.")
nsqUrl = flag.String("nsq.addr", "http://localhost:4151/stats", "Address of the NSQ host.")
enabledCollectors = flag.String("collectors", "nsqstats", "Comma-separated list of collectors to use.")
nsqdURL = flag.String("nsqd.addr", "http://localhost:4151/stats", "Address of the nsqd node.")
enabledCollectors = flag.String("collect", "stats.topics,stats.channels", "Comma-separated list of collectors to use.")
namespace = flag.String("namespace", "nsq", "Namespace for the NSQ metrics.")
collectorRegistry = map[string]func(name string, x *collector.NsqExecutor) error{
"nsqstats": addStatsCollector,
collectorRegistry = map[string]func(names []string) (collector.Collector, error){
"stats": createNsqdStats,
}
// stats.* collectors
statsRegistry = map[string]func(namespace string) collector.StatsCollector{
"topics": collector.TopicsCollector,
"channels": collector.ChannelsCollector,
"clients": collector.ClientsCollector,
}
)
@@ -37,11 +44,8 @@ func main() {
}
prometheus.MustRegister(ex)
handler := prometheus.Handler()
if *metricsPath == "" || *metricsPath == "/" {
http.Handle(*metricsPath, handler)
} else {
http.Handle(*metricsPath, handler)
http.Handle(*metricsPath, prometheus.Handler())
if *metricsPath != "" && *metricsPath != "/" {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>NSQ Exporter</title></head>
@@ -53,6 +57,7 @@ func main() {
})
}
log.Print("listening to ", *listenAddress)
err = http.ListenAndServe(*listenAddress, nil)
if err != nil {
log.Fatal(err)
@@ -60,41 +65,62 @@ func main() {
}
func createNsqExecutor() (*collector.NsqExecutor, error) {
ex := collector.NewNsqExecutor(*namespace)
collectors := make(map[string][]string)
for _, name := range strings.Split(*enabledCollectors, ",") {
name = strings.TrimSpace(name)
addCollector, has := collectorRegistry[name]
parts := strings.SplitN(name, ".", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid collector name: %s", name)
}
collectors[parts[0]] = append(collectors[parts[0]], parts[1])
}
ex := collector.NewNsqExecutor(*namespace)
for collector, subcollectors := range collectors {
newCollector, has := collectorRegistry[collector]
if !has {
return nil, fmt.Errorf("unknown collector: %s", name)
return nil, fmt.Errorf("invalid collector: %s", collector)
}
if err := addCollector(name, ex); err != nil {
c, err := newCollector(subcollectors)
if err != nil {
return nil, err
}
ex.AddCollector(collector, c)
}
return ex, nil
}
func addStatsCollector(name string, ex *collector.NsqExecutor) error {
u, err := url.Parse(normalizeURL(*nsqUrl))
func createNsqdStats(statsCollectors []string) (collector.Collector, error) {
nsqdURL, err := normalizeURL(*nsqdURL)
if err != nil {
return err
return nil, err
}
if u.Scheme == "" {
u.Scheme = "http"
stats := collector.NewNsqdStats(*namespace, nsqdURL)
for _, c := range statsCollectors {
newStatsCollector, has := statsRegistry[c]
if !has {
return nil, fmt.Errorf("unknown stats collector: %s", c)
}
stats.Use(newStatsCollector(*namespace))
}
return stats, nil
}
func normalizeURL(ustr string) (string, error) {
ustr = strings.ToLower(ustr)
if !strings.HasPrefix(ustr, "https://") && !strings.HasPrefix(ustr, "http://") {
ustr = "http://" + ustr
}
u, err := url.Parse(ustr)
if err != nil {
return "", err
}
if u.Path == "" {
u.Path = "/stats"
}
u.RawQuery = "format=json"
ex.AddCollector(name, collector.NewStatsCollector(u.String()))
return nil
}
func normalizeURL(u string) string {
u = strings.ToLower(u)
if !strings.HasPrefix(u, "https://") && !strings.HasPrefix(u, "http://") {
return "http://" + u
}
return u
return u.String(), nil
}