/
main.go
62 lines (51 loc) · 1.23 KB
/
main.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"github.com/gorilla/mux"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
SCHEME string = "http"
HOSTNAME string = "127.0.0.1"
PORT int = 2112
indexPage string = ""
)
// Record metrics example from prometheus goclient docs
func recordMetrics() {
go func() {
for {
opsProcessed.Inc()
time.Sleep(2 * time.Second)
}
}()
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, indexPage)
}
var (
opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
Name: "myapp_processed_ops_total",
Help: "The total number of processed events",
})
)
func main() {
recordMetrics()
r := mux.NewRouter()
r.Handle("/metrics", promhttp.Handler())
r.HandleFunc("/", indexHandler)
r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
t, err := route.GetPathTemplate()
if err != nil {
return err
}
if t != "/" {
indexPage += fmt.Sprintf(`<p><a href="%s://%s:%d%v">%v</a></p>`, SCHEME, HOSTNAME, PORT, t, t) + "\n"
}
return nil
})
http.ListenAndServe(fmt.Sprintf(":%d", PORT), r)
}