-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (49 loc) · 1.39 KB
/
main.go
File metadata and controls
60 lines (49 loc) · 1.39 KB
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
package main
import (
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/telemetry"
)
var AppMetrics = &MyAppMetrics{telemetry.NewScope("app")}
type MyAppMetrics struct {
*telemetry.Scope
}
func (m *MyAppMetrics) RecordMyAppHit() {
m.RecordHit("my_app_hit", nil)
}
func (m *MyAppMetrics) RecordAppGauge(value float64) {
m.RecordGauge("my_app_gauge", nil, value)
}
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
// telemetry.Collector middleware mounts /metrics endpoint
// with prometheus metrics collector.
r.Use(telemetry.Collector(telemetry.Config{
AllowAny: true,
}, []string{"/api"})) // path prefix filters records generic http request metrics
r.Route("/api", func(r chi.Router) {
r.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
})
r.Get("/hit", func(w http.ResponseWriter, r *http.Request) {
// record a hit
AppMetrics.RecordMyAppHit()
w.Write([]byte("Hit recorded!"))
})
r.Get("/gauge/{value}", func(w http.ResponseWriter, r *http.Request) {
value := chi.URLParam(r, "value")
floatValue, err := strconv.ParseFloat(value, 64)
if err != nil {
w.Write([]byte("Invalid value"))
return
}
// record a gauge
AppMetrics.RecordAppGauge(floatValue)
w.Write([]byte("Gauge recorded!"))
})
})
http.ListenAndServe("localhost:3000", r)
}