mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-21 20:26:36 +00:00
chore: pass context aware logger to collectors (#1582)
This commit is contained in:
@@ -11,13 +11,14 @@ import (
|
||||
|
||||
"github.com/go-kit/log"
|
||||
"github.com/go-kit/log/level"
|
||||
"github.com/google/uuid"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||
"github.com/prometheus/client_golang/prometheus/collectors/version"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
func (c *Collectors) BuildServeHTTP(disableExporterMetrics bool, timeoutMargin float64) http.HandlerFunc {
|
||||
func (c *Collectors) BuildServeHTTP(logger log.Logger, disableExporterMetrics bool, timeoutMargin float64) http.HandlerFunc {
|
||||
collectorFactory := func(timeout time.Duration, requestedCollectors []string) (error, *Prometheus) {
|
||||
filteredCollectors := make(map[string]Collector)
|
||||
// scrape all enabled collectors if no collector is requested
|
||||
@@ -33,15 +34,16 @@ func (c *Collectors) BuildServeHTTP(disableExporterMetrics bool, timeoutMargin f
|
||||
}
|
||||
|
||||
filtered := Collectors{
|
||||
logger: c.logger,
|
||||
collectors: filteredCollectors,
|
||||
perfCounterQuery: c.perfCounterQuery,
|
||||
}
|
||||
|
||||
return nil, NewPrometheus(timeout, &filtered, c.logger)
|
||||
return nil, NewPrometheus(timeout, &filtered, logger)
|
||||
}
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
logger := log.With(logger, "remote", r.RemoteAddr, "correlation_id", uuid.New().String())
|
||||
|
||||
const defaultTimeout = 10.0
|
||||
|
||||
var timeoutSeconds float64
|
||||
@@ -49,7 +51,7 @@ func (c *Collectors) BuildServeHTTP(disableExporterMetrics bool, timeoutMargin f
|
||||
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))
|
||||
_ = level.Warn(logger).Log("msg", fmt.Sprintf("Couldn't parse X-Prometheus-Scrape-Timeout-Seconds: %q. Defaulting timeout to %f", v, defaultTimeout))
|
||||
}
|
||||
}
|
||||
if timeoutSeconds == 0 {
|
||||
@@ -60,7 +62,7 @@ func (c *Collectors) BuildServeHTTP(disableExporterMetrics bool, timeoutMargin f
|
||||
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)
|
||||
_ = level.Warn(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
|
||||
@@ -76,7 +78,7 @@ func (c *Collectors) BuildServeHTTP(disableExporterMetrics bool, timeoutMargin f
|
||||
}
|
||||
|
||||
h := promhttp.HandlerFor(reg, promhttp.HandlerOpts{
|
||||
ErrorLog: stdlog.New(log.NewStdlibAdapter(level.Error(c.logger)), "", stdlog.Lshortfile),
|
||||
ErrorLog: stdlog.New(log.NewStdlibAdapter(level.Error(logger)), "", stdlog.Lshortfile),
|
||||
})
|
||||
h.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user