Skip to content

Commit

Permalink
Ability to scrape dynamic targets
Browse files Browse the repository at this point in the history
  • Loading branch information
Wimo committed Jul 21, 2021
1 parent 57719ba commit 931fd96
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cmd/postgres_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package main

import (
"context"
"net/http"
"os"

Expand Down Expand Up @@ -115,6 +116,12 @@ func main() {
prometheus.MustRegister(exporter)

http.Handle(*metricPath, promhttp.Handler())

registries := make(map[string]*prometheus.Registry)
http.HandleFunc("/probe", func(w http.ResponseWriter, req *http.Request) {
probeHandler(w, req, logger, registries, opts...)
})

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=UTF-8") // nolint: errcheck
w.Write(landingPage) // nolint: errcheck
Expand All @@ -127,3 +134,29 @@ func main() {
os.Exit(1)
}
}

func probeHandler(w http.ResponseWriter, r *http.Request, logger log.Logger, registries map[string]*prometheus.Registry, opts ...ExporterOpt) {

ctx, cancel := context.WithCancel(r.Context())
defer cancel()
r = r.WithContext(ctx)

target := r.URL.Query().Get("target")
if target == "" {
http.Error(w, "Target parameter is missing", http.StatusBadRequest)
return
}

registry, target_registered := registries[target]
if !target_registered {
registry = prometheus.NewPedanticRegistry()
exporter := NewExporter([]string{target}, opts...)
registry.MustRegister(version.NewCollector(exporterName))
registry.MustRegister(exporter)
registries[target] = registry
}

h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)

}

0 comments on commit 931fd96

Please sign in to comment.