-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathapi.go
More file actions
135 lines (123 loc) · 3.59 KB
/
Copy pathapi.go
File metadata and controls
135 lines (123 loc) · 3.59 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"fmt"
"math/rand"
"net/http"
_ "net/http/pprof"
"time"
"github.com/prometheus/client_golang/prometheus"
)
var (
namespace = "demo"
requestHistogram = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: "api",
Name: "request_duration_seconds",
Help: "A classic histogram of the API HTTP request durations in seconds.",
Buckets: prometheus.ExponentialBuckets(0.0001, 1.5, 25),
},
[]string{"method", "path", "status"},
)
nativeRequestHistogram = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: "api",
Name: "request_duration_native_seconds",
Help: "A native histogram of the API HTTP request durations in seconds.",
NativeHistogramBucketFactor: 1.1,
},
[]string{"method", "path", "status"},
)
requestsInProgress = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "api",
Name: "http_requests_in_progress",
Help: "The current number of API HTTP requests in progress.",
})
)
func init() {
prometheus.MustRegister(requestHistogram)
prometheus.MustRegister(nativeRequestHistogram)
prometheus.MustRegister(requestsInProgress)
}
type responseOpts struct {
baseLatency time.Duration
errorRatio float64
// Whenever 10*outageDuration has passed, an outage will be simulated
// that lasts for outageDuration. During the outage, errorRatio is
// increased by a factor of 10, and baseLatency by a factor of 3. At
// start-up time, an outage is simulated, too (so that you can see the
// effects right ahead and don't have to wait for 10*outageDuration).
outageDuration time.Duration
}
var opts = map[string]map[string]responseOpts{
"/api/foo": {
"GET": {
baseLatency: 10 * time.Millisecond,
errorRatio: 0.005,
outageDuration: 23 * time.Second,
},
"POST": {
baseLatency: 20 * time.Millisecond,
errorRatio: 0.02,
outageDuration: time.Minute,
},
},
"/api/bar": {
"GET": {
baseLatency: 15 * time.Millisecond,
errorRatio: 0.0025,
outageDuration: 13 * time.Second,
},
"POST": {
baseLatency: 50 * time.Millisecond,
errorRatio: 0.01,
outageDuration: 47 * time.Second,
},
},
}
func handleAPI(w http.ResponseWriter, r *http.Request) {
begun := time.Now()
requestsInProgress.Inc()
status := http.StatusOK
defer func() {
requestsInProgress.Dec()
requestHistogram.With(prometheus.Labels{
"method": r.Method,
"path": r.URL.Path,
"status": fmt.Sprint(status),
}).Observe(time.Since(begun).Seconds())
nativeRequestHistogram.With(prometheus.Labels{
"method": r.Method,
"path": r.URL.Path,
"status": fmt.Sprint(status),
}).Observe(time.Since(begun).Seconds())
}()
pathOpts, ok := opts[r.URL.Path]
if !ok {
status = http.StatusNotFound
http.Error(w, fmt.Sprintf("Path %q not found.", r.URL.Path), status)
return
}
methodOpts, ok := pathOpts[r.Method]
if !ok {
status = http.StatusMethodNotAllowed
http.Error(w, fmt.Sprintf("Method %q not allowed.", r.Method), status)
return
}
latencyFactor := time.Duration(1)
errorFactor := 1.
if time.Since(start)%(10*methodOpts.outageDuration) < methodOpts.outageDuration {
latencyFactor *= 3
errorFactor *= 10
}
time.Sleep(
(methodOpts.baseLatency + time.Duration(rand.NormFloat64()*float64(methodOpts.baseLatency)/10)) * latencyFactor,
)
if rand.Float64() <= methodOpts.errorRatio*errorFactor {
status = http.StatusInternalServerError
http.Error(w, "Fake error to test monitoring.", status)
}
}