storj/cmd/statreceiver/parser.go
JT Olio 362f447d9f
cmd/statreceiver: lua-scriptable stat receiver (#636)
* cmd/statreceiver: lua-scriptable stat receiver

Change-Id: I3ce0fe3f1ef4b1f4f27eed90bac0e91cfecf22d7

* some updates

Change-Id: I7c3485adcda1278fce01ae077b4761b3ddb9fb7a

* more comments

Change-Id: I0bb22993cd934c3d40fc1da80d07e49e686b80dd

* linter fixes

Change-Id: Ied014304ecb9aadcf00a6b66ad28f856a428d150

* catch errors

Change-Id: I6e1920f1fd941e66199b30bc427285c19769fc70

* review feedback

Change-Id: I9d4051851eab18970c5f5ddcf4ff265508e541d3

* errorgroup improvements

Change-Id: I4699dda3022f0485fbb50c9dafe692d3921734ff

* too tricky

the previous thing was better for memory with lots of errors at a time
but https://play.golang.org/p/RweTMRjoSCt is too much of a foot gun

Change-Id: I23f0b3d77dd4288fcc20b3756a7110359576bf44
2018-12-11 11:24:31 -07:00

72 lines
1.4 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"log"
"sync"
"time"
"github.com/zeebo/admission/admproto"
)
const (
kb = 1024
)
// Parser is a PacketDest that sends data to a MetricDest
type Parser struct {
d MetricDest
f []*PacketFilter
scratch sync.Pool
}
// NewParser creates a Parser. It sends metrics to d, provided they pass all
// of the provided PacketFilters
func NewParser(d MetricDest, f ...*PacketFilter) *Parser {
return &Parser{
d: d, f: f,
scratch: sync.Pool{
New: func() interface{} {
var x [10 * kb]byte
return &x
},
}}
}
// Packet implements PacketDest
func (p *Parser) Packet(data []byte, ts time.Time) (err error) {
data, err = admproto.CheckChecksum(data)
if err != nil {
return err
}
scratch := p.scratch.Get().(*[10 * kb]byte)
defer p.scratch.Put(scratch)
r := admproto.NewReaderWith((*scratch)[:])
data, appb, instb, err := r.Begin(data)
if err != nil {
return err
}
app, inst := string(appb), string(instb)
for _, f := range p.f {
if !f.Filter(app, inst) {
return nil
}
}
var key []byte
var value float64
for len(data) > 0 {
data, key, value, err = r.Next(data)
if err != nil {
return err
}
err = p.d.Metric(app, inst, key, value, ts)
if err != nil {
log.Printf("failed to write metric: %v", err)
continue
}
}
return nil
}