-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
41 lines (32 loc) · 985 Bytes
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type HttpServer struct {
mux *http.ServeMux
}
func NewHttpServer() *HttpServer {
s := &HttpServer{
mux: http.NewServeMux(),
}
s.mux.HandleFunc("/metrics", promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}).ServeHTTP)
s.mux.HandleFunc("/scrape", s.ScrapeHandler)
return s
}
func (s *HttpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.mux.ServeHTTP(w, r)
}
//https://github.com/oliver006/redis_exporter
func (s *HttpServer) ScrapeHandler(w http.ResponseWriter, r *http.Request) {
target := r.URL.Query().Get("target")
if target == "" {
http.Error(w, "'target' parameter must be specified", 400)
return
}
registry := prometheus.NewRegistry()
exporter := NewExporter(target)
registry.MustRegister(exporter)
promhttp.HandlerFor(registry, promhttp.HandlerOpts{}).ServeHTTP(w, r)
}