This repository has been archived by the owner on Jan 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
84 lines (78 loc) · 2.4 KB
/
server.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
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
requestTotals = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "who_lives_where_request_count",
Help: "Count of requests for who_lives_where service.",
},
[]string{"code", "method"},
)
requestProcessingTimes = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "who_lives_where_processing_time",
Help: "Processing time latencies for the who_lives_where service.",
// 0.1 seconds = 100 millisecond, up to 10 seconds
Buckets: prometheus.LinearBuckets(0.1, 0.1, 100),
},
[]string{"who"},
)
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
startTime := time.Now().UnixNano()
fmt.Fprintf(w, `
<html><head><title>Who lives where?</title></head>
<body>
<p>Hi there!</p>
<p>Find out who lives where:</p>
<ul>
<li><a href="/person?who=anita">Anita</a></li>
<li><a href="/person?who=tamao">Tamao</a></li>
<li><a href="/person?who=ilya">Ilya</a></li>
</ul>
</body>
</html>
`)
timeNanoseconds := time.Now().UnixNano() - startTime
requestTotals.WithLabelValues("200", r.Method).Inc()
requestProcessingTimes.WithLabelValues("home").Observe(
// micro milli seconds
float64(timeNanoseconds) / (1000 * 1000 * 1000),
)
})
http.HandleFunc("/person", func(w http.ResponseWriter, r *http.Request) {
startTime := time.Now().UnixNano()
person := r.URL.Query()["who"][0]
responseCode := "200"
if person == "anita" {
fmt.Fprintf(w, "Anita lives in Mexico")
} else if person == "tamao" {
fmt.Fprintf(w, "Tamao lives in California")
} else if person == "ilya" {
time.Sleep(1 * time.Second)
fmt.Fprintf(w, "Ilya lives in London")
} else {
fmt.Fprintf(w, "Person not found, error!")
responseCode = "404"
w.WriteHeader(http.StatusNotFound)
}
timeNanoseconds := time.Now().UnixNano() - startTime
requestTotals.WithLabelValues(responseCode, r.Method).Inc()
requestProcessingTimes.WithLabelValues(person).Observe(
// micro milli seconds
float64(timeNanoseconds) / (1000 * 1000 * 1000),
)
})
prometheus.MustRegister(requestTotals)
prometheus.MustRegister(requestProcessingTimes)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":8080", nil))
}