func report(inbox chan *LogRequest, outbox chan *store.Bucket, register map[store.BKey]*store.Bucket) { for _ = range time.Tick(time.Second * 2) { utils.MeasureI("web.inbox", int64(len(inbox))) utils.MeasureI("web.register", int64(len(register))) utils.MeasureI("web.outbox", int64(len(outbox))) } }
func report(i chan *bucket.Bucket, l chan *LM, o chan []*LM) { for _ = range time.Tick(time.Second * 5) { utils.MeasureI("librato_outlet.inbox", int64(len(i))) utils.MeasureI("librato_outlet.lms", int64(len(l))) utils.MeasureI("librato_outlet.outbox", int64(len(o))) } }
// Keep an eye on the lenghts of our bufferes. If they are maxed out, something // is going wrong. func (r *Receiver) report() { for _ = range time.Tick(time.Second * 2) { utils.MeasureI("receiver.inbox", int64(len(r.Inbox))) utils.MeasureI("receiver.register", int64(len(r.Register.m))) utils.MeasureI("receiver.outbox", int64(len(r.Outbox))) } }
func report(i chan *store.Bucket, l chan LM, o chan []LM) { for _ = range time.Tick(time.Second * 5) { utils.MeasureI("librato.inbox", int64(len(i))) utils.MeasureI("librato.lms", int64(len(l))) utils.MeasureI("librato.outbox", int64(len(o))) } }
func (l *LibratoOutlet) Report() { for _ = range time.Tick(time.Second * 2) { utils.MeasureI("librato-outlet.inbox", "buckets", int64(len(l.Inbox))) utils.MeasureI("librato-outlet.conversions", "payloads", int64(len(l.Conversions))) utils.MeasureI("librato-outlet.outbox", "requests", int64(len(l.Outbox))) } }
// Keep an eye on the lenghts of our bufferes. If they are maxed out, something // is going wrong. func (r *Receiver) Report() { for _ = range time.Tick(time.Second * 2) { na := atomic.LoadUint64(&r.numBuckets) atomic.AddUint64(&r.numBuckets, -na) utils.MeasureI("receiver.buckets", "buckets", int64(na)) utils.MeasureI("receiver.inbox", "requests", int64(len(r.Inbox))) utils.MeasureI("receiver.register", "buckets", int64(len(r.Register.m))) utils.MeasureI("receiver.outbox", "buckets", int64(len(r.Outbox))) } }
func post(in <-chan []LM) { for metrics := range in { if len(metrics) < 1 { fmt.Printf("at=%q\n", "post.empty.metrics") continue } token := store.Token{Id: metrics[0].Token} token.Get() payload := LP{metrics} j, err := json.Marshal(payload) postBody := bytes.NewBuffer(j) if err != nil { utils.MeasureE("librato.json", err) continue } req, err := http.NewRequest("POST", libratoUrl, postBody) if err != nil { continue } req.Header.Add("Content-Type", "application/json") req.SetBasicAuth(token.User, token.Pass) resp, err := http.DefaultClient.Do(req) if err != nil { utils.MeasureE("librato-post", err) continue } if resp.StatusCode/100 != 2 { b, _ := ioutil.ReadAll(resp.Body) fmt.Printf("status=%d post-body=%s resp-body=%s\n", resp.StatusCode, postBody, b) } utils.MeasureI("librato.post", 1) resp.Body.Close() } }
func NewBucket(token string, rdr *bufio.Reader) ([]*Bucket, error) { var buckets []*Bucket lp := logplex.NewReader(rdr) for { packet, err := lp.ReadMsg() if err != nil { if err == io.EOF { break } fmt.Printf("at=logplex-error err=%s\n", err) return nil, err } d, err := encoding.ParseMsgData(packet.Msg) if err != nil { continue } name, ok := d["measure"] if !ok { continue } source, ok := d["source"] if !ok { source = "" } var val float64 tmpVal, ok := d["val"] if ok { val, err = strconv.ParseFloat(tmpVal, 64) if err != nil { fmt.Printf("at=error error=\"unable to parse val.\"\n") continue } } else { val = float64(1) } t, err := packet.Time() if err != nil { fmt.Printf("at=time-error error=%s\n", err) continue } m := &Bucket{} m.Token = token m.Time = utils.RoundTime(t, time.Minute) m.Name = name m.Source = source m.Vals = append(m.Vals, val) buckets = append(buckets, m) } utils.MeasureI("received-measurements", int64(len(buckets))) return buckets, nil }
func convert(in <-chan *store.Bucket, out chan<- LM) { for b := range in { b.Get() fmt.Printf("at=librato.process.bucket minute=%d name=%q\n", b.Time.Minute(), b.Name) out <- LM{Token: b.Token, Time: b.Time.Unix(), Source: b.Source, Name: b.Name + ".last", Val: ff(b.Last())} out <- LM{Token: b.Token, Time: b.Time.Unix(), Source: b.Source, Name: b.Name + ".min", Val: ff(b.Min())} out <- LM{Token: b.Token, Time: b.Time.Unix(), Source: b.Source, Name: b.Name + ".max", Val: ff(b.Max())} out <- LM{Token: b.Token, Time: b.Time.Unix(), Source: b.Source, Name: b.Name + ".mean", Val: ff(b.Mean())} out <- LM{Token: b.Token, Time: b.Time.Unix(), Source: b.Source, Name: b.Name + ".median", Val: ff(b.Median())} out <- LM{Token: b.Token, Time: b.Time.Unix(), Source: b.Source, Name: b.Name + ".perc95", Val: ff(b.P95())} out <- LM{Token: b.Token, Time: b.Time.Unix(), Source: b.Source, Name: b.Name + ".perc99", Val: ff(b.P99())} out <- LM{Token: b.Token, Time: b.Time.Unix(), Source: b.Source, Name: b.Name + ".count", Val: fi(b.Count())} out <- LM{Token: b.Token, Time: b.Time.Unix(), Source: b.Source, Name: b.Name + ".sum", Val: ff(b.Sum())} utils.MeasureI("librato.convert", 1) } }
func report(o chan *store.Bucket) { for _ = range time.Tick(time.Second * 5) { utils.MeasureI("postgres_outlet.outbox", int64(len(o))) } }
func post(outbox <-chan []*LM) { for metrics := range outbox { if len(metrics) < 1 { fmt.Printf("at=%q\n", "empty-metrics-error") continue } sampleMetric := metrics[0] token := token.Token{Id: sampleMetric.Token} // If a global user/token is provided, use the token for all metrics. // This enable a databaseless librato_outlet. if len(globalTokenUser) == 0 || len(globalTokenPass) == 0 { token.Get() } else { token.User = globalTokenUser token.Pass = globalTokenPass } payload := new(LP) payload.Gauges = metrics j, err := json.Marshal(payload) if err != nil { fmt.Printf("at=json-marshal-error error=%s\n", err) continue } if len(j) == 0 { fmt.Printf("at=empty-body-error body=%s\n", j) continue } fmt.Printf("at=%q name=%s source=%s len=%d\n", "post-metric", sampleMetric.Name, sampleMetric.Source, len(metrics)) maxRetry := 5 for i := 0; i <= maxRetry; i++ { b := bytes.NewBuffer(j) req, err := http.NewRequest("POST", libratoUrl, b) if err != nil { fmt.Printf("at=%q error=%s body=%s\n", "request-error", err, b) continue } req.Header.Add("Content-Type", "application/json") req.SetBasicAuth(token.User, token.Pass) resp, err := http.DefaultClient.Do(req) if err != nil { fmt.Printf("at=%q error=%s body=%s\n", "do-error", err, b) continue } if resp.StatusCode/100 == 2 { resp.Body.Close() utils.MeasureI("librato-http-post", 1) break } else { resp.Body.Close() if i == maxRetry { fmt.Printf("at=%q status=%d\n", "librato-status-error", resp.StatusCode) } } } } }