Migrated everything to new client_golang.

This commit is contained in:
Bjoern Rabenstein 2014-06-26 19:20:36 +02:00
commit 0563ecd29d
11 changed files with 388 additions and 246 deletions

View file

@ -17,10 +17,11 @@ import (
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/exp"
"github.com/prometheus/node_exporter/collector"
)
const subsystem = "exporter"
var (
configFile = flag.String("config", "node_exporter.conf", "config file.")
memProfile = flag.String("memprofile", "", "write memory profile to this file")
@ -28,8 +29,27 @@ var (
enabledCollectors = flag.String("enabledCollectors", "attributes,diskstats,filesystem,loadavg,meminfo,stat,netdev", "comma-seperated list of collectors to use")
printCollectors = flag.Bool("printCollectors", false, "If true, print available collectors and exit")
interval = flag.Duration("interval", 60*time.Second, "refresh interval")
scrapeDurations = prometheus.NewDefaultHistogram()
metricsUpdated = prometheus.NewGauge()
collectorLabelNames = []string{"collector", "result"}
scrapeDurations = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: collector.Namespace,
Subsystem: subsystem,
Name: "scrape_duration_seconds",
Help: "node_exporter: Duration of a scrape job.",
},
collectorLabelNames,
)
metricsUpdated = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: collector.Namespace,
Subsystem: subsystem,
Name: "metrics_updated",
Help: "node_exporter: Number of metrics updated.",
},
collectorLabelNames,
)
)
func main() {
@ -41,14 +61,13 @@ func main() {
}
return
}
registry := prometheus.NewRegistry()
collectors, err := loadCollectors(*configFile, registry)
collectors, err := loadCollectors(*configFile)
if err != nil {
log.Fatalf("Couldn't load config and collectors: %s", err)
}
registry.Register("node_exporter_scrape_duration_seconds", "node_exporter: Duration of a scrape job.", prometheus.NilLabels, scrapeDurations)
registry.Register("node_exporter_metrics_updated", "node_exporter: Number of metrics updated.", prometheus.NilLabels, metricsUpdated)
prometheus.MustRegister(scrapeDurations)
prometheus.MustRegister(metricsUpdated)
glog.Infof("Enabled collectors:")
for n, _ := range collectors {
@ -60,7 +79,7 @@ func main() {
signal.Notify(sigHup, syscall.SIGHUP)
signal.Notify(sigUsr1, syscall.SIGUSR1)
go serveStatus(registry)
go serveStatus()
glog.Infof("Starting initial collection")
collect(collectors)
@ -69,7 +88,7 @@ func main() {
for {
select {
case <-sigHup:
collectors, err = loadCollectors(*configFile, registry)
collectors, err = loadCollectors(*configFile)
if err != nil {
log.Fatalf("Couldn't load config and collectors: %s", err)
}
@ -96,7 +115,7 @@ func main() {
}
func loadCollectors(file string, registry prometheus.Registry) (map[string]collector.Collector, error) {
func loadCollectors(file string) (map[string]collector.Collector, error) {
collectors := map[string]collector.Collector{}
config, err := getConfig(file)
if err != nil {
@ -107,7 +126,7 @@ func loadCollectors(file string, registry prometheus.Registry) (map[string]colle
if !ok {
log.Fatalf("Collector '%s' not available", name)
}
c, err := fn(*config, registry)
c, err := fn(*config)
if err != nil {
return nil, err
}
@ -126,9 +145,9 @@ func getConfig(file string) (*collector.Config, error) {
return config, json.Unmarshal(bytes, &config)
}
func serveStatus(registry prometheus.Registry) {
exp.Handle(prometheus.ExpositionResource, registry.Handler())
http.ListenAndServe(*listeningAddress, exp.DefaultCoarseMux)
func serveStatus() {
http.Handle("/metrics", prometheus.Handler())
http.ListenAndServe(*listeningAddress, nil)
}
func collect(collectors map[string]collector.Collector) {
@ -147,17 +166,15 @@ func Execute(name string, c collector.Collector) {
begin := time.Now()
updates, err := c.Update()
duration := time.Since(begin)
var result string
label := map[string]string{
"collector": name,
}
if err != nil {
glog.Infof("ERROR: %s failed after %fs: %s", name, duration.Seconds(), err)
label["result"] = "error"
result = "error"
} else {
glog.Infof("OK: %s success after %fs.", name, duration.Seconds())
label["result"] = "success"
result = "success"
}
scrapeDurations.Add(label, duration.Seconds())
metricsUpdated.Set(label, float64(updates))
scrapeDurations.WithLabelValues(name, result).Observe(duration.Seconds())
metricsUpdated.WithLabelValues(name, result).Set(float64(updates))
}