Skip to content

Commit

Permalink
Support completer queries from SearchBackend
Browse files Browse the repository at this point in the history
This allows the SearchBackend to return query completions for virtual queries:
e.g. virt.v1.*.servers-stat* returns ["virt.v1.*.servers-status:"].

Why test for running a completion query using 'HasSuffix(query, "*")' rather
than 'format == "completer"? The reason is that graphite-web translates
format=completer from the javascript into format=json and a standard 'find'
request, and it is this standard 'find' request which reaches carbonzipper.

This shuffles the find response encoding bits into a function so that the
search completer case can early-out.
  • Loading branch information
kanatohodets committed Dec 2, 2016
1 parent 72d5c25 commit 64a4933
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ func findHandler(w http.ResponseWriter, req *http.Request) {

Metrics.FindRequests.Add(1)

queries := []string{req.FormValue("query")}
originalQuery := req.FormValue("query")
queries := []string{originalQuery}

rewrite, _ := url.ParseRequestURI(req.URL.RequestURI())
v := rewrite.Query()
Expand All @@ -314,6 +315,18 @@ func findHandler(w http.ResponseWriter, req *http.Request) {

if searchConfigured && strings.HasPrefix(queries[0], Config.SearchPrefix) {
Metrics.SearchRequests.Add(1)
// 'completer' requests are translated into standard Find requests with
// a trailing '*' by graphite-web
if strings.HasSuffix(queries[0], "*") {
searchCompleterResponse := multiGet([]string{Config.SearchBackend}, rewrite.RequestURI())
matches, _ := findUnpackPB(nil, searchCompleterResponse)
// this is a completer request, and so we should return the set of
// virtual metrics returned by carbonsearch verbatim, rather than trying
// to find them on the stores
encodeFindResponse(format, originalQuery, w, matches)
return
}

// Send query to SearchBackend. The result is []queries for StorageBackends
searchResponse := multiGet([]string{Config.SearchBackend}, rewrite.RequestURI())
m, _ := findUnpackPB(req, searchResponse)
Expand Down Expand Up @@ -362,11 +375,14 @@ func findHandler(w http.ResponseWriter, req *http.Request) {
}
}

encodeFindResponse(format, originalQuery, w, metrics)
}

func encodeFindResponse(format, query string, w http.ResponseWriter, metrics []*pb.GlobMatch) {
switch format {
case "protobuf":
w.Header().Set("Content-Type", contentTypeProtobuf)
var result pb.GlobResponse
query := req.FormValue("query")
result.Name = &query
result.Matches = metrics
b, _ := result.Marshal()
Expand Down

0 comments on commit 64a4933

Please sign in to comment.