-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
59 lines (55 loc) · 1.7 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
package main
import (
"log"
"math/rand"
"time"
"github.com/valyala/fasthttp"
"github.com/zerodha/fastglue"
fastgluemetrics "github.com/zerodha/fastglue-metrics"
)
var (
fakeResponse = make([]byte, 1024*1000*1)
)
func main() {
// Initialize fastglue.
g := fastglue.NewGlue()
// Initialise fastglue-metrics exporter.
exporter := fastgluemetrics.NewMetrics(g, fastgluemetrics.Opts{
ExposeGoMetrics: true,
NormalizeHTTPStatus: true,
ServiceName: "dummy",
MatchedRoutePathParam: g.MatchedRoutePathParam,
})
// Register handlers.
g.GET("/", func(r *fastglue.Request) error {
return r.SendEnvelope("Welcome to dummy-app metrics. Visit /metrics.")
})
g.GET("/fake", func(r *fastglue.Request) error {
r.RequestCtx.Write(fakeResponse)
return nil
})
g.GET("/slow/{user}/ping", func(r *fastglue.Request) error {
sleep := 0.5 + rand.Float64()*1.75
time.Sleep(time.Duration(sleep) * 1000 * time.Millisecond)
return r.SendEnvelope("Sleeping slow respo")
})
g.GET("/bad/{user}", func(r *fastglue.Request) error {
status := [9]int{300, 400, 413, 500, 417, 404, 402, 503, 502}
return r.SendErrorEnvelope(status[rand.Intn(9)], "oops", nil, "")
})
// Expose the registered metrics at `/metrics` path.
g.GET("/metrics", exporter.HandleMetrics)
// HTTP server.
s := &fasthttp.Server{
Name: "metrics",
ReadTimeout: time.Millisecond * 3000,
WriteTimeout: time.Millisecond * 6000,
MaxKeepaliveDuration: time.Millisecond * 5000,
MaxRequestBodySize: 50000,
ReadBufferSize: 50000,
}
log.Println("starting server on :6090")
if err := g.ListenAndServe("0.0.0.0:6090", "", s); err != nil {
log.Println("error starting server:", err)
}
}