-
Notifications
You must be signed in to change notification settings - Fork 0
/
cities.go
48 lines (37 loc) · 829 Bytes
/
cities.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
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var cities = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "city_total",
Help: "Per-city Counts",
},
[]string{"city"},
)
type cityCount struct {
Count float64
City string
}
func handler(w http.ResponseWriter, r *http.Request) {
var cc cityCount
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&cc); err != nil {
http.Error(w, "Invalid JSON", 400)
return
}
log.Println(cc)
cities.WithLabelValues(cc.City).Add(cc.Count)
}
func init() {
prometheus.MustRegister(cities)
}
func main() {
http.HandleFunc("/", handler)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":8080", nil))
}