-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
131 lines (118 loc) · 2.82 KB
/
main.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
package main
import (
"bytes"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"os"
"strconv"
"strings"
"time"
sigsci "github.com/signalsciences/sigsci-module-golang"
)
var (
debug = false
)
func helloworld(w http.ResponseWriter, r *http.Request) {
if debug {
reqbytes, _ := httputil.DumpRequest(r, true)
reqstr := string(reqbytes)
if !strings.HasSuffix(reqstr, "\n") {
reqstr += "\n"
}
log.Printf("REQUEST %s:\n%s", r.Header.Get("Content-Type"), reqstr)
}
delay := 0
body := []byte("OK")
code := 200
var err error
var qs url.Values
if r.URL != nil {
qs, err = url.ParseQuery(r.URL.RawQuery)
if err != nil {
log.Println("url.ParseQuery err:", err)
}
}
if qs == nil {
qs = make(url.Values)
}
if num, err := strconv.Atoi(qs.Get("response_time")); err == nil {
delay = num
}
if num, err := strconv.Atoi(qs.Get("response_code")); err == nil {
code = num
}
if num, err := strconv.Atoi(qs.Get("size")); err == nil {
body = bytes.Repeat([]byte{'a'}, num)
}
if len(qs.Get("echo")) > 0 {
body, err = ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("ioutil.ReadAll erred: %s", err)
}
r.Body = ioutil.NopCloser(bytes.NewReader(body))
}
if delay > 0 {
time.Sleep(time.Millisecond * time.Duration(delay))
}
if code >= 300 && code < 400 {
w.Header().Set("Location", "/foo")
}
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Content-Length", strconv.Itoa(len(body)))
// Populate varX response headers from form values for mtest form processing
if varq := r.FormValue("varq"); len(varq) > 0 {
w.Header().Add("varq", varq)
}
if varb := r.PostFormValue("varb"); len(varb) > 0 {
w.Header().Add("varb", varb)
}
if place := r.PostFormValue("place"); len(place) > 0 {
w.Header().Add("place", place)
}
if debug {
// Record the response so it can be logged
wrec := httptest.NewRecorder()
for k := range w.Header() {
v := w.Header().Get(k)
wrec.Header().Set(k, v)
}
wrec.WriteHeader(code)
wrec.Write(body)
resp := wrec.Result()
respbytes, _ := httputil.DumpResponse(resp, true)
respstr := string(respbytes)
if !strings.HasSuffix(respstr, "\n") {
respstr += "\n"
}
log.Printf("RESPONSE:\n%s", respstr)
}
w.WriteHeader(code)
w.Write(body)
}
func main() {
if dbg := os.Getenv("DEBUG"); len(dbg) > 0 && dbg != "0" {
debug = true
}
mux := http.NewServeMux()
// "/" is handle everything
mux.HandleFunc("/response", helloworld)
h, err := sigsci.NewModule(mux,
sigsci.Socket("tcp", "agent:9090"),
sigsci.Timeout(1500*time.Millisecond),
// Match agent defaults and better deal with mtest behavior test defaults
sigsci.MaxContentLength(300*1024),
sigsci.AnomalySize(512*1024),
)
if err != nil {
log.Fatal(err)
}
s := &http.Server{
Handler: h,
Addr: "0.0.0.0:8085",
}
log.Fatal(s.ListenAndServe())
}