-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
154 lines (134 loc) · 3.85 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"gopkg.in/yaml.v3"
)
type Config struct {
StatusCodes []struct {
StatusCode int `yaml:"code"`
Repeat int `yaml:"repeat"`
} `yaml:"status_codes"`
Metrics []struct {
Identifier string `yaml:"identifier"`
ValueCycle struct {
InitialValue int `yaml:"initial_value"`
Trends []struct {
Type string `yaml:"type"`
Step int `yaml:"step"`
Repeat int `yaml:"repeat"`
} `yaml:"trends"`
} `yaml:"value_cycle"`
} `yaml:"metrics"`
metricCounters []struct {
ActualValue int
ActualCounter int
ActualTrend int
}
actualStatusCodeIndex int
actualStatusCodeCounter int
requestCounter int
}
func (c *Config) getResponseBody() (body string) {
sb := strings.Builder{}
for index, metric := range c.Metrics {
sb.WriteString(metric.Identifier)
sb.WriteString(" ")
sb.WriteString(c.getActualValueFromMetric(index))
sb.WriteString("\n")
}
return sb.String()
}
func (c *Config) getActualValueFromMetric(metricIndex int) string {
return strconv.Itoa(c.metricCounters[metricIndex].ActualValue)
}
func (c *Config) getActualReturnCode() int {
return c.StatusCodes[c.actualStatusCodeIndex].StatusCode
}
func (c *Config) stepStatusCode() {
if c.actualStatusCodeCounter < c.StatusCodes[c.actualStatusCodeIndex].Repeat-1 {
c.actualStatusCodeCounter++
} else if c.actualStatusCodeIndex < len(c.StatusCodes)-1 {
c.actualStatusCodeCounter = 0
c.actualStatusCodeIndex++
} else {
c.actualStatusCodeCounter = 0
c.actualStatusCodeIndex = 0
}
}
func (c *Config) nextValueOfMetric(index int) {
if c.Metrics[index].ValueCycle.Trends[c.metricCounters[index].ActualTrend].Type == "increment" {
c.metricCounters[index].ActualValue += c.Metrics[index].ValueCycle.Trends[c.metricCounters[index].ActualTrend].Step
} else {
c.metricCounters[index].ActualValue -= c.Metrics[index].ValueCycle.Trends[c.metricCounters[index].ActualTrend].Step
}
}
func (c *Config) stepMetric(index int) {
if c.metricCounters[index].ActualCounter < c.Metrics[index].ValueCycle.Trends[c.metricCounters[index].ActualTrend].Repeat {
c.metricCounters[index].ActualCounter++
c.nextValueOfMetric(index)
} else if c.metricCounters[index].ActualTrend < len(c.Metrics[index].ValueCycle.Trends)-1 {
c.metricCounters[index].ActualTrend++
c.metricCounters[index].ActualCounter = 0
c.nextValueOfMetric(index)
} else {
c.metricCounters[index].ActualCounter = 0
c.metricCounters[index].ActualTrend = 0
c.nextValueOfMetric(index)
}
}
// increment all counters
func (c *Config) prepareForNextRequest() {
c.stepStatusCode()
for index := range c.Metrics {
c.stepMetric(index)
}
}
func (c *Config) init() {
file, err := os.Open(configFilePath)
defer file.Close()
if err != nil {
log.Println(err.Error())
}
decoder := yaml.NewDecoder(file)
if err := decoder.Decode(&c); err != nil {
log.Println(err.Error())
}
c.requestCounter = 0
c.actualStatusCodeCounter = 0
c.actualStatusCodeIndex = 0
c.metricCounters = make([]struct {
ActualValue int
ActualCounter int
ActualTrend int
}, len(c.Metrics))
for index, metric := range c.Metrics {
c.metricCounters[index].ActualValue = metric.ValueCycle.InitialValue
c.metricCounters[index].ActualCounter = 0
c.metricCounters[index].ActualTrend = 0
}
}
var conf Config
var configFilePath string
// no thread safe (1 request at a time or crash)
func metricsHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(conf.getActualReturnCode())
fmt.Fprint(w, conf.getResponseBody())
conf.prepareForNextRequest()
}
func main() {
if len(os.Args) < 2 {
log.Println("Config file not specified, using default /app/config.yml")
configFilePath = "/app/config.yml"
} else {
configFilePath = os.Args[1]
}
conf.init()
fmt.Printf("%+v\n", conf)
http.HandleFunc("/metrics", metricsHandler)
http.ListenAndServe(":5000", nil)
}