forked from leoleovich/grafsy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
188 lines (170 loc) · 4.85 KB
/
server.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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"regexp"
"strconv"
"strings"
"time"
)
type Server struct {
conf Config
lc LocalConfig
mon *Monitoring
lg log.Logger
ch chan string
chA chan string
aM *regexp.Regexp
aggrRegexp *regexp.Regexp
}
// Aggregate metrics with prefix
func (s Server) aggrMetricsWithPrefix() {
for ; ; time.Sleep(time.Duration(s.conf.AggrInterval) * time.Second) {
// We assume, that aggregation is done for a current point in time
aggrTimestamp := time.Now().Unix()
workingList := make(map[string]*MetricData)
chanSize := len(s.chA)
for i := 0; i < chanSize; i++ {
split := strings.Fields(<-s.chA)
metricName := split[0]
value, err := strconv.ParseFloat(split[1], 64)
if err != nil {
s.lg.Println("Can not parse value of metric ", metricName, ": ", split[1])
continue
}
_, metricExist := workingList[metricName]
if !metricExist {
workingList[metricName] = &MetricData{}
}
if strings.HasPrefix(metricName, s.conf.SumPrefix) {
workingList[metricName].value += value
} else if strings.HasPrefix(metricName, s.conf.AvgPrefix) {
workingList[metricName].value += value
workingList[metricName].amount++
} else if strings.HasPrefix(metricName, s.conf.MinPrefix) {
if !metricExist {
workingList[metricName].value = value
} else if workingList[metricName].value > value {
workingList[metricName].value = value
}
} else if strings.HasPrefix(metricName, s.conf.MaxPrefix) {
if workingList[metricName].value < value {
workingList[metricName].value = value
}
}
}
/*
We may have a problem, that working_list size will be bigger than main buffer/space in it.
But then go suppose to block appending into buffer and wait until space will be free.
I am not sure if we need to check free space of main buffer here...
*/
for metricName, metricData := range workingList {
value := metricData.value
var prefix string
if strings.HasPrefix(metricName, s.conf.SumPrefix) {
prefix = s.conf.SumPrefix
} else if strings.HasPrefix(metricName, s.conf.AvgPrefix) {
value = metricData.value / float64(metricData.amount)
prefix = s.conf.AvgPrefix
} else if strings.HasPrefix(metricName, s.conf.MinPrefix) {
prefix = s.conf.MinPrefix
} else if strings.HasPrefix(metricName, s.conf.MaxPrefix) {
prefix = s.conf.MaxPrefix
}
select {
case s.ch <- fmt.Sprintf("%s %.2f %d", strings.Replace(metricName, prefix, "", -1), value, aggrTimestamp):
default:
s.lg.Printf("Too many metrics in the main queue (%d). I can not append sum metrics", len(s.ch))
s.mon.dropped++
}
}
}
}
/*
Validate metrics list
Find proper channel for metric
Check overflow of the channel
Put metric in a proper channel
*/
func (s Server) cleanAndUseIncomingData(metrics []string) {
for _, metric := range metrics {
if s.aM.MatchString(metric) {
if s.aggrRegexp.MatchString(metric) {
select {
case s.chA <- metric:
default:
s.mon.dropped++
}
} else {
select {
case s.ch <- metric:
default:
s.mon.dropped++
}
}
} else {
if metric != "" {
s.mon.invalid++
s.lg.Printf("Removing bad metric '%s' from the list", metric)
}
}
}
}
// Reading metrics from network
func (s Server) handleRequest(conn net.Conn) {
connbuf := bufio.NewReader(conn)
defer conn.Close()
for {
s.mon.got.net++
metric, err := connbuf.ReadString('\n')
// Even if error occurred we still put "metric" into analysis, cause it can be a valid metric, but without \n
s.cleanAndUseIncomingData([]string{strings.Replace(strings.Replace(metric, "\r", "", -1), "\n", "", -1)})
if err != nil {
conn.Close()
break
}
}
}
// Reading metrics from files in folder. This is a second way how to send metrics, except network
func (s Server) handleDirMetrics() {
for ; ; time.Sleep(time.Duration(s.conf.ClientSendInterval) * time.Second) {
files, err := ioutil.ReadDir(s.conf.MetricDir)
if err != nil {
panic(err.Error())
}
for _, f := range files {
results_list := readMetricsFromFile(s.conf.MetricDir + "/" + f.Name())
s.mon.got.dir += len(results_list)
s.cleanAndUseIncomingData(results_list)
}
}
}
func (s Server) runServer() {
// Listen for incoming connections.
l, err := net.Listen("tcp", s.conf.LocalBind)
if err != nil {
s.lg.Println("Failed to run server:", err.Error())
os.Exit(1)
} else {
s.lg.Println("Server is running")
}
defer l.Close()
// Run goroutine for reading metrics from metricDir
go s.handleDirMetrics()
// Run goroutine for aggr metrics with prefix
go s.aggrMetricsWithPrefix()
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
s.lg.Println("Error accepting: ", err.Error())
os.Exit(1)
}
// Handle connections in a new goroutine.
go s.handleRequest(conn)
}
}