This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
offline.go
139 lines (122 loc) · 3.21 KB
/
offline.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
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"text/template"
"github.com/Masterminds/sprig"
)
const (
outDir = "."
)
type writerFactory func(*offlineHandler) io.Writer
type outputFormat struct {
template string
extension string
}
type offlineHandler struct {
format outputFormat
buffer []HTTPEvent
defaultBatchSize int
currentBatchSize int
numWrites int
writerFactory writerFactory
templateFuncMap template.FuncMap
}
func getOfflineHandler(template string, extension string) eventHandler {
return &offlineHandler{
format: getFormat(template, extension),
defaultBatchSize: flags.batchSize,
currentBatchSize: flags.batchSize,
writerFactory: getFileWriter,
templateFuncMap: sprig.TxtFuncMap(),
}
}
func (h *offlineHandler) handleEvent(logEvent HTTPEvent) {
go telemetry.logUsage(telemetryUsageOffline)
logEvent.ReqHeaders = h.readReplayHeaders(logEvent.ReqHeaders)
h.buffer = append(h.buffer, logEvent)
if len(h.buffer) == h.currentBatchSize {
h.flushBuffer()
}
}
func (h *offlineHandler) readReplayHeaders(headers []Header) []Header {
toRemove := []int{}
for i, header := range headers {
headerParts := strings.Split(header.Name, "Replay_")
if len(headerParts) != 2 {
continue
}
// all special "replay_" headers should be removed
// before the data is persisted in any way
toRemove = append(toRemove, i)
switch headerParts[1] {
case "batch":
dynamicBatchSize, err := strconv.Atoi(header.Value)
if err != nil {
log.Println("Unable to parse dynamic batch size: " + header.Value)
continue
} else if dynamicBatchSize == 0 {
h.flushBuffer()
log.Printf("Batch size cannot be zero! Using default batch=%d\n", h.defaultBatchSize)
} else {
h.flushBuffer()
log.Printf("Detected dynamic batch size with size %d\n", dynamicBatchSize)
h.currentBatchSize = dynamicBatchSize
}
}
}
return removeAll(headers, toRemove)
}
func (h *offlineHandler) getNextFileName() string {
return fmt.Sprintf("replay_scenarios_%d.%s", h.numWrites, h.format.extension)
}
func getFileWriter(h *offlineHandler) io.Writer {
if h.format.extension == "" {
log.Println("[ERROR] File extension is empty, not writing file")
return nil
}
fileName := h.getNextFileName()
out := filepath.Join(outDir, fileName)
f, err := os.Create(out)
if err != nil {
logErr(err)
return nil
}
return f
}
func (h *offlineHandler) runTemplate() error {
t, err := template.New("").Funcs(h.templateFuncMap).Parse(h.format.template)
if err != nil {
return err
}
return t.Execute(h.writerFactory(h), h.buffer)
}
// KarateGen: Write out buffered events in the case of a user-enacted exit (i.e. ctrl+c)
func (h *offlineHandler) flushBuffer() {
numEvents := len(h.buffer)
if numEvents > 0 {
log.Println("Flushing buffer...")
err := h.runTemplate()
if err == nil {
suffix := ""
if numEvents > 1 {
suffix = "s"
}
log.Printf("Wrote %d scenario%s to file %s\n", numEvents, suffix, h.getNextFileName())
h.numWrites++
} else {
logErr(err)
}
}
h.resetBuffer()
}
func (h *offlineHandler) resetBuffer() {
// https://stackoverflow.com/a/16973160
h.buffer = nil
h.currentBatchSize = h.defaultBatchSize
}