-
Notifications
You must be signed in to change notification settings - Fork 0
/
httphandlerunderstanding.go
112 lines (96 loc) · 3.05 KB
/
httphandlerunderstanding.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"flag"
"fmt"
"math/rand"
"net/http"
"os"
"time"
"github.com/fatih/structs"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/waleedsamy/letsgo/config"
log "github.com/sirupsen/logrus"
)
var (
httpResponsesTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "letsgo",
Subsystem: "http_server",
Name: "http_responses_total",
Help: "The count of http responses issued, classified by code and method.",
},
[]string{"code", "method"},
)
httpResponseLatencies = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "letsgo",
Subsystem: "http_server",
Name: "http_response_latencies",
Help: "Duration of sample batch send calls to the remote storage.",
Buckets: prometheus.DefBuckets,
},
[]string{"code", "method"},
)
)
func init() {
prometheus.MustRegister(httpResponsesTotal)
prometheus.MustRegister(httpResponseLatencies)
log.SetOutput(os.Stdout)
}
type pound float32
type database map[string]pound
func (p pound) String() string {
return fmt.Sprintf("£%.2f", p)
}
func (d database) bar(w http.ResponseWriter, r *http.Request) {
start := time.Now()
time.Sleep(time.Duration(rand.Int31n(1000)) * time.Millisecond)
fmt.Fprintf(w, "%s: %s", "bar", d["bar"])
elapsed := time.Since(start)
msElapsed := elapsed / time.Millisecond
httpResponsesTotal.WithLabelValues("200", r.Method).Inc()
httpResponseLatencies.WithLabelValues("200", r.Method).Observe(float64(msElapsed))
}
func (d database) foo(w http.ResponseWriter, r *http.Request) {
start := time.Now()
time.Sleep(time.Duration(rand.Int31n(1000)) * time.Millisecond)
fmt.Fprintf(w, "%s: %s", "foo", d["foo"])
elapsed := time.Since(start)
msElapsed := elapsed / time.Millisecond
httpResponsesTotal.WithLabelValues("200", r.Method).Inc()
httpResponseLatencies.WithLabelValues("200", r.Method).Observe(float64(msElapsed))
}
func (d database) baz(w http.ResponseWriter, r *http.Request) {
start := time.Now()
time.Sleep(time.Duration(rand.Int31n(1000)) * time.Millisecond)
fmt.Fprintf(w, "%s: %s", "baz", d["baz"])
elapsed := time.Since(start)
msElapsed := elapsed / time.Millisecond
httpResponsesTotal.WithLabelValues("200", r.Method).Inc()
httpResponseLatencies.WithLabelValues("200", r.Method).Observe(float64(msElapsed))
}
func main() {
configFile := flag.String("config.file", "", "Let configuration file name.")
flag.Parse()
if *configFile == "" {
flag.PrintDefaults()
os.Exit(1)
}
var db = database{
"foo": 1,
"bar": 2,
"baz": 3,
}
http.HandleFunc("/foo", db.foo)
http.HandleFunc("/bar", db.bar)
http.Handle("/baz", http.HandlerFunc(db.baz))
http.Handle("/metrics", promhttp.Handler())
cfg, err := config.GetConfig(*configFile)
if err != nil {
log.WithField("config.file", *configFile).Fatal("unable to parse file")
}
log.WithFields(log.Fields{"port": cfg.Port}).Info("Let is ready")
log.WithFields(structs.Map(cfg)).Info("Let is configured by")
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil))
}