-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
56 lines (47 loc) · 1.58 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
package main
import (
"encoding/json"
"net/http"
"time"
"github.com/autometrics-dev/autometrics-go/prometheus/autometrics"
"github.com/autometrics-dev/autometrics-go/prometheus/midhttp"
)
//go:generate autometrics
func main() {
autometrics.Init(
nil,
autometrics.DefBuckets,
autometrics.BuildInfo{Version: "0.4.0", Commit: "anySHA", Branch: ""},
)
http.Handle("/json", midhttp.Autometrics(
http.HandlerFunc(jsonHandler),
autometrics.WithSloName("API"),
autometrics.WithAlertLatency(100*time.Millisecond, 0.99),
))
http.Handle("/error", midhttp.Autometrics(
http.HandlerFunc(errorHandler),
autometrics.WithSloName("API"),
autometrics.WithAlertLatency(100*time.Millisecond, 0.99),
))
http.Handle("/slow", midhttp.Autometrics(
http.HandlerFunc(slowHandler),
autometrics.WithSloName("API"),
autometrics.WithAlertLatency(100*time.Millisecond, 0.99),
))
http.ListenAndServe(":8080", nil)
}
// jsonHandler is the handler function for the '/json' endpoint.
func jsonHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]string{"message": "Hello, World!"}
json.NewEncoder(w).Encode(data)
}
// errorHandler is the handler function for the '/error' endpoint.
func errorHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Oops! Something went wrong.", http.StatusInternalServerError)
}
// slowHandler is the handler function for the '/slow' endpoint.
func slowHandler(w http.ResponseWriter, r *http.Request) {
time.Sleep(5 * time.Second)
data := map[string]string{"message": "This response took 5 seconds to generate."}
json.NewEncoder(w).Encode(data)
}