-
Notifications
You must be signed in to change notification settings - Fork 1
/
prom.go
41 lines (33 loc) · 1.04 KB
/
prom.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 (
"fmt"
"net/http"
log "github.com/Sirupsen/logrus"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type promMessage struct {
gatherer prom.Gatherer
}
type promIO struct {
scrapeSignalChan chan bool
messageChan chan promMessage
}
func mflowPromHandler(pio *promIO) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Debug("Prometheus wants to scrape some metrics ...")
pio.scrapeSignalChan <- true
pmsg := <-pio.messageChan
promhttp.HandlerFor(pmsg.gatherer, promhttp.HandlerOpts{}).ServeHTTP(w, r)
}
}
func mflowStatusHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("cogito ergo sum\n"))
}
func exposePrometheusEndpoint(port int, pio *promIO) {
log.Debugf("Exposing a Prometheus endpoint at %d", port)
http.HandleFunc("/metrics", mflowPromHandler(pio))
http.HandleFunc("/status", mflowStatusHandler)
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}