mirror of
https://github.com/prometheus-community/windows_exporter
synced 2024-12-25 16:02:04 +00:00
0711268d3c
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
78 lines
2.4 KiB
Go
78 lines
2.4 KiB
Go
//go:build windows
|
|
|
|
package collector
|
|
|
|
import (
|
|
"fmt"
|
|
stdlog "log"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/go-kit/log"
|
|
"github.com/go-kit/log/level"
|
|
"github.com/prometheus-community/windows_exporter/pkg/types"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/collectors"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
"github.com/prometheus/common/version"
|
|
)
|
|
|
|
func (c *Collectors) BuildServeHTTP(disableExporterMetrics bool, timeoutMargin float64) http.HandlerFunc {
|
|
collectorFactory := func(timeout time.Duration, requestedCollectors []string) (error, *Prometheus) {
|
|
filteredCollectors := make(map[string]types.Collector)
|
|
// scrape all enabled collectors if no collector is requested
|
|
if len(requestedCollectors) == 0 {
|
|
filteredCollectors = c.collectors
|
|
}
|
|
for _, name := range requestedCollectors {
|
|
col, exists := c.collectors[name]
|
|
if !exists {
|
|
return fmt.Errorf("unavailable collector: %s", name), nil
|
|
}
|
|
filteredCollectors[name] = col
|
|
}
|
|
return nil, NewPrometheus(timeout, c, c.logger)
|
|
}
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
const defaultTimeout = 10.0
|
|
|
|
var timeoutSeconds float64
|
|
if v := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds"); v != "" {
|
|
var err error
|
|
timeoutSeconds, err = strconv.ParseFloat(v, 64)
|
|
if err != nil {
|
|
_ = level.Warn(c.logger).Log("msg", fmt.Sprintf("Couldn't parse X-Prometheus-Scrape-Timeout-Seconds: %q. Defaulting timeout to %f", v, defaultTimeout))
|
|
}
|
|
}
|
|
if timeoutSeconds == 0 {
|
|
timeoutSeconds = defaultTimeout
|
|
}
|
|
timeoutSeconds = timeoutSeconds - timeoutMargin
|
|
|
|
reg := prometheus.NewRegistry()
|
|
err, wc := collectorFactory(time.Duration(timeoutSeconds*float64(time.Second)), r.URL.Query()["collect[]"])
|
|
if err != nil {
|
|
_ = level.Warn(c.logger).Log("msg", "Couldn't create filtered metrics handler", "err", err)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
w.Write([]byte(fmt.Sprintf("Couldn't create filtered metrics handler: %s", err))) //nolint:errcheck
|
|
return
|
|
}
|
|
|
|
reg.MustRegister(wc)
|
|
if !disableExporterMetrics {
|
|
reg.MustRegister(
|
|
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
|
|
collectors.NewGoCollector(),
|
|
version.NewCollector("windows_exporter"),
|
|
)
|
|
}
|
|
|
|
h := promhttp.HandlerFor(reg, promhttp.HandlerOpts{
|
|
ErrorLog: stdlog.New(log.NewStdlibAdapter(level.Error(c.logger)), "", stdlog.Lshortfile),
|
|
})
|
|
h.ServeHTTP(w, r)
|
|
}
|
|
}
|