-
Notifications
You must be signed in to change notification settings - Fork 115
/
routemonitors.go
314 lines (282 loc) · 9.59 KB
/
routemonitors.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package routemonitors
import (
"bufio"
"context"
"fmt"
"log"
"math"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/openshift/osde2e/pkg/common/helper"
"github.com/openshift/osde2e/pkg/common/metadata"
vegeta "github.com/tsenart/vegeta/lib"
"github.com/tsenart/vegeta/lib/plot"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
consoleNamespace = "openshift-console"
consoleLabel = "console"
monitoringNamespace = "openshift-monitoring"
oauthNamespace = "openshift-authentication"
oauthName = "oauth-openshift"
)
type RouteMonitors struct {
Monitors map[string]<-chan *vegeta.Result
Metrics map[string]*vegeta.Metrics
Plots map[string]*plot.Plot
targeters map[string]vegeta.Targeter
attackers []*vegeta.Attacker
ReportData map[string][]RouteMonData
}
// Frequency of requests per second (per route)
const (
pollFrequency = 3
timeoutSeconds = 3 * time.Second
)
// Data Structure used to store time-value values of Route Monitor Data
type RouteMonData struct {
Time, Value float64
}
// Detects the available routes in the cluster and initializes monitors for their availability
func Create(ctx context.Context, h *helper.H) (*RouteMonitors, error) {
// record all targeters created in a map, accessible via a key which is their URL
targeters := make(map[string]vegeta.Targeter, 0)
// Create a monitor for the web console
consoleRoute, err := h.Route().RouteV1().Routes(consoleNamespace).Get(ctx, consoleLabel, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("could not retrieve console route %s", consoleLabel)
}
consoleUrl := fmt.Sprintf("https://%s", consoleRoute.Spec.Host)
u, err := url.Parse(consoleUrl)
if err == nil {
consoleTargeter := vegeta.NewStaticTargeter(vegeta.Target{
Method: "GET",
URL: consoleUrl,
})
targeters[u.Host] = consoleTargeter
}
// Create a monitor for the oauth URL
oauthRoute, err := h.Route().RouteV1().Routes(oauthNamespace).Get(ctx, oauthName, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("could not retrieve oauth route %s", oauthName)
}
oauthUrl := fmt.Sprintf("https://%s/healthz", oauthRoute.Spec.Host)
u, err = url.Parse(oauthUrl)
if err == nil {
oauthTargeter := vegeta.NewStaticTargeter(vegeta.Target{
Method: "GET",
URL: oauthUrl,
})
targeters[u.Host] = oauthTargeter
}
// Create monitors for API Server URLs
apiservers, err := h.Cfg().ConfigV1().APIServers().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("could not retrieve list of API servers")
}
for _, apiServer := range apiservers.Items {
for _, servingCert := range apiServer.Spec.ServingCerts.NamedCertificates {
for _, name := range servingCert.Names {
apiUrl := fmt.Sprintf("https://%s:6443/healthz", name)
apiTargeter := vegeta.NewStaticTargeter(vegeta.Target{
Method: "GET",
URL: apiUrl,
})
u, err := url.Parse(apiUrl)
if err == nil {
targeters[u.Host] = apiTargeter
}
}
}
}
// If we created any routes during workload testing, let's add them too
workloadRoutes, err := h.Route().RouteV1().Routes(h.CurrentProject()).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("could not retrieve list of workload routes")
}
for _, workloadRoute := range workloadRoutes.Items {
workloadUrl := fmt.Sprintf("https://%s", workloadRoute.Spec.Host)
u, err := url.Parse(workloadUrl)
if err == nil {
workloadTargeter := vegeta.NewStaticTargeter(vegeta.Target{
Method: "GET",
URL: workloadUrl,
})
targeters[u.Host] = workloadTargeter
}
}
// Create monitors for each route existing in openshift-monitoring
monitoringRoutes, err := h.Route().RouteV1().Routes(monitoringNamespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("could not retrieve list of workload routes")
}
for _, monitoringRoute := range monitoringRoutes.Items {
monitoringURL := fmt.Sprintf("https://%s", monitoringRoute.Spec.Host)
u, err := url.Parse(monitoringURL)
if err == nil {
monitoringTargeter := vegeta.NewStaticTargeter(vegeta.Target{
Method: "GET",
URL: monitoringURL,
})
targeters[u.Host] = monitoringTargeter
}
}
return &RouteMonitors{
Monitors: make(map[string]<-chan *vegeta.Result),
Metrics: make(map[string]*vegeta.Metrics),
Plots: make(map[string]*plot.Plot),
targeters: targeters,
ReportData: make(map[string][]RouteMonData),
}, nil
}
// Sets the RouteMonitors to begin polling the configured routes with traffic
func (rm *RouteMonitors) Start() {
pollRate := vegeta.Rate{Freq: pollFrequency, Per: time.Second}
timeout := vegeta.Timeout(timeoutSeconds)
for url, targeter := range rm.targeters {
log.Printf("Setting up monitor for %s\n", url)
attacker := vegeta.NewAttacker(timeout)
rm.Monitors[url] = attacker.Attack(targeter, pollRate, 0, url)
rm.Plots[url] = createPlot(url)
rm.attackers = append(rm.attackers, attacker)
}
for title := range rm.Monitors {
rm.Metrics[title] = &vegeta.Metrics{}
}
}
// Sets the RouteMonitors to cease polling the configured routes with traffic
func (rm *RouteMonitors) End() {
for _, attacker := range rm.attackers {
attacker.Stop()
}
for _, metric := range rm.Metrics {
metric.Close()
}
}
// Stores the measured RouteMonitor metrics inside osde2e metadata for DataHub
func (rm *RouteMonitors) StoreMetadata() {
for title, metric := range rm.Metrics {
latency := float64(metric.Latencies.Mean / time.Millisecond)
if latency < 0 {
latency = 0
}
if math.IsNaN(metric.Throughput) {
metric.Throughput = 0
}
if math.IsNaN(metric.Success) {
metric.Success = 0
}
metadata.Instance.SetRouteLatency(title, latency)
metadata.Instance.SetRouteThroughput(title, metric.Throughput)
metadata.Instance.SetRouteAvailability(title, metric.Success)
}
}
// Saves the measured RouteMonitor metrics in HDR Histogram reports in the specified base directory
func (rm *RouteMonitors) SaveReports(baseDir string) error {
outputDirectory := filepath.Join(baseDir, "route-monitors")
if _, err := os.Stat(outputDirectory); os.IsNotExist(err) {
if err := os.Mkdir(outputDirectory, os.FileMode(0o755)); err != nil {
return fmt.Errorf("error while creating route monitor report directory %s: %v", outputDirectory, err)
}
}
for title, metric := range rm.Metrics {
histoPath := filepath.Join(outputDirectory, fmt.Sprintf("%s.histo", title))
reporter := vegeta.NewHDRHistogramPlotReporter(metric)
out, err := os.Create(histoPath)
if err != nil {
return fmt.Errorf("error while creating route monitor report: %v", err)
}
reporter.Report(out)
log.Printf("Wrote route monitor histogram: %s\n", histoPath)
}
return nil
}
// Saves HTML-formatted latency plots in the specified base directory
func (rm *RouteMonitors) SavePlots(baseDir string) error {
outputDirectory := filepath.Join(baseDir, "route-monitors")
if _, err := os.Stat(outputDirectory); os.IsNotExist(err) {
if err := os.Mkdir(outputDirectory, os.FileMode(0o755)); err != nil {
return fmt.Errorf("error while creating route monitor report directory %s: %v", outputDirectory, err)
}
}
for title, pl := range rm.Plots {
plotPath := filepath.Join(outputDirectory, fmt.Sprintf("%s.html", title))
out, err := os.Create(plotPath)
if err != nil {
return fmt.Errorf("error while creating route monitor report: %v", err)
}
pl.WriteTo(out)
log.Printf("Wrote route monitor plot: %s\n", plotPath)
}
return nil
}
// Creates a new plot with the specified title
func createPlot(title string) *plot.Plot {
return plot.New(
plot.Title(title),
plot.Label(plot.ErrorLabeler),
)
}
// Extracts the numbers and title from the route monitor plot files
func (rm *RouteMonitors) ExtractData(baseDir string) error {
outputDirectory := filepath.Join(baseDir, "route-monitors")
if _, err := os.Stat(outputDirectory); os.IsNotExist(err) {
if err := os.MkdirAll(outputDirectory, os.FileMode(0o755)); err != nil {
return fmt.Errorf("error while creating route monitor report directory %s: %v", outputDirectory, err)
}
}
for title := range rm.Plots {
// Open the plot file
htmlFilePath := filepath.Join(outputDirectory, fmt.Sprintf("%s.html", title))
file, err := os.Open(htmlFilePath)
if err != nil {
log.Printf("Unable to read route monitor plot file; %v\n", err)
}
// Regex to match numbers for the data variable inside the plot file
startRegex := regexp.MustCompile(`var data`)
dataNumRegex := regexp.MustCompile(`[0-9]*\.?[0-9]+,[0-9]*\.?[0-9]+`)
// This list stores the numbers
dataList := make([]RouteMonData, 0)
// Scan each line in the html file to extract data
scanner := bufio.NewReader(file)
scanLoop:
for {
fileBytes, _, err := scanner.ReadLine()
if err != nil {
break scanLoop
}
line := strings.TrimSpace(string(fileBytes))
if readData := len(line) > 0 && startRegex.MatchString(line); !readData {
continue scanLoop
}
if dataNumRegex.FindString(line) == "" {
continue scanLoop
}
numStrings := strings.Split(startRegex.FindString(line), ",")
if len(numStrings) < 2 {
continue scanLoop
}
numData := RouteMonData{}
numData.Time, err = strconv.ParseFloat(numStrings[0], 64)
if err != nil {
log.Printf("Error while parsing route monitor data values - %v", err)
}
numData.Value, err = strconv.ParseFloat(numStrings[1], 64)
if err != nil {
log.Printf("Error while parsing route monitor data values - %v", err)
}
dataList = append(dataList, numData)
}
// Store the extracted data corresponding to the plotfile title
rm.ReportData[title] = dataList
if err != nil {
log.Printf("Error - %v", err.Error())
}
}
return nil
}