Skip to content

Commit

Permalink
Merge pull request #13330 from influxdata/dn-backport-1_7-fix-prom-ap…
Browse files Browse the repository at this point in the history
…i-panic

fix #10595: fix panic in Prometheus read API
  • Loading branch information
dgnorton authored Apr 12, 2019
2 parents 1f8dbc9 + e82afae commit 001da28
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions services/httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,25 @@ func (h *Handler) servePromRead(w http.ResponseWriter, r *http.Request, user met
return
}

respond := func(resp *remote.ReadResponse) {
data, err := proto.Marshal(resp)
if err != nil {
h.httpError(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/x-protobuf")
w.Header().Set("Content-Encoding", "snappy")

compressed = snappy.Encode(nil, data)
if _, err := w.Write(compressed); err != nil {
h.httpError(w, err.Error(), http.StatusInternalServerError)
return
}

atomic.AddInt64(&h.stats.QueryRequestBytesTransmitted, int64(len(compressed)))
}

ctx := context.Background()
rs, err := h.Store.Read(ctx, readRequest)
if err != nil {
Expand All @@ -1080,6 +1099,12 @@ func (h *Handler) servePromRead(w http.ResponseWriter, r *http.Request, user met
resp := &remote.ReadResponse{
Results: []*remote.QueryResult{{}},
}

if rs == nil {
respond(resp)
return
}

for rs.Next() {
cur := rs.Cursor()
if cur == nil {
Expand Down Expand Up @@ -1137,22 +1162,8 @@ func (h *Handler) servePromRead(w http.ResponseWriter, r *http.Request, user met
)
}
}
data, err := proto.Marshal(resp)
if err != nil {
h.httpError(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/x-protobuf")
w.Header().Set("Content-Encoding", "snappy")

compressed = snappy.Encode(nil, data)
if _, err := w.Write(compressed); err != nil {
h.httpError(w, err.Error(), http.StatusInternalServerError)
return
}

atomic.AddInt64(&h.stats.QueryRequestBytesTransmitted, int64(len(compressed)))
respond(resp)
}

func (h *Handler) serveFluxQuery(w http.ResponseWriter, r *http.Request, user meta.User) {
Expand Down

0 comments on commit 001da28

Please sign in to comment.