-
Notifications
You must be signed in to change notification settings - Fork 538
Expand file tree
/
Copy pathbatch.go
More file actions
188 lines (159 loc) · 5.12 KB
/
batch.go
File metadata and controls
188 lines (159 loc) · 5.12 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package client
import (
"errors"
"fmt"
"slices"
"strconv"
"strings"
"time"
"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
"github.com/grafana/loki/pkg/push"
"github.com/prometheus/common/model"
"github.com/grafana/alloy/internal/component/common/loki"
)
var (
errMaxStreamsLimitExceeded = errors.New("streams limit exceeded")
)
// SentDataMarkerHandler is a slice of the MarkerHandler interface, that the batch interacts with to report the event that
// all data in the batch has been delivered or a client failed to do so.
type SentDataMarkerHandler interface {
UpdateSentData(segmentId, dataCount int)
}
// batch holds pending log streams waiting to be sent to Loki, and it's used
// to reduce the number of push requests to Loki aggregating multiple log streams
// and entries in a single batch request. In case of multi-tenant Promtail, log
// streams for each tenant are stored in a dedicated batch.
type batch struct {
streams map[string]*push.Stream
// totalBytes holds the total amounts of bytes, across the log lines in this batch.
totalBytes int
createdAt time.Time
maxStreams int
// segmentCounter tracks the amount of entries for each segment present in this batch.
segmentCounter map[int]int
}
func newBatch(maxStreams int, entries ...loki.Entry) *batch {
b := &batch{
streams: map[string]*push.Stream{},
totalBytes: 0,
createdAt: time.Now(),
maxStreams: maxStreams,
segmentCounter: map[int]int{},
}
// Add entries to the batch
for _, entry := range entries {
//never error here
_ = b.add(entry, 0)
}
return b
}
// add an entry to the batch. segmentNum is used to associate batch with a segment from WAL.
// If entry is added from non backed WAL client it can be anything and is unused.
func (b *batch) add(entry loki.Entry, segmentNum int) error {
b.totalBytes += entrySize(entry.Entry)
// Append the entry to an already existing stream (if any)
labels := labelsMapToString(entry.Labels)
if stream, ok := b.streams[labels]; ok {
stream.Entries = append(stream.Entries, entry.Entry)
b.countForSegment(segmentNum)
return nil
}
streams := len(b.streams)
if b.maxStreams > 0 && streams >= b.maxStreams {
return fmt.Errorf("%w, streams: %d exceeds limit: %d, stream: '%s'", errMaxStreamsLimitExceeded, streams, b.maxStreams, labels)
}
// Add the entry as a new stream
b.streams[labels] = &push.Stream{
Labels: labels,
Entries: []push.Entry{entry.Entry},
}
b.countForSegment(segmentNum)
return nil
}
// labelsMapToString encodes an entry's label set as a string, ignoring internal labels
func labelsMapToString(ls model.LabelSet) string {
var b strings.Builder
totalSize := 2
lstrs := make([]model.LabelName, 0, len(ls))
for l, v := range ls {
// skip internal labels
if strings.HasPrefix(string(l), "__") {
continue
}
lstrs = append(lstrs, l)
// guess size increase: 2 for `, ` between labels and 3 for the `=` and quotes around label value
totalSize += len(l) + 2 + len(v) + 3
}
b.Grow(totalSize)
b.WriteByte('{')
slices.Sort(lstrs)
for i, l := range lstrs {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(string(l))
b.WriteString(`=`)
b.WriteString(strconv.Quote(string(ls[l])))
}
b.WriteByte('}')
return b.String()
}
// sizeBytes returns the current batch size in bytes
func (b *batch) sizeBytes() int {
return b.totalBytes
}
// sizeBytesAfter returns the size of the batch after the input entry
// will be added to the batch itself
func (b *batch) sizeBytesAfter(entry push.Entry) int {
return b.totalBytes + entrySize(entry)
}
// age of the batch since its creation
func (b *batch) age() time.Duration {
return time.Since(b.createdAt)
}
// encode the batch as snappy-compressed push request, and returns
// the encoded bytes and the number of encoded entries
func (b *batch) encode() ([]byte, int, error) {
req, entriesCount := b.createPushRequest()
buf, err := proto.Marshal(req)
if err != nil {
return nil, 0, err
}
buf = snappy.Encode(nil, buf)
return buf, entriesCount, nil
}
// creates push request and returns it, together with number of entries
func (b *batch) createPushRequest() (*push.PushRequest, int) {
req := push.PushRequest{
Streams: make([]push.Stream, 0, len(b.streams)),
}
entriesCount := 0
for _, stream := range b.streams {
req.Streams = append(req.Streams, *stream)
entriesCount += len(stream.Entries)
}
return &req, entriesCount
}
// countForSegment tracks that one data item has been read from a certain WAL segment.
func (b *batch) countForSegment(segmentNum int) {
if curr, ok := b.segmentCounter[segmentNum]; ok {
b.segmentCounter[segmentNum] = curr + 1
return
}
b.segmentCounter[segmentNum] = 1
}
// reportAsSentData will report for all segments whose data is part of this batch, the amount of that data as sent to
// the provided SentDataMarkerHandler
func (b *batch) reportAsSentData(h SentDataMarkerHandler) {
for seg, data := range b.segmentCounter {
h.UpdateSentData(seg, data)
}
}
func entrySize(entry push.Entry) int {
structuredMetadataSize := 0
for _, label := range entry.StructuredMetadata {
structuredMetadataSize += label.Size()
}
return len(entry.Line) + structuredMetadataSize
}