-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
69 lines (57 loc) · 1.49 KB
/
processor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package omloader
import (
"io"
"github.com/prometheus/prometheus/prompb"
"go.uber.org/zap"
)
const processorBatchSize = 4096
const processorChannelSize = 8192
type Processor interface {
Process(reader io.Reader) error
AddMutator(mutator LabelMutatorFn)
}
type LabelMutatorFn func(labels []prompb.Label) []prompb.Label
type processor struct {
logger *zap.Logger
sender Sender
mutators []LabelMutatorFn
}
func NewProcessor(logger *zap.Logger, sender Sender) Processor {
return &processor{
logger: logger,
sender: sender,
}
}
func (p *processor) Process(reader io.Reader) error {
ch := make(chan *prompb.TimeSeries, processorChannelSize)
omReader := NewOpenMetricsReader(reader, ch)
go func() {
defer close(ch)
if err := omReader.Read(); err != nil {
p.logger.Error("error reading file", zap.Error(err))
}
}()
// TODO(herko): [perf] extend processor to group time series by labes and send it in batches / in parallel
batch := make([]prompb.TimeSeries, 0, processorBatchSize)
for ts := range ch {
for _, mutator := range p.mutators {
ts.Labels = mutator(ts.Labels)
}
batch = append(batch, *ts)
if len(batch) == processorBatchSize {
if err := p.sender.Send(batch); err != nil {
return err
}
batch = make([]prompb.TimeSeries, 0, processorBatchSize)
}
}
if len(batch) > 0 {
if err := p.sender.Send(batch); err != nil {
return err
}
}
return nil
}
func (p *processor) AddMutator(mutator LabelMutatorFn) {
p.mutators = append(p.mutators, mutator)
}