| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | |
| 6 | "github.com/prometheus/client_golang/prometheus" |
| 7 | "github.com/prometheus/client_golang/prometheus/promhttp" |
| 8 | ) |
| 9 | |
| 10 | type HttpServer struct { |
| 11 | mux *http.ServeMux |
| 12 | } |
| 13 | |
| 14 | func NewHttpServer() *HttpServer { |
| 15 | s := &HttpServer{ |
| 16 | mux: http.NewServeMux(), |
| 17 | } |
| 18 | s.mux.HandleFunc("/metrics", promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}).ServeHTTP) |
| 19 | |
| 20 | s.mux.HandleFunc("/scrape", s.ScrapeHandler) |
| 21 | return s |
| 22 | } |
| 23 | |
| 24 | func (s *HttpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 25 | s.mux.ServeHTTP(w, r) |
| 26 | } |
| 27 | |
| 28 | //https://github.com/oliver006/redis_exporter/blob/master/exporter.go |
| 29 | func (s *HttpServer) ScrapeHandler(w http.ResponseWriter, r *http.Request) { |
| 30 | target := r.URL.Query().Get("target") |
| 31 | if target == "" { |
| 32 | http.Error(w, "'target' parameter must be specified", 400) |
| 33 | //e.targetScrapeRequestErrors.Inc() |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | registry := prometheus.NewRegistry() |
| 38 | e := NewExporter(&ExporterTarget{ |
| 39 | Host: target, |
| 40 | }) |
| 41 | registry.MustRegister(e) |
| 42 | |
| 43 | promhttp.HandlerFor(registry, promhttp.HandlerOpts{}).ServeHTTP(w, r) |
| 44 | } |