2023-11-04 19:51:35 +00:00
|
|
|
//go:build windows
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
stdlog "log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2024-08-24 17:14:38 +00:00
|
|
|
"github.com/google/uuid"
|
2023-11-04 19:51:35 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/collectors"
|
2024-04-09 19:59:14 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/collectors/version"
|
2023-11-04 19:51:35 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
)
|
|
|
|
|
2024-08-24 17:14:38 +00:00
|
|
|
func (c *Collectors) BuildServeHTTP(logger log.Logger, disableExporterMetrics bool, timeoutMargin float64) http.HandlerFunc {
|
2023-11-04 19:51:35 +00:00
|
|
|
collectorFactory := func(timeout time.Duration, requestedCollectors []string) (error, *Prometheus) {
|
2024-08-05 13:50:41 +00:00
|
|
|
filteredCollectors := make(map[string]Collector)
|
2023-11-04 19:51:35 +00:00
|
|
|
// 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
|
|
|
|
}
|
2024-01-21 22:11:26 +00:00
|
|
|
|
|
|
|
filtered := Collectors{
|
|
|
|
collectors: filteredCollectors,
|
|
|
|
perfCounterQuery: c.perfCounterQuery,
|
|
|
|
}
|
|
|
|
|
2024-08-24 17:14:38 +00:00
|
|
|
return nil, NewPrometheus(timeout, &filtered, logger)
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-08-24 17:14:38 +00:00
|
|
|
logger := log.With(logger, "remote", r.RemoteAddr, "correlation_id", uuid.New().String())
|
|
|
|
|
2023-11-04 19:51:35 +00:00
|
|
|
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 {
|
2024-08-24 17:14:38 +00:00
|
|
|
_ = level.Warn(logger).Log("msg", fmt.Sprintf("Couldn't parse X-Prometheus-Scrape-Timeout-Seconds: %q. Defaulting timeout to %f", v, defaultTimeout))
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if timeoutSeconds == 0 {
|
|
|
|
timeoutSeconds = defaultTimeout
|
|
|
|
}
|
2024-08-10 20:05:33 +00:00
|
|
|
timeoutSeconds -= timeoutMargin
|
2023-11-04 19:51:35 +00:00
|
|
|
|
|
|
|
reg := prometheus.NewRegistry()
|
|
|
|
err, wc := collectorFactory(time.Duration(timeoutSeconds*float64(time.Second)), r.URL.Query()["collect[]"])
|
|
|
|
if err != nil {
|
2024-08-24 17:14:38 +00:00
|
|
|
_ = level.Warn(logger).Log("msg", "Couldn't create filtered metrics handler", "err", err)
|
2023-11-04 19:51:35 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2024-08-29 22:26:15 +00:00
|
|
|
_, _ = w.Write([]byte(fmt.Sprintf("Couldn't create filtered metrics handler: %s", err)))
|
2023-11-04 19:51:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reg.MustRegister(wc)
|
|
|
|
if !disableExporterMetrics {
|
|
|
|
reg.MustRegister(
|
|
|
|
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
|
|
|
|
collectors.NewGoCollector(),
|
|
|
|
version.NewCollector("windows_exporter"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
h := promhttp.HandlerFor(reg, promhttp.HandlerOpts{
|
2024-08-24 17:14:38 +00:00
|
|
|
ErrorLog: stdlog.New(log.NewStdlibAdapter(level.Error(logger)), "", stdlog.Lshortfile),
|
2023-11-04 19:51:35 +00:00
|
|
|
})
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
}
|