logmower-shipper/cmd/sender.go

126 lines
3.0 KiB
Go
Raw Normal View History

package logmower
import (
2022-11-04 08:47:45 +00:00
"context"
2022-11-05 23:43:59 +00:00
"path/filepath"
"time"
"github.com/jtagcat/util"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.uber.org/zap"
)
var (
2022-11-04 08:47:45 +00:00
promShipperMongoSent = promauto.NewCounterVec(prom.CounterOpts{
Subsystem: "shipper",
2022-11-06 13:57:05 +00:00
Name: "sent",
2022-11-06 14:56:12 +00:00
Help: "Log lines successfully committed to mongo",
2022-11-04 08:47:45 +00:00
}, []string{"filename"})
promShipperMongoSentError = promauto.NewCounterVec(prom.CounterOpts{
Subsystem: "shipper",
2022-11-06 13:57:05 +00:00
Name: "mongo_errors",
Help: "Errors while submitting to mongo", // TODO:
2022-11-04 08:47:45 +00:00
}, []string{"filename"})
2022-11-06 01:43:18 +00:00
promLineParsingErr = promauto.NewCounterVec(prom.CounterOpts{
Subsystem: "shipper",
2022-11-06 13:57:05 +00:00
Name: "lines_parsing_errors",
2022-11-06 01:43:18 +00:00
Help: "Errors while parsing log line suffixes",
}, []string{"filename"})
2022-11-06 14:56:12 +00:00
promShipperQueued = promauto.NewGaugeVec(prom.GaugeOpts{
2022-11-04 08:47:45 +00:00
Subsystem: "shipper",
2022-11-06 13:57:05 +00:00
Name: "queued",
2022-11-06 14:56:12 +00:00
Help: "Log lines in queue to be batched and sent to mongo",
2022-11-04 08:47:45 +00:00
}, []string{"filename"})
promShipperSynced = promauto.NewGaugeVec(prom.GaugeOpts{
Subsystem: "shipper",
Name: "batches_synced",
Help: "All batches available have been sent to mongo",
}, []string{"filename"})
)
const (
2022-11-05 23:45:19 +00:00
MaxBatchItems = 100
MaxBatchTime = time.Second
)
func init() {
promauto.NewGaugeFunc(prom.GaugeOpts{
Subsystem: "shipper",
Name: "queue_size",
2022-11-04 08:47:45 +00:00
Help: "Submit queue size cap",
}, func() float64 {
return float64(SendQueueLimit)
})
promauto.NewGaugeFunc(prom.GaugeOpts{
Subsystem: "shipper",
2022-11-04 08:47:45 +00:00
Name: "batch_size",
Help: "batching size cap",
}, func() float64 {
2022-11-04 08:47:45 +00:00
return float64(MaxBatchItems)
})
2022-11-04 08:47:45 +00:00
promauto.NewGaugeFunc(prom.GaugeOpts{
Subsystem: "shipper",
Name: "batch_time",
Help: "batching delay cap",
}, func() float64 {
return float64(MaxBatchTime)
})
}
func (s *submitter) sender(name string, sendQueue <-chan mLog) {
baseName := filepath.Base(name)
batched := make(chan []mLog)
go func() {
2022-11-04 08:47:45 +00:00
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
2022-11-05 23:43:59 +00:00
baseName := filepath.Base(name)
2022-11-04 08:47:45 +00:00
for {
2022-11-06 14:56:12 +00:00
promShipperQueued.WithLabelValues(baseName).Set(float64(
2022-11-04 08:47:45 +00:00
len(sendQueue)))
timer := time.NewTimer(time.Second)
select {
case <-ctx.Done():
return
case <-timer.C:
}
}
}()
util.Batch(MaxBatchItems, MaxBatchTime, sendQueue, batched)
// returns when sendQueue is closed
}()
2022-11-04 08:47:45 +00:00
s.Add(1)
defer s.Done()
for {
2022-11-05 23:43:59 +00:00
promShipperSynced.WithLabelValues(baseName).Set(1)
2022-11-04 08:47:45 +00:00
batch, ok := <-batched
if !ok {
return
}
2022-11-05 23:43:59 +00:00
promShipperSynced.WithLabelValues(baseName).Set(0)
2022-11-04 08:47:45 +00:00
var batchBson []interface{} // mongo does not like typing
for _, b := range batch {
batchBson = append(batchBson, b.toBson())
}
2022-11-04 08:47:45 +00:00
result, err := s.db.InsertMany(mongoTimeoutCtx(context.Background()), batchBson, nil)
2022-11-06 13:23:20 +00:00
promShipperMongoSent.WithLabelValues(baseName).Add(float64(
len(result.InsertedIDs)))
if err != nil {
s.l.Error("mongo send returned error; TODO: add some selective retry here or something", zap.Error(err)) // TODO:
}
}
}