mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-25 06:06:35 +00:00
Add URL filter for collectors
Signed-off-by: Björn Fischer <bfischer@inovex.de>
This commit is contained in:
29
exporter.go
29
exporter.go
@@ -339,9 +339,21 @@ func main() {
|
|||||||
|
|
||||||
h := &metricsHandler{
|
h := &metricsHandler{
|
||||||
timeoutMargin: *timeoutMargin,
|
timeoutMargin: *timeoutMargin,
|
||||||
collectorFactory: func(timeout time.Duration) *windowsCollector {
|
collectorFactory: func(timeout time.Duration, requestedCollectors []string) (error, *windowsCollector) {
|
||||||
return &windowsCollector{
|
filteredCollectors := make(map[string]collector.Collector)
|
||||||
collectors: collectors,
|
// scrape all enabled collectors if no collector is requested
|
||||||
|
if len(requestedCollectors) == 0 {
|
||||||
|
filteredCollectors = collectors
|
||||||
|
}
|
||||||
|
for _, name := range requestedCollectors {
|
||||||
|
col, exists := collectors[name]
|
||||||
|
if !exists {
|
||||||
|
return fmt.Errorf("unavailable collector: %s", name), nil
|
||||||
|
}
|
||||||
|
filteredCollectors[name] = col
|
||||||
|
}
|
||||||
|
return nil, &windowsCollector{
|
||||||
|
collectors: filteredCollectors,
|
||||||
maxScrapeDuration: timeout,
|
maxScrapeDuration: timeout,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -455,7 +467,7 @@ loop:
|
|||||||
|
|
||||||
type metricsHandler struct {
|
type metricsHandler struct {
|
||||||
timeoutMargin float64
|
timeoutMargin float64
|
||||||
collectorFactory func(timeout time.Duration) *windowsCollector
|
collectorFactory func(timeout time.Duration, requestedCollectors []string) (error, *windowsCollector)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mh *metricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (mh *metricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -475,7 +487,14 @@ func (mh *metricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
timeoutSeconds = timeoutSeconds - mh.timeoutMargin
|
timeoutSeconds = timeoutSeconds - mh.timeoutMargin
|
||||||
|
|
||||||
reg := prometheus.NewRegistry()
|
reg := prometheus.NewRegistry()
|
||||||
reg.MustRegister(mh.collectorFactory(time.Duration(timeoutSeconds * float64(time.Second))))
|
err, wc := mh.collectorFactory(time.Duration(timeoutSeconds*float64(time.Second)), r.URL.Query()["collect[]"])
|
||||||
|
if err != nil {
|
||||||
|
log.Warnln("Couldn't create filtered metrics handler: ", err)
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
w.Write([]byte(fmt.Sprintf("Couldn't create filtered metrics handler: %s", err)))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reg.MustRegister(wc)
|
||||||
reg.MustRegister(
|
reg.MustRegister(
|
||||||
prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}),
|
prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}),
|
||||||
prometheus.NewGoCollector(),
|
prometheus.NewGoCollector(),
|
||||||
|
|||||||
Reference in New Issue
Block a user