forked from kanatohodets/carbonsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
290 lines (242 loc) · 7.96 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
// handle virt. namespace metric requests from carbon zipper
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"net/url"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"strings"
"sync"
"github.com/kanatohodets/carbonsearch/consumer"
"github.com/kanatohodets/carbonsearch/consumer/httpapi"
"github.com/kanatohodets/carbonsearch/consumer/kafka"
"github.com/kanatohodets/carbonsearch/database"
"github.com/kanatohodets/carbonsearch/tag"
"github.com/kanatohodets/carbonsearch/util"
pb "github.com/dgryski/carbonzipper/carbonzipperpb"
"github.com/gogo/protobuf/proto"
)
// BuildVersion is provided to be overridden at build time. Eg. go build -ldflags -X 'main.BuildVersion=...'
var BuildVersion = "(development build)"
var db *database.Database
var stats *util.Stats
var virtPrefix string
// TODO(btyler) convert tags to byte slices right away so hash functions don't need casting
func parseQuery(queryLimit int, query string) (map[string][]string, error) {
/*
parse something like this:
'virt.v1.server-state:live.server-hw:intel.lb-pool:www'
into a map of 'tags' like this:
{
"server": [ "server-state:live", "server-hw:intel"],
"lb": ["lb-pool:www"]
}
where a 'tag' is a complete "prefix-key:value" item, such as "server-state:live".
these will be used to search the "left" side of our indexes: tag -> [$join_key, $join_key...]
*/
validExp := strings.HasPrefix(query, virtPrefix)
if !validExp {
return nil, fmt.Errorf("main: the query is not a valid virtual metric (must start with %q): %s", virtPrefix, query)
}
raw := strings.TrimPrefix(query, virtPrefix)
//NOTE(btyler) v1 only supports (implicit) 'and': otherwise we need precedence rules and...yuck
// additionally, you can get 'or' by adding more metrics to your query
tags := strings.Split(raw, ".")
if len(tags) > queryLimit {
return nil, fmt.Errorf(
"parseQuery: max query size is %v, but this query has %v tags. try again with a smaller query",
queryLimit,
len(tags),
)
}
tagsByService := make(map[string][]string)
for _, queryTag := range tags {
service, _, err := tag.Parse(queryTag)
if err != nil {
return nil, err
}
stats.QueryTagsByService.Add(service, 1)
_, ok := tagsByService[service]
if !ok {
tagsByService[service] = []string{}
}
tagsByService[service] = append(tagsByService[service], queryTag)
}
return tagsByService, nil
}
func handleQuery(rawQuery string, query map[string][]string) (pb.GlobResponse, error) {
metrics, err := db.Query(query)
var result pb.GlobResponse
if err != nil {
return result, err
}
result.Name = &rawQuery
result.Matches = make([]*pb.GlobMatch, 0, len(metrics))
for _, metric := range metrics {
result.Matches = append(result.Matches, &pb.GlobMatch{Path: proto.String(metric), IsLeaf: proto.Bool(true)})
}
return result, nil
}
func findHandler(queryLimit int, w http.ResponseWriter, req *http.Request) {
uri, _ := url.ParseRequestURI(req.URL.RequestURI())
uriQuery := uri.Query()
stats.QueriesHandled.Add(1)
queries := uriQuery["query"]
if len(queries) != 1 {
err := fmt.Errorf("req validation: there must be exactly one 'query' url param")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
formats := uriQuery["format"]
if len(formats) != 1 {
err := fmt.Errorf("req validation: there must be exactly one 'format' url param")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
format := formats[0]
if format != "protobuf" && format != "json" {
err := fmt.Errorf("main: %q is not a recognized format: known formats are 'protobuf' and 'json'", format)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rawQuery := queries[0]
queryTags, err := parseQuery(queryLimit, rawQuery)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
result, err := handleQuery(rawQuery, queryTags)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if format == "protobuf" {
w.Header().Set("Content-Type", "application/x-protobuf")
b, _ := result.Marshal()
w.Write(b)
} else if format == "json" {
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
err = enc.Encode(result)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func main() {
configPath := flag.String("config", "config.yaml", "Path to the `config file`.")
blockingProfile := flag.String("blockProfile", "", "Path to `block profile output file`. Block profiler disabled if empty.")
cpuProfile := flag.String("cpuProfile", "", "Path to `cpu profile output file`. CPU profiler disabled if empty.")
virtPrefix = *flag.String("prefix", "virt.v1.", "Query prefix")
flag.Parse()
if *configPath == "" {
printUsageErrorAndExit("Can't run without a config file")
}
if *blockingProfile != "" {
f, err := os.Create(*blockingProfile)
if err != nil {
log.Fatal(err.Error())
}
runtime.SetBlockProfileRate(1)
defer f.Close()
defer pprof.Lookup("block").WriteTo(f, 1)
}
if *cpuProfile != "" {
f, err := os.Create(*cpuProfile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
type Config struct {
Port int `yaml:"port"`
QueryLimit int `yaml:"query_limit"`
ResultLimit int `yaml:"result_limit"`
Consumers map[string]string `yaml:"consumers"`
}
conf := &Config{}
err := util.ReadConfig(*configPath, conf)
if err != nil {
printErrorAndExit(1, "could not read config: %s", err)
}
if len(conf.Consumers) == 0 {
printErrorAndExit(1, "config doesn't have any consumers. carbonsearch won't have anything to search on. Take a peek in %q, see if it looks like it should", *configPath)
}
stats = util.InitStats()
wg := &sync.WaitGroup{}
db = database.New(conf.ResultLimit, stats)
quit := make(chan bool)
constructors := map[string]func(string) (consumer.Consumer, error){
"kafka": func(confPath string) (consumer.Consumer, error) {
c, err := kafka.New(confPath)
return c, err
},
"httpapi": func(confPath string) (consumer.Consumer, error) {
c, err := httpapi.New(confPath)
return c, err
},
}
consumers := []consumer.Consumer{}
for consumerName, consumerConfigPath := range conf.Consumers {
constructor, ok := constructors[consumerName]
if !ok {
printErrorAndExit(1, "carbonsearch doesn't know how to create consumer %q. talk to the authors, or remove %q from the list of consumers in %q", consumerName, consumerName, *configPath)
}
consumer, err := constructor(consumerConfigPath)
if err != nil {
printErrorAndExit(1, "could not create new %s consumer: %s", consumerName, err)
}
err = consumer.Start(wg, db)
if err != nil {
printErrorAndExit(1, "could not start %s consumer: %s", consumerName, err)
}
consumers = append(consumers, consumer)
}
go func() {
http.HandleFunc("/metrics/find/", func(w http.ResponseWriter, req *http.Request) {
findHandler(conf.QueryLimit, w, req)
})
portStr := fmt.Sprintf(":%d", conf.Port)
log.Println("Starting carbonsearch", BuildVersion)
log.Printf("listening on %s\n", portStr)
log.Println(http.ListenAndServe(portStr, nil))
}()
go func() {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Kill, os.Interrupt)
<-signals
log.Println("Shutting down...")
for _, consumer := range consumers {
err := consumer.Stop()
if err != nil {
log.Printf("Failed to close consumer %s: %s", consumer.Name(), err)
}
}
quit <- true
}()
wg.Wait()
}
func printErrorAndExit(code int, format string, values ...interface{}) {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", fmt.Sprintf(format, values...))
fmt.Fprintln(os.Stderr)
os.Exit(code)
}
func printUsageErrorAndExit(format string, values ...interface{}) {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", fmt.Sprintf(format, values...))
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Available command line options:")
flag.PrintDefaults()
os.Exit(64)
}