MCPcopy
hub / github.com/prometheus/node_exporter / innerHandler

Method innerHandler

node_exporter.go:127–180  ·  view source on GitHub ↗

innerHandler is used to create both the one unfiltered http.Handler to be wrapped by the outer handler and also the filtered handlers created on the fly. The former is accomplished by calling innerHandler without any arguments (in which case it will log all the collectors enabled via command-line fl

(filters ...string)

Source from the content-addressed store, hash-verified

125// (in which case it will log all the collectors enabled via command-line
126// flags).
127func (h *handler) innerHandler(filters ...string) (http.Handler, error) {
128 nc, err := collector.NewNodeCollector(h.logger, filters...)
129 if err != nil {
130 return nil, fmt.Errorf("couldn't create collector: %s", err)
131 }
132
133 // Only log the creation of an unfiltered handler, which should happen
134 // only once upon startup.
135 if len(filters) == 0 {
136 h.logger.Info("Enabled collectors")
137 for n := range nc.Collectors {
138 h.enabledCollectors = append(h.enabledCollectors, n)
139 }
140 sort.Strings(h.enabledCollectors)
141 for _, c := range h.enabledCollectors {
142 h.logger.Info(c)
143 }
144 }
145
146 r := prometheus.NewRegistry()
147 r.MustRegister(versioncollector.NewCollector("node_exporter"))
148 if err := r.Register(nc); err != nil {
149 return nil, fmt.Errorf("couldn't register node collector: %s", err)
150 }
151
152 var handler http.Handler
153 if h.includeExporterMetrics {
154 handler = promhttp.HandlerFor(
155 prometheus.Gatherers{h.exporterMetricsRegistry, r},
156 promhttp.HandlerOpts{
157 ErrorLog: slog.NewLogLogger(h.logger.Handler(), slog.LevelError),
158 ErrorHandling: promhttp.ContinueOnError,
159 MaxRequestsInFlight: h.maxRequests,
160 Registry: h.exporterMetricsRegistry,
161 },
162 )
163 // Note that we have to use h.exporterMetricsRegistry here to
164 // use the same promhttp metrics for all expositions.
165 handler = promhttp.InstrumentMetricHandler(
166 h.exporterMetricsRegistry, handler,
167 )
168 } else {
169 handler = promhttp.HandlerFor(
170 r,
171 promhttp.HandlerOpts{
172 ErrorLog: slog.NewLogLogger(h.logger.Handler(), slog.LevelError),
173 ErrorHandling: promhttp.ContinueOnError,
174 MaxRequestsInFlight: h.maxRequests,
175 },
176 )
177 }
178
179 return handler, nil
180}
181
182func main() {
183 var (

Callers 2

newHandlerFunction · 0.95
ServeHTTPMethod · 0.95

Calls 1

NewNodeCollectorFunction · 0.92

Tested by

no test coverage detected