-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
184 lines (168 loc) · 4.81 KB
/
service.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"time"
)
type errorAnomalyConfig struct {
ResponseCode int
Count int
}
type slowdownAnomalyConfig struct {
SlowdownMillis int
Count int
}
type crashAnomalyConfig struct {
Code int
}
type resourceAnomalyConfig struct {
Severity int
Count int
}
type callee struct {
Adr string // URL address to call
Count int // number of calls per minute
}
type config struct {
ErrorConfig errorAnomalyConfig
SlowdownConfig slowdownAnomalyConfig
CrashConfig crashAnomalyConfig
ResourceConfig resourceAnomalyConfig
Callees []callee
Balanced bool
Proxy bool
}
var conf config
var reqcount int
func receiveConfig(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
w.WriteHeader(http.StatusNoContent)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
//fmt.Printf(string(body))
err = json.Unmarshal(body, &conf)
if err != nil {
fmt.Printf("config payload wrong")
log.Printf("%s config payload is wrong", os.Args[0])
panic(err)
}
log.Printf("%s received a new service config deployment", os.Args[0])
default:
fmt.Fprintf(w, "sorry, only POST method is supported.")
}
defer r.Body.Close()
}
func handleIcon(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
}
func sayHello(w http.ResponseWriter, r *http.Request) {
var buf bytes.Buffer
reqcount++
fmt.Fprintf(&buf, "it's the %d call\n", reqcount)
fmt.Fprintf(&buf, "what I did:\n")
// first call all callees we have in the config with the multiplicity given
failures := false
for ci, element := range conf.Callees {
if !conf.Balanced || reqcount%len(conf.Callees) == ci {
for i := 0; i < element.Count; i++ {
req, err := http.NewRequest("GET", element.Adr, nil)
if err != nil {
// os.Args[0] to get the current exe name
log.Printf("%s error reading request.", os.Args[0])
os.Exit(1)
}
if conf.Proxy {
log.Printf("%s dt header: %s ", os.Args[0], r.Header.Get("X-Dynatrace"))
log.Printf("%s RemoteAddr: %s ", os.Args[0], r.RemoteAddr)
req.Header.Set("X-Dynatrace", r.Header.Get("X-Dynatrace"))
req.Header.Set("x-forwarded-for", r.RemoteAddr)
req.Header.Set("forwarded", r.RemoteAddr)
}
req.Header.Set("Cache-Control", "no-cache")
client := &http.Client{Timeout: time.Second * 10}
resp, err := client.Do(req)
if err != nil {
log.Printf("%s error reading response.", os.Args[0])
os.Exit(1)
} else {
if resp.StatusCode != 200 {
log.Printf("%s got a bad return", os.Args[0])
failures = true
}
}
defer resp.Body.Close()
}
fmt.Fprintf(&buf, "called %s %d times\n", element.Adr, element.Count)
}
}
// then check if we should crash the process
if conf.CrashConfig.Code != 0 {
log.Printf("%s cashed.", os.Args[0])
panic("a problem")
}
// then check if we should add a delay
if conf.SlowdownConfig.SlowdownMillis != 0 && conf.SlowdownConfig.Count > 0 {
time.Sleep(time.Duration(conf.SlowdownConfig.SlowdownMillis) * time.Millisecond)
conf.SlowdownConfig.Count = conf.SlowdownConfig.Count - 1
fmt.Fprintf(&buf, "sleeped for %d millis\n", conf.SlowdownConfig.SlowdownMillis)
}
// then check if we should increase resource consumption
if conf.ResourceConfig.Severity != 0 && conf.ResourceConfig.Count > 0 {
for c := 0; c <= conf.ResourceConfig.Severity; c++ {
m1 := [100][100]int{}
for i := 0; i < 100; i++ {
for j := 0; j < 100; j++ {
m1[i][j] = rand.Int()
}
}
}
fmt.Fprintf(&buf, "allocated %d 100x100 matrices with random values\n", conf.ResourceConfig.Severity)
conf.ResourceConfig.Count = conf.ResourceConfig.Count - 1
log.Printf("%s high resource consumption service call", os.Args[0])
}
// then check if the should return an error response code
if failures || (conf.ErrorConfig.ResponseCode != 0 && conf.ErrorConfig.Count > 0) {
if conf.ErrorConfig.ResponseCode == 400 {
w.WriteHeader(http.StatusForbidden)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
conf.ErrorConfig.Count = conf.ErrorConfig.Count - 1
} else {
message := r.URL.Path
message = strings.TrimPrefix(message, "/")
message = "finally returned " + message
w.Write([]byte(message))
}
defer r.Body.Close()
}
func main() {
port := 8080
if len(os.Args) > 1 {
arg := os.Args[1]
fmt.Printf("Start anomaly simulation service at port: %s\n", arg)
i1, err := strconv.Atoi(arg)
if err == nil {
port = i1
}
} else {
fmt.Printf("Start anomaly simulation service at default port: %d\n", port)
}
http.HandleFunc("/", sayHello)
http.HandleFunc("/favicon.ico", handleIcon)
http.HandleFunc("/config", receiveConfig)
if err := http.ListenAndServe(":"+strconv.Itoa(port), nil); err != nil {
panic(err)
}
}