forked from brancz/prometheus-example-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
49 lines (42 loc) · 1.35 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
package main
import (
"flag"
"log"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
version = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "version",
Help: "Version information about this binary",
ConstLabels: map[string]string{
"version": "v0.1.0",
},
})
httpRequestsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Count of all HTTP requests",
}, []string{"code", "method"})
)
func main() {
bind := ""
flagset := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flagset.StringVar(&bind, "bind", ":8080", "The socket to bind to.")
flagset.Parse(os.Args[1:])
r := prometheus.NewRegistry()
r.MustRegister(httpRequestsTotal)
r.MustRegister(version)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello from example application."))
})
notfound := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
http.Handle("/", promhttp.InstrumentHandlerCounter(httpRequestsTotal, handler))
http.Handle("/err", promhttp.InstrumentHandlerCounter(httpRequestsTotal, notfound))
http.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
log.Fatal(http.ListenAndServe(bind, nil))
}