forked from grafana/loki
/
comparator.go
282 lines (260 loc) · 7.69 KB
/
comparator.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package comparator
import (
"fmt"
"io"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/grafana/loki/pkg/canary/reader"
)
const (
ErrOutOfOrderEntry = "out of order entry %s was received before entries: %v\n"
ErrEntryNotReceivedWs = "websocket failed to receive entry %v within %f seconds\n"
ErrEntryNotReceived = "failed to receive entry %v within %f seconds\n"
ErrDuplicateEntry = "received a duplicate entry for ts %v\n"
ErrUnexpectedEntry = "received an unexpected entry with ts %v\n"
)
var (
totalEntries = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "loki_canary",
Name: "total_entries",
Help: "counts log entries written to the file",
})
outOfOrderEntries = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "loki_canary",
Name: "out_of_order_entries",
Help: "counts log entries received with a timestamp more recent than the others in the queue",
})
wsMissingEntries = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "loki_canary",
Name: "websocket_missing_entries",
Help: "counts log entries not received within the maxWait duration via the websocket connection",
})
missingEntries = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "loki_canary",
Name: "missing_entries",
Help: "counts log entries not received within the maxWait duration via both websocket and direct query",
})
unexpectedEntries = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "loki_canary",
Name: "unexpected_entries",
Help: "counts a log entry received which was not expected (e.g. received after reported missing)",
})
duplicateEntries = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "loki_canary",
Name: "duplicate_entries",
Help: "counts a log entry received more than one time",
})
responseLatency prometheus.Histogram
)
type Comparator struct {
entMtx sync.Mutex
w io.Writer
entries []*time.Time
ackdEntries []*time.Time
maxWait time.Duration
pruneInterval time.Duration
confirmAsync bool
sent chan time.Time
recv chan time.Time
rdr reader.LokiReader
quit chan struct{}
done chan struct{}
}
func NewComparator(writer io.Writer, maxWait time.Duration, pruneInterval time.Duration,
buckets int, sentChan chan time.Time, receivedChan chan time.Time, reader reader.LokiReader, confirmAsync bool) *Comparator {
c := &Comparator{
w: writer,
entries: []*time.Time{},
maxWait: maxWait,
pruneInterval: pruneInterval,
confirmAsync: confirmAsync,
sent: sentChan,
recv: receivedChan,
rdr: reader,
quit: make(chan struct{}),
done: make(chan struct{}),
}
responseLatency = promauto.NewHistogram(prometheus.HistogramOpts{
Namespace: "loki_canary",
Name: "response_latency",
Help: "is how long it takes for log lines to be returned from Loki in seconds.",
Buckets: prometheus.ExponentialBuckets(0.5, 2, buckets),
})
go c.run()
return c
}
func (c *Comparator) Stop() {
if c.quit != nil {
close(c.quit)
<-c.done
c.quit = nil
}
}
func (c *Comparator) entrySent(time time.Time) {
c.entMtx.Lock()
defer c.entMtx.Unlock()
c.entries = append(c.entries, &time)
totalEntries.Inc()
}
// entryReceived removes the received entry from the buffer if it exists, reports on out of order entries received
func (c *Comparator) entryReceived(ts time.Time) {
c.entMtx.Lock()
defer c.entMtx.Unlock()
// Output index
k := 0
matched := false
for i, e := range c.entries {
if ts.Equal(*e) {
matched = true
// If this isn't the first item in the list we received it out of order
if i != 0 {
outOfOrderEntries.Inc()
_, _ = fmt.Fprintf(c.w, ErrOutOfOrderEntry, e, c.entries[:i])
}
responseLatency.Observe(time.Since(ts).Seconds())
// Put this element in the acknowledged entries list so we can use it to check for duplicates
c.ackdEntries = append(c.ackdEntries, c.entries[i])
// Do not increment output index, effectively causing this element to be dropped
} else {
// If the current index doesn't match the output index, update the array with the correct position
if i != k {
c.entries[k] = c.entries[i]
}
k++
}
}
if !matched {
duplicate := false
for _, e := range c.ackdEntries {
if ts.Equal(*e) {
duplicate = true
duplicateEntries.Inc()
_, _ = fmt.Fprintf(c.w, ErrDuplicateEntry, ts.UnixNano())
break
}
}
if !duplicate {
_, _ = fmt.Fprintf(c.w, ErrUnexpectedEntry, ts.UnixNano())
unexpectedEntries.Inc()
}
}
// Nil out the pointers to any trailing elements which were removed from the slice
for i := k; i < len(c.entries); i++ {
c.entries[i] = nil // or the zero value of T
}
c.entries = c.entries[:k]
}
func (c *Comparator) Size() int {
c.entMtx.Lock()
defer c.entMtx.Unlock()
return len(c.entries)
}
func (c *Comparator) run() {
t := time.NewTicker(c.pruneInterval)
defer func() {
t.Stop()
close(c.done)
}()
for {
select {
case e := <-c.recv:
c.entryReceived(e)
case e := <-c.sent:
c.entrySent(e)
case <-t.C:
c.pruneEntries()
case <-c.quit:
return
}
}
}
func (c *Comparator) pruneEntries() {
c.entMtx.Lock()
defer c.entMtx.Unlock()
missing := []*time.Time{}
k := 0
for i, e := range c.entries {
// If the time is outside our range, assume the entry has been lost report and remove it
if e.Before(time.Now().Add(-c.maxWait)) {
missing = append(missing, e)
wsMissingEntries.Inc()
_, _ = fmt.Fprintf(c.w, ErrEntryNotReceivedWs, e.UnixNano(), c.maxWait.Seconds())
} else {
if i != k {
c.entries[k] = c.entries[i]
}
k++
}
}
// Nil out the pointers to any trailing elements which were removed from the slice
for i := k; i < len(c.entries); i++ {
c.entries[i] = nil // or the zero value of T
}
c.entries = c.entries[:k]
if len(missing) > 0 {
if c.confirmAsync {
go c.confirmMissing(missing)
} else {
c.confirmMissing(missing)
}
}
// Prune the acknowledged list, remove anything older than our maxwait
k = 0
for i, e := range c.ackdEntries {
if e.Before(time.Now().Add(-c.maxWait)) {
// Do nothing, if we don't increment the output index k, this will be dropped
} else {
if i != k {
c.ackdEntries[k] = c.ackdEntries[i]
}
k++
}
}
// Nil out the pointers to any trailing elements which were removed from the slice
for i := k; i < len(c.ackdEntries); i++ {
c.ackdEntries[i] = nil // or the zero value of T
}
c.ackdEntries = c.ackdEntries[:k]
}
func (c *Comparator) confirmMissing(missing []*time.Time) {
// Because we are querying loki timestamps vs the timestamp in the log,
// make the range +/- 10 seconds to allow for clock inaccuracies
start := *missing[0]
start = start.Add(-10 * time.Second)
end := *missing[len(missing)-1]
end = end.Add(10 * time.Second)
recvd, err := c.rdr.Query(start, end)
if err != nil {
_, _ = fmt.Fprintf(c.w, "error querying loki: %s", err)
return
}
k := 0
for i, m := range missing {
found := false
for _, r := range recvd {
if (*m).Equal(r) {
// Entry was found in loki, this can be dropped from the list of missing
// which is done by NOT incrementing the output index k
found = true
}
}
if !found {
// Item is still missing
if i != k {
missing[k] = missing[i]
}
k++
}
}
// Nil out the pointers to any trailing elements which were removed from the slice
for i := k; i < len(missing); i++ {
missing[i] = nil // or the zero value of T
}
missing = missing[:k]
for _, e := range missing {
missingEntries.Inc()
_, _ = fmt.Fprintf(c.w, ErrEntryNotReceived, e.UnixNano(), c.maxWait.Seconds())
}
}