Skip to content

Commit

Permalink
cmd/tsdbrelay: fix metadata not working, add metrics for relay errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gbrayut committed Aug 16, 2016
1 parent d5f0681 commit a641035
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
35 changes: 26 additions & 9 deletions cmd/tsdbrelay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var (
denormalizationRules map[string]*denormalize.DenormalizationRule

relayPutUrls []string

tags = opentsdb.TagSet{}
)

func main() {
Expand Down Expand Up @@ -130,14 +132,25 @@ func main() {
if err = collect.Init(collectUrl, "tsdbrelay"); err != nil {
log.Fatal(err)
}
if err := metadata.Init(collectUrl, false); err != nil {
log.Fatal(err)
}
// Make sure these get zeroed out instead of going unknown on restart
collect.Add("puts.relayed", tags, 0)
collect.Add("puts.error", tags, 0)
collect.Add("metadata.relayed", tags, 0)
collect.Add("metadata.error", tags, 0)
collect.Add("additional.puts.relayed", tags, 0)
collect.Add("additional.puts.error", tags, 0)
metadata.AddMetricMeta("tsdbrelay.puts.relayed", metadata.Counter, metadata.Count, "Number of successful puts relayed to opentsdb target")
metadata.AddMetricMeta("tsdbrelay.puts.error", metadata.Counter, metadata.Count, "Number of puts that could not be relayed to opentsdb target")
metadata.AddMetricMeta("tsdbrelay.metadata.relayed", metadata.Counter, metadata.Count, "Number of successful metadata puts relayed to bosun target")
metadata.AddMetricMeta("tsdbrelay.metadata.error", metadata.Counter, metadata.Count, "Number of metadata puts that could not be relayed to bosun target")
metadata.AddMetricMeta("tsdbrelay.additional.puts.relayed", metadata.Counter, metadata.Count, "Number of successful puts relayed to additional targets")
metadata.AddMetricMeta("tsdbrelay.additional.puts.error", metadata.Counter, metadata.Count, "Number of puts that could not be relayed to additional targets")
log.Fatal(http.ListenAndServe(*listenAddr, nil))
}

func init() {
metadata.AddMetricMeta("tsdbrelay.puts.relayed", metadata.Counter, metadata.Count, "Number of successful puts relayed")
metadata.AddMetricMeta("tsdbrelay.metadata.relayed", metadata.Counter, metadata.Count, "Number of successful metadata puts relayed")
}

func verbose(format string, a ...interface{}) {
if *logVerbose {
log.Printf(format, a...)
Expand Down Expand Up @@ -183,10 +196,11 @@ func (rp *relayProxy) relayPut(responseWriter http.ResponseWriter, r *http.Reque
rp.TSDBProxy.ServeHTTP(w, r)
if w.code/100 != 2 {
verbose("got status %d", w.code)
collect.Add("puts.error", tags, 1)
return
}
verbose("relayed to tsdb")
collect.Add("puts.relayed", opentsdb.TagSet{}, 1)
collect.Add("puts.relayed", tags, 1)
// Send to bosun in a separate go routine so we can end the source's request.
go func() {
body := bytes.NewBuffer(reader.buf.Bytes())
Expand Down Expand Up @@ -223,10 +237,12 @@ func (rp *relayProxy) relayPut(responseWriter http.ResponseWriter, r *http.Reque
resp, err := http.DefaultClient.Do(req)
if err != nil {
verbose("secondary relay error: %v", err)
collect.Add("puts.additional.error", tags, 1)
return
}
resp.Body.Close()
verbose("secondary relay success")
collect.Add("puts.additional.relayed", tags, 1)
}
}()
}
Expand Down Expand Up @@ -291,10 +307,11 @@ func (rp *relayProxy) relayMetadata(responseWriter http.ResponseWriter, r *http.
rp.BosunProxy.ServeHTTP(w, r)
if w.code != 204 {
verbose("got status %d", w.code)
collect.Add("metadata.error", tags, 1)
return
}
verbose("relayed metadata to bosun")
collect.Add("metadata.relayed", opentsdb.TagSet{}, 1)
collect.Add("metadata.relayed", tags, 1)
if r.Header.Get(relayHeader) != "" {
return
}
Expand All @@ -312,11 +329,11 @@ func (rp *relayProxy) relayMetadata(responseWriter http.ResponseWriter, r *http.
req.Header.Add(relayHeader, myHost)
resp, err := http.DefaultClient.Do(req)
if err != nil {
verbose("secondary relay error: %v", err)
verbose("secondary relay metadata error: %v", err)
return
}
resp.Body.Close()
verbose("secondary relay success")
verbose("secondary relay metadata success")
}
}()
}
Expand Down
4 changes: 4 additions & 0 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"reflect"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -198,6 +199,9 @@ func Init(u *url.URL, debug bool) error {
if err != nil {
return err
}
if strings.HasPrefix(mh.Host, ":") {
mh.Host = "localhost" + mh.Host
}
metahost = mh.String()
metadebug = debug
go collectMetadata()
Expand Down

0 comments on commit a641035

Please sign in to comment.