diff options
author | Ben Ye <yb532204897@gmail.com> | 2019-12-31 11:19:37 -0500 |
---|---|---|
committer | Ben Kochie <superq@gmail.com> | 2019-12-31 17:19:37 +0100 |
commit | 2477c5c67dff7e7655a9d466450235e9c9eac193 (patch) | |
tree | 198cb44d48f454df765984bc614e1b1972a646e8 /node_exporter.go | |
parent | a80b7d0bc5ee93e704bab22e7592ed8b7d65899e (diff) | |
download | prometheus_node_collector-2477c5c67dff7e7655a9d466450235e9c9eac193.tar.bz2 prometheus_node_collector-2477c5c67dff7e7655a9d466450235e9c9eac193.tar.xz prometheus_node_collector-2477c5c67dff7e7655a9d466450235e9c9eac193.zip |
switch to go-kit/log (#1575)
Signed-off-by: yeya24 <yb532204897@gmail.com>
Diffstat (limited to 'node_exporter.go')
-rw-r--r-- | node_exporter.go | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/node_exporter.go b/node_exporter.go index b48170c..6e92cc3 100644 --- a/node_exporter.go +++ b/node_exporter.go | |||
@@ -15,13 +15,17 @@ package main | |||
15 | 15 | ||
16 | import ( | 16 | import ( |
17 | "fmt" | 17 | "fmt" |
18 | "github.com/prometheus/common/promlog" | ||
19 | "github.com/prometheus/common/promlog/flag" | ||
18 | "net/http" | 20 | "net/http" |
19 | _ "net/http/pprof" | 21 | _ "net/http/pprof" |
22 | "os" | ||
20 | "sort" | 23 | "sort" |
21 | 24 | ||
25 | "github.com/go-kit/kit/log" | ||
26 | "github.com/go-kit/kit/log/level" | ||
22 | "github.com/prometheus/client_golang/prometheus" | 27 | "github.com/prometheus/client_golang/prometheus" |
23 | "github.com/prometheus/client_golang/prometheus/promhttp" | 28 | "github.com/prometheus/client_golang/prometheus/promhttp" |
24 | "github.com/prometheus/common/log" | ||
25 | "github.com/prometheus/common/version" | 29 | "github.com/prometheus/common/version" |
26 | "github.com/prometheus/node_exporter/collector" | 30 | "github.com/prometheus/node_exporter/collector" |
27 | "github.com/prometheus/node_exporter/https" | 31 | "github.com/prometheus/node_exporter/https" |
@@ -38,13 +42,15 @@ type handler struct { | |||
38 | exporterMetricsRegistry *prometheus.Registry | 42 | exporterMetricsRegistry *prometheus.Registry |
39 | includeExporterMetrics bool | 43 | includeExporterMetrics bool |
40 | maxRequests int | 44 | maxRequests int |
45 | logger log.Logger | ||
41 | } | 46 | } |
42 | 47 | ||
43 | func newHandler(includeExporterMetrics bool, maxRequests int) *handler { | 48 | func newHandler(includeExporterMetrics bool, maxRequests int, logger log.Logger) *handler { |
44 | h := &handler{ | 49 | h := &handler{ |
45 | exporterMetricsRegistry: prometheus.NewRegistry(), | 50 | exporterMetricsRegistry: prometheus.NewRegistry(), |
46 | includeExporterMetrics: includeExporterMetrics, | 51 | includeExporterMetrics: includeExporterMetrics, |
47 | maxRequests: maxRequests, | 52 | maxRequests: maxRequests, |
53 | logger: logger, | ||
48 | } | 54 | } |
49 | if h.includeExporterMetrics { | 55 | if h.includeExporterMetrics { |
50 | h.exporterMetricsRegistry.MustRegister( | 56 | h.exporterMetricsRegistry.MustRegister( |
@@ -53,7 +59,7 @@ func newHandler(includeExporterMetrics bool, maxRequests int) *handler { | |||
53 | ) | 59 | ) |
54 | } | 60 | } |
55 | if innerHandler, err := h.innerHandler(); err != nil { | 61 | if innerHandler, err := h.innerHandler(); err != nil { |
56 | log.Fatalf("Couldn't create metrics handler: %s", err) | 62 | panic(fmt.Sprintf("Couldn't create metrics handler: %s", err)) |
57 | } else { | 63 | } else { |
58 | h.unfilteredHandler = innerHandler | 64 | h.unfilteredHandler = innerHandler |
59 | } | 65 | } |
@@ -63,7 +69,7 @@ func newHandler(includeExporterMetrics bool, maxRequests int) *handler { | |||
63 | // ServeHTTP implements http.Handler. | 69 | // ServeHTTP implements http.Handler. |
64 | func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | 70 | func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
65 | filters := r.URL.Query()["collect[]"] | 71 | filters := r.URL.Query()["collect[]"] |
66 | log.Debugln("collect query:", filters) | 72 | level.Debug(h.logger).Log("msg", "collect query:", "filters", filters) |
67 | 73 | ||
68 | if len(filters) == 0 { | 74 | if len(filters) == 0 { |
69 | // No filters, use the prepared unfiltered handler. | 75 | // No filters, use the prepared unfiltered handler. |
@@ -73,7 +79,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |||
73 | // To serve filtered metrics, we create a filtering handler on the fly. | 79 | // To serve filtered metrics, we create a filtering handler on the fly. |
74 | filteredHandler, err := h.innerHandler(filters...) | 80 | filteredHandler, err := h.innerHandler(filters...) |
75 | if err != nil { | 81 | if err != nil { |
76 | log.Warnln("Couldn't create filtered metrics handler:", err) | 82 | level.Warn(h.logger).Log("msg", "Couldn't create filtered metrics handler:", "err", err) |
77 | w.WriteHeader(http.StatusBadRequest) | 83 | w.WriteHeader(http.StatusBadRequest) |
78 | w.Write([]byte(fmt.Sprintf("Couldn't create filtered metrics handler: %s", err))) | 84 | w.Write([]byte(fmt.Sprintf("Couldn't create filtered metrics handler: %s", err))) |
79 | return | 85 | return |
@@ -81,13 +87,13 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |||
81 | filteredHandler.ServeHTTP(w, r) | 87 | filteredHandler.ServeHTTP(w, r) |
82 | } | 88 | } |
83 | 89 | ||
84 | // innerHandler is used to create buth the one unfiltered http.Handler to be | 90 | // innerHandler is used to create both the one unfiltered http.Handler to be |
85 | // wrapped by the outer handler and also the filtered handlers created on the | 91 | // wrapped by the outer handler and also the filtered handlers created on the |
86 | // fly. The former is accomplished by calling innerHandler without any arguments | 92 | // fly. The former is accomplished by calling innerHandler without any arguments |
87 | // (in which case it will log all the collectors enabled via command-line | 93 | // (in which case it will log all the collectors enabled via command-line |
88 | // flags). | 94 | // flags). |
89 | func (h *handler) innerHandler(filters ...string) (http.Handler, error) { | 95 | func (h *handler) innerHandler(filters ...string) (http.Handler, error) { |
90 | nc, err := collector.NewNodeCollector(filters...) | 96 | nc, err := collector.NewNodeCollector(h.logger, filters...) |
91 | if err != nil { | 97 | if err != nil { |
92 | return nil, fmt.Errorf("couldn't create collector: %s", err) | 98 | return nil, fmt.Errorf("couldn't create collector: %s", err) |
93 | } | 99 | } |
@@ -95,14 +101,14 @@ func (h *handler) innerHandler(filters ...string) (http.Handler, error) { | |||
95 | // Only log the creation of an unfiltered handler, which should happen | 101 | // Only log the creation of an unfiltered handler, which should happen |
96 | // only once upon startup. | 102 | // only once upon startup. |
97 | if len(filters) == 0 { | 103 | if len(filters) == 0 { |
98 | log.Infof("Enabled collectors:") | 104 | level.Info(h.logger).Log("msg", "Enabled collectors") |
99 | collectors := []string{} | 105 | collectors := []string{} |
100 | for n := range nc.Collectors { | 106 | for n := range nc.Collectors { |
101 | collectors = append(collectors, n) | 107 | collectors = append(collectors, n) |
102 | } | 108 | } |
103 | sort.Strings(collectors) | 109 | sort.Strings(collectors) |
104 | for _, n := range collectors { | 110 | for _, c := range collectors { |
105 | log.Infof(" - %s", n) | 111 | level.Info(h.logger).Log("collector", c) |
106 | } | 112 | } |
107 | } | 113 | } |
108 | 114 | ||
@@ -114,7 +120,6 @@ func (h *handler) innerHandler(filters ...string) (http.Handler, error) { | |||
114 | handler := promhttp.HandlerFor( | 120 | handler := promhttp.HandlerFor( |
115 | prometheus.Gatherers{h.exporterMetricsRegistry, r}, | 121 | prometheus.Gatherers{h.exporterMetricsRegistry, r}, |
116 | promhttp.HandlerOpts{ | 122 | promhttp.HandlerOpts{ |
117 | ErrorLog: log.NewErrorLogger(), | ||
118 | ErrorHandling: promhttp.ContinueOnError, | 123 | ErrorHandling: promhttp.ContinueOnError, |
119 | MaxRequestsInFlight: h.maxRequests, | 124 | MaxRequestsInFlight: h.maxRequests, |
120 | Registry: h.exporterMetricsRegistry, | 125 | Registry: h.exporterMetricsRegistry, |
@@ -154,15 +159,17 @@ func main() { | |||
154 | ).Default("").String() | 159 | ).Default("").String() |
155 | ) | 160 | ) |
156 | 161 | ||
157 | log.AddFlags(kingpin.CommandLine) | 162 | promlogConfig := &promlog.Config{} |
163 | flag.AddFlags(kingpin.CommandLine, promlogConfig) | ||
158 | kingpin.Version(version.Print("node_exporter")) | 164 | kingpin.Version(version.Print("node_exporter")) |
159 | kingpin.HelpFlag.Short('h') | 165 | kingpin.HelpFlag.Short('h') |
160 | kingpin.Parse() | 166 | kingpin.Parse() |
167 | logger := promlog.New(promlogConfig) | ||
161 | 168 | ||
162 | log.Infoln("Starting node_exporter", version.Info()) | 169 | level.Info(logger).Log("msg", "Starting node_exporter", "version", version.Info()) |
163 | log.Infoln("Build context", version.BuildContext()) | 170 | level.Info(logger).Log("msg", "Build context", "build_context", version.BuildContext()) |
164 | 171 | ||
165 | http.Handle(*metricsPath, newHandler(!*disableExporterMetrics, *maxRequests)) | 172 | http.Handle(*metricsPath, newHandler(!*disableExporterMetrics, *maxRequests, logger)) |
166 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | 173 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
167 | w.Write([]byte(`<html> | 174 | w.Write([]byte(`<html> |
168 | <head><title>Node Exporter</title></head> | 175 | <head><title>Node Exporter</title></head> |
@@ -173,9 +180,10 @@ func main() { | |||
173 | </html>`)) | 180 | </html>`)) |
174 | }) | 181 | }) |
175 | 182 | ||
176 | log.Infoln("Listening on", *listenAddress) | 183 | level.Info(logger).Log("msg", "Listening on", "address", *listenAddress) |
177 | server := &http.Server{Addr: *listenAddress} | 184 | server := &http.Server{Addr: *listenAddress} |
178 | if err := https.Listen(server, *configFile); err != nil { | 185 | if err := https.Listen(server, *configFile); err != nil { |
179 | log.Fatal(err) | 186 | level.Error(logger).Log("err", err) |
187 | os.Exit(1) | ||
180 | } | 188 | } |
181 | } | 189 | } |