Skip to content

Commit

Permalink
fix wrong json body on /status endpoint (refs veepee-oss#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Damien PLENARD committed Aug 19, 2019
1 parent a21cdf8 commit a8c98e0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
12 changes: 6 additions & 6 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ go-build:
stage: build
image: vpgrp/golang:latest
before_script:
- mkdir -p ${GOPATH}/src/github.com/vente-privee
- ln -fsv ${CI_PROJECT_DIR} ${GOPATH}/src/github.com/vente-privee/influxdb-relay
- mkdir -p ${GOPATH}/src/github.com/veepee-moc
- ln -fsv ${CI_PROJECT_DIR} ${GOPATH}/src/github.com/veepee-moc/influxdb-relay
script:
- cd ${GOPATH}/src/github.com/vente-privee/influxdb-relay
- cd ${GOPATH}/src/github.com/veepee-moc/influxdb-relay
- go get
- go build -a -ldflags '-extldflags "-static"' -o influxdb-relay

Expand All @@ -52,11 +52,11 @@ go-tests:
stage: test
image: vpgrp/golang
before_script:
- mkdir -p ${GOPATH}/src/github.com/vente-privee
- ln -fsv ${CI_PROJECT_DIR} ${GOPATH}/src/github.com/vente-privee/influxdb-relay
- mkdir -p ${GOPATH}/src/github.com/veepee-moc
- ln -fsv ${CI_PROJECT_DIR} ${GOPATH}/src/github.com/veepee-moc/influxdb-relay
- go get github.com/stretchr/testify/assert
script:
- cd ${GOPATH}/src/github.com/vente-privee/influxdb-relay
- cd ${GOPATH}/src/github.com/veepee-moc/influxdb-relay
- go get
- go test ./...

Expand Down
16 changes: 11 additions & 5 deletions relay/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,17 @@ func jsonResponse(w http.ResponseWriter, r response) {
_, _ = w.Write(data)
}

type stats interface {

}

type poster interface {
post([]byte, string, string, string) (*responseData, error)
getStats() map[string]string
getStats() stats
}

type simpleStats struct {
location string
}

type simplePoster struct {
Expand All @@ -264,10 +272,8 @@ func newSimplePoster(location string, timeout time.Duration, skipTLSVerification
}
}

func (s *simplePoster) getStats() map[string]string {
v := make(map[string]string)
v["location"] = s.location
return v
func (s *simplePoster) getStats() stats {
return simpleStats{location: s.location}
}

func (s *simplePoster) post(buf []byte, query string, auth string, endpoint string) (*responseData, error) {
Expand Down
15 changes: 7 additions & 8 deletions relay/http_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package relay

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"sync"
Expand All @@ -13,18 +11,19 @@ import (
"github.com/influxdata/influxdb/models"
)

type status struct {
Status map[string]stats `json:"status"`
}

func (h *HTTP) handleStatus(w http.ResponseWriter, r *http.Request, _ time.Time) {
if r.Method == http.MethodGet || r.Method == http.MethodHead {
// old but gold
st := make(map[string]map[string]string)
st := status{Status: make(map[string]stats)}

for _, b := range h.backends {
st[b.name] = b.poster.getStats()
st.Status[b.name] = b.poster.getStats()
}

j, _ := json.Marshal(st)

jsonResponse(w, response{http.StatusOK, fmt.Sprintf("{\"status\": %s}", string(j))})
jsonResponse(w, response{http.StatusOK, st})
} else {
jsonResponse(w, response{http.StatusMethodNotAllowed, http.StatusText(http.StatusMethodNotAllowed)})
return
Expand Down
24 changes: 11 additions & 13 deletions relay/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package relay
import (
"bytes"
"net/http"
"strconv"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -51,12 +50,17 @@ func newRetryBuffer(size, batch int, max time.Duration, p poster) *retryBuffer {
return r
}

func (r *retryBuffer) getStats() map[string]string {
stats := make(map[string]string)
stats["buffering"] = strconv.FormatInt(int64(r.buffering), 10)
for k, v := range r.list.getStats() {
stats[k] = v
}
type retryStats struct {
buffering int64
maxSize int64
size int64
}

func (r *retryBuffer) getStats() stats {
stats := retryStats{}
stats.buffering = int64(r.buffering)
stats.maxSize = int64(r.list.maxSize)
stats.size = int64(r.list.size)
return stats
}

Expand Down Expand Up @@ -169,12 +173,6 @@ func newBufferList(maxSize, maxBatch int) *bufferList {
}
}

func (l *bufferList) getStats() map[string]string {
stats := make(map[string]string)
stats["size"] = strconv.FormatInt(int64(l.size), 10)
stats["maxSize"] = strconv.FormatInt(int64(l.maxSize), 10)
return stats
}

// Empty the buffer to drop any buffered query
// This allows to flush 'impossible' queries which loop infinitely
Expand Down

0 comments on commit a8c98e0

Please sign in to comment.