aboutsummaryrefslogtreecommitdiff
path: root/node_exporter.go
diff options
context:
space:
mode:
authorSiavash Safi <siavash.safi@gmail.com>2017-10-14 14:23:42 +0200
committerBen Kochie <superq@gmail.com>2017-10-14 14:23:42 +0200
commitf3a70226025bf81e55eaf9d7386f9f7b3a267694 (patch)
tree1a721e7563babb1980089a31c2bf6557ab678f8e /node_exporter.go
parent8f9edf87b5e826fa6a4dc43883d2cb6311d2e1ee (diff)
downloadprometheus_node_collector-f3a70226025bf81e55eaf9d7386f9f7b3a267694.tar.bz2
prometheus_node_collector-f3a70226025bf81e55eaf9d7386f9f7b3a267694.tar.xz
prometheus_node_collector-f3a70226025bf81e55eaf9d7386f9f7b3a267694.zip
Add `collect[]` parameter (#699)
* Add `collect[]` parameter * Add TODo comment about staticcheck ignored * Restore promhttp.HandlerOpts * Log a warning and return HTTP error instead of failing * Check collector existence and status, cleanups * Fix warnings and error messages * Don't panic, return error if collector registration failed * Update README
Diffstat (limited to 'node_exporter.go')
-rw-r--r--node_exporter.go49
1 files changed, 38 insertions, 11 deletions
diff --git a/node_exporter.go b/node_exporter.go
index 1077188..7c1958e 100644
--- a/node_exporter.go
+++ b/node_exporter.go
@@ -14,6 +14,7 @@
14package main 14package main
15 15
16import ( 16import (
17 "fmt"
17 "net/http" 18 "net/http"
18 _ "net/http/pprof" 19 _ "net/http/pprof"
19 20
@@ -29,6 +30,40 @@ func init() {
29 prometheus.MustRegister(version.NewCollector("node_exporter")) 30 prometheus.MustRegister(version.NewCollector("node_exporter"))
30} 31}
31 32
33func handler(w http.ResponseWriter, r *http.Request) {
34 filters := r.URL.Query()["collect[]"]
35 log.Debugln("collect query:", filters)
36
37 nc, err := collector.NewNodeCollector(filters...)
38 if err != nil {
39 log.Warnln("Couldn't create", err)
40 w.WriteHeader(http.StatusBadRequest)
41 w.Write([]byte(fmt.Sprintf("Couldn't create %s", err)))
42 return
43 }
44
45 registry := prometheus.NewRegistry()
46 err = registry.Register(nc)
47 if err != nil {
48 log.Errorln("Couldn't register collector:", err)
49 w.WriteHeader(http.StatusInternalServerError)
50 w.Write([]byte(fmt.Sprintf("Couldn't register collector: %s", err)))
51 return
52 }
53
54 gatherers := prometheus.Gatherers{
55 prometheus.DefaultGatherer,
56 registry,
57 }
58 // Delegate http serving to Prometheus client library, which will call collector.Collect.
59 h := promhttp.HandlerFor(gatherers,
60 promhttp.HandlerOpts{
61 ErrorLog: log.NewErrorLogger(),
62 ErrorHandling: promhttp.ContinueOnError,
63 })
64 h.ServeHTTP(w, r)
65}
66
32func main() { 67func main() {
33 var ( 68 var (
34 listenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface.").Default(":9100").String() 69 listenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface.").Default(":9100").String()
@@ -43,6 +78,7 @@ func main() {
43 log.Infoln("Starting node_exporter", version.Info()) 78 log.Infoln("Starting node_exporter", version.Info())
44 log.Infoln("Build context", version.BuildContext()) 79 log.Infoln("Build context", version.BuildContext())
45 80
81 // This instance is only used to check collector creation and logging.
46 nc, err := collector.NewNodeCollector() 82 nc, err := collector.NewNodeCollector()
47 if err != nil { 83 if err != nil {
48 log.Fatalf("Couldn't create collector: %s", err) 84 log.Fatalf("Couldn't create collector: %s", err)
@@ -52,17 +88,8 @@ func main() {
52 log.Infof(" - %s", n) 88 log.Infof(" - %s", n)
53 } 89 }
54 90
55 if err := prometheus.Register(nc); err != nil { 91 // TODO(ts): Remove deprecated and problematic InstrumentHandlerFunc usage.
56 log.Fatalf("Couldn't register collector: %s", err) 92 http.HandleFunc(*metricsPath, prometheus.InstrumentHandlerFunc("prometheus", handler))
57 }
58 handler := promhttp.HandlerFor(prometheus.DefaultGatherer,
59 promhttp.HandlerOpts{
60 ErrorLog: log.NewErrorLogger(),
61 ErrorHandling: promhttp.ContinueOnError,
62 })
63
64 // TODO(ts): Remove deprecated and problematic InstrumentHandler usage.
65 http.Handle(*metricsPath, prometheus.InstrumentHandler("prometheus", handler))
66 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 93 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
67 w.Write([]byte(`<html> 94 w.Write([]byte(`<html>
68 <head><title>Node Exporter</title></head> 95 <head><title>Node Exporter</title></head>