Add prometheus metrics
This commit is contained in:
parent
3ea5327762
commit
574ced6e36
33
main.go
33
main.go
@ -4,11 +4,15 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
|
"mcquay.me/metrics"
|
||||||
)
|
)
|
||||||
|
|
||||||
const version = "v0.0.11"
|
const version = "v0.1.0"
|
||||||
|
|
||||||
type v struct {
|
type v struct {
|
||||||
Hostname string `json:"hostname"`
|
Hostname string `json:"hostname"`
|
||||||
@ -16,11 +20,15 @@ type v struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
m, err := metrics.New("hw")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("metrics: %v", err)
|
||||||
|
}
|
||||||
hn, err := os.Hostname()
|
hn, err := os.Hostname()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("hostname: %+v", err)
|
log.Fatalf("hostname: %+v", err)
|
||||||
}
|
}
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
http.HandleFunc("/", m.WrapFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
r := v{
|
r := v{
|
||||||
Hostname: hn,
|
Hostname: hn,
|
||||||
@ -29,14 +37,25 @@ func main() {
|
|||||||
if err := json.NewEncoder(w).Encode(r); err != nil {
|
if err := json.NewEncoder(w).Encode(r); err != nil {
|
||||||
log.Printf("json: %+v", err)
|
log.Printf("json: %+v", err)
|
||||||
}
|
}
|
||||||
})
|
}))
|
||||||
http.HandleFunc("/env", func(w http.ResponseWriter, req *http.Request) {
|
http.HandleFunc("/env", m.WrapFunc("/env", func(w http.ResponseWriter, req *http.Request) {
|
||||||
for _, line := range os.Environ() {
|
for _, line := range os.Environ() {
|
||||||
fmt.Fprintf(w, "%v\n", line)
|
fmt.Fprintf(w, "%v\n", line)
|
||||||
}
|
}
|
||||||
})
|
}))
|
||||||
http.HandleFunc("/live", ok)
|
|
||||||
http.HandleFunc("/ready", ok)
|
codes := []int{
|
||||||
|
http.StatusBadGateway,
|
||||||
|
http.StatusBadRequest,
|
||||||
|
http.StatusUnauthorized,
|
||||||
|
}
|
||||||
|
http.HandleFunc("/bad", m.WrapFunc("/bad", func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
code := codes[rand.Intn(len(codes))]
|
||||||
|
w.WriteHeader(code)
|
||||||
|
}))
|
||||||
|
http.HandleFunc("/live", m.WrapFunc("/live", ok))
|
||||||
|
http.HandleFunc("/ready", m.WrapFunc("/ready", ok))
|
||||||
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
if err := http.ListenAndServe(":8080", nil); err != nil {
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
||||||
log.Fatalf("listen and serve: %v", err)
|
log.Fatalf("listen and serve: %v", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user