Skip to content

Commit

Permalink
Ability to scrape dynamic targets
Browse files Browse the repository at this point in the history
Signed-off-by: Wimo <wimo@wimodya-athukorala.com>
  • Loading branch information
Wimo committed Mar 1, 2021
1 parent f26ca65 commit 5ac5e44
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cmd/postgres_exporter/postgres_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,18 @@ func contains(a []string, x string) bool {
return false
}

func setupExporter(dsn string, opts ...ExporterOpt) *prometheus.Registry {
exporter := NewExporter([]string{dsn}, opts...)
defer func() {
exporter.servers.Close()
}()

registry := prometheus.NewRegistry()
registry.MustRegister(version.NewCollector("postgres_exporter"))
registry.MustRegister(exporter)
return registry
}

func main() {
kingpin.Version(fmt.Sprintf("postgres_exporter %s (built with %s)\n", Version, runtime.Version()))
log.AddFlags(kingpin.CommandLine)
Expand Down Expand Up @@ -1841,6 +1853,23 @@ func main() {

prometheus.MustRegister(exporter)

registries := make(map[string]*prometheus.Registry)
http.HandleFunc("/probe", func(w http.ResponseWriter, r *http.Request) {
target := r.URL.Query().Get("target")
if target == "" {
http.Error(w, "'target' parameter must be specified", 400)
return
}

_, target_registered := registries[target]
if !target_registered {
log.Infof("Registering %s", target)
registries[target] = setupExporter(target, opts...)
}

promhttp.HandlerFor(registries[target], promhttp.HandlerOpts{}).ServeHTTP(w, r)
})

http.Handle(*metricPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=UTF-8") // nolint: errcheck
Expand Down

0 comments on commit 5ac5e44

Please sign in to comment.