2022-10-11 07:55:22 +00:00
|
|
|
package logmower
|
|
|
|
|
|
|
|
import (
|
2022-11-04 08:47:45 +00:00
|
|
|
"context"
|
2022-11-05 23:43:59 +00:00
|
|
|
"path/filepath"
|
2022-10-11 07:55:22 +00:00
|
|
|
"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-06 16:41:10 +00:00
|
|
|
promShipperQueued = promauto.NewGaugeVec(prom.GaugeOpts{
|
2022-11-06 15:02:49 +00:00
|
|
|
Namespace: PrometheusPrefix,
|
2022-11-06 16:41:10 +00:00
|
|
|
// Subsystem: "shipper",
|
|
|
|
Name: "shipper_record", // "queued",
|
|
|
|
Help: "Log records in queue to be batched and sent to database",
|
2022-11-04 08:47:45 +00:00
|
|
|
}, []string{"filename"})
|
2022-11-06 16:41:10 +00:00
|
|
|
promShipperDbSent = promauto.NewCounterVec(prom.CounterOpts{
|
2022-11-06 15:02:49 +00:00
|
|
|
Namespace: PrometheusPrefix,
|
2022-11-06 16:41:10 +00:00
|
|
|
// Subsystem: "shipper",
|
|
|
|
Name: "record", // "sent",
|
|
|
|
Help: "Log records successfully committed to database",
|
2022-11-04 08:47:45 +00:00
|
|
|
}, []string{"filename"})
|
2022-11-06 16:41:10 +00:00
|
|
|
promShipperBatchSizeResult = promauto.NewHistogram(prom.HistogramOpts{
|
2022-11-06 15:02:49 +00:00
|
|
|
Namespace: PrometheusPrefix,
|
2022-11-06 16:41:10 +00:00
|
|
|
// Subsystem: "shipper",
|
|
|
|
Name: "bulk_submission_message", // "items_in_batch"
|
|
|
|
Help: "Batch size for database submissions",
|
|
|
|
Buckets: []float64{1, 5, 10, 50, 100, 500, 1000, 5000, 10000},
|
|
|
|
})
|
|
|
|
promShipperMongoSentError = promauto.NewCounterVec(prom.CounterOpts{
|
2022-11-06 15:02:49 +00:00
|
|
|
Namespace: PrometheusPrefix,
|
2022-11-06 16:41:10 +00:00
|
|
|
// Subsystem: "shipper",
|
|
|
|
Name: "insertion_error", // "errors",
|
|
|
|
Help: "Errors while submitting to database", // TODO:
|
2022-11-04 08:47:45 +00:00
|
|
|
}, []string{"filename"})
|
|
|
|
promShipperSynced = promauto.NewGaugeVec(prom.GaugeOpts{
|
2022-11-06 15:02:49 +00:00
|
|
|
Namespace: PrometheusPrefix,
|
2022-11-04 08:47:45 +00:00
|
|
|
Subsystem: "shipper",
|
|
|
|
Name: "batches_synced",
|
2022-11-06 16:41:10 +00:00
|
|
|
Help: "All batches available have been committed database (0 or 1)",
|
2022-11-04 08:47:45 +00:00
|
|
|
}, []string{"filename"})
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-11-06 16:41:10 +00:00
|
|
|
MaxBatchItems = 10000
|
|
|
|
MaxBatchTime = 5 * time.Second
|
2022-10-11 07:55:22 +00:00
|
|
|
)
|
|
|
|
|
2022-11-06 13:46:07 +00:00
|
|
|
func (s *submitter) sender(name string, sendQueue <-chan mLog) {
|
|
|
|
baseName := filepath.Base(name)
|
|
|
|
|
2022-10-11 07:55:22 +00:00
|
|
|
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)
|
2022-11-06 13:46:07 +00:00
|
|
|
// returns when sendQueue is closed
|
2022-10-11 07:55:22 +00:00
|
|
|
}()
|
|
|
|
|
2022-11-04 08:47:45 +00:00
|
|
|
s.Add(1)
|
|
|
|
defer s.Done()
|
|
|
|
|
2022-10-11 07:55:22 +00:00
|
|
|
for {
|
2022-11-05 23:43:59 +00:00
|
|
|
promShipperSynced.WithLabelValues(baseName).Set(1)
|
2022-11-04 08:47:45 +00:00
|
|
|
|
2022-10-11 07:55:22 +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
|
|
|
|
2022-10-11 07:55:22 +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 16:41:10 +00:00
|
|
|
promShipperDbSent.WithLabelValues(baseName).Add(float64(
|
2022-10-11 07:55:22 +00:00
|
|
|
len(result.InsertedIDs)))
|
|
|
|
|
|
|
|
if err != nil {
|
2022-11-06 16:41:10 +00:00
|
|
|
s.l.Error("submission to database", zap.Error(err)) // TODO: add some selective retry here or something
|
2022-10-11 07:55:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|