/
debug_http.go
165 lines (143 loc) · 3.85 KB
/
debug_http.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
package http
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
cmodel "github.com/Cepave/open-falcon-backend/common/model"
cutils "github.com/Cepave/open-falcon-backend/common/utils"
"github.com/Cepave/open-falcon-backend/modules/graph/api"
"github.com/Cepave/open-falcon-backend/modules/graph/g"
"github.com/Cepave/open-falcon-backend/modules/graph/store"
)
func configDebugRoutes() {
http.HandleFunc("/count", func(w http.ResponseWriter, r *http.Request) {
for i := 0; i < store.GraphItems.Size; i++ {
keys := store.GraphItems.KeysByIndex(i)
if len(keys) == 0 {
w.Write([]byte(fmt.Sprintf("%d\n", 0)))
return
}
oneHourAgo := time.Now().Unix() - 3600
count := 0
for _, ckey := range keys {
item := store.GraphItems.First(ckey)
if item == nil {
continue
}
if item.Timestamp > oneHourAgo {
count++
}
}
w.Write([]byte(fmt.Sprintf("%d\n", count)))
}
})
// 接收数据 endpoint metric ts step dstype value [tags]
http.HandleFunc("/api/recv/", func(w http.ResponseWriter, r *http.Request) {
urlParam := r.URL.Path[len("/api/recv/"):]
args := strings.Split(urlParam, "/")
argsLen := len(args)
if !(argsLen == 6 || argsLen == 7) {
RenderDataJson(w, "bad args")
return
}
endpoint := args[0]
metric := args[1]
ts, _ := strconv.ParseInt(args[2], 10, 64)
step, _ := strconv.ParseInt(args[3], 10, 32)
dstype := args[4]
value, _ := strconv.ParseFloat(args[5], 64)
tags := make(map[string]string)
if argsLen == 7 {
tags = cutils.DictedTagstring(args[6])
}
item := &cmodel.MetaData{
Endpoint: endpoint,
Metric: metric,
Timestamp: ts,
Step: step,
CounterType: dstype,
Value: value,
Tags: tags,
}
gitem, err := convert2GraphItem(item)
if err != nil {
RenderDataJson(w, err)
return
}
api.HandleItems([]*cmodel.GraphItem{gitem})
RenderDataJson(w, "ok")
})
http.HandleFunc("/v2/api/recv", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
if !(len(r.Form["e"]) > 0 && len(r.Form["m"]) > 0 && len(r.Form["v"]) > 0 &&
len(r.Form["ts"]) > 0 && len(r.Form["step"]) > 0 && len(r.Form["type"]) > 0) {
RenderDataJson(w, "bad args")
return
}
endpoint := r.Form["e"][0]
metric := r.Form["m"][0]
value, _ := strconv.ParseFloat(r.Form["v"][0], 64)
ts, _ := strconv.ParseInt(r.Form["ts"][0], 10, 64)
step, _ := strconv.ParseInt(r.Form["step"][0], 10, 32)
dstype := r.Form["type"][0]
tags := make(map[string]string)
if len(r.Form["t"]) > 0 {
tagstr := r.Form["t"][0]
tagVals := strings.Split(tagstr, ",")
for _, tag := range tagVals {
tagPairs := strings.Split(tag, "=")
if len(tagPairs) == 2 {
tags[tagPairs[0]] = tagPairs[1]
}
}
}
item := &cmodel.MetaData{
Endpoint: endpoint,
Metric: metric,
Timestamp: ts,
Step: step,
CounterType: dstype,
Value: value,
Tags: tags,
}
gitem, err := convert2GraphItem(item)
if err != nil {
RenderDataJson(w, err)
return
}
api.HandleItems([]*cmodel.GraphItem{gitem})
RenderDataJson(w, "ok")
})
}
func convert2GraphItem(d *cmodel.MetaData) (*cmodel.GraphItem, error) {
item := &cmodel.GraphItem{}
item.Endpoint = d.Endpoint
item.Metric = d.Metric
item.Tags = d.Tags
item.Timestamp = d.Timestamp
item.Value = d.Value
item.Step = int(d.Step)
if item.Step < g.MIN_STEP {
item.Step = g.MIN_STEP
}
item.Heartbeat = item.Step * 2
if d.CounterType == g.GAUGE {
item.DsType = d.CounterType
item.Min = "U"
item.Max = "U"
} else if d.CounterType == g.COUNTER {
item.DsType = g.DERIVE
item.Min = "0"
item.Max = "U"
} else if d.CounterType == g.DERIVE {
item.DsType = g.DERIVE
item.Min = "0"
item.Max = "U"
} else {
return item, fmt.Errorf("not_supported_counter_type")
}
item.Timestamp = item.Timestamp - item.Timestamp%int64(item.Step)
return item, nil
}