-
Notifications
You must be signed in to change notification settings - Fork 488
/
main.go
558 lines (463 loc) · 14.6 KB
/
main.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package main
import (
"bytes"
"crypto/tls"
"errors"
"flag"
"fmt"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"reflect"
"time"
"github.com/go-test/deep"
jaeger_grpc "github.com/jaegertracing/jaeger/cmd/agent/app/reporter/grpc"
zaplogfmt "github.com/jsternberg/zap-logfmt"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"github.com/grafana/tempo/pkg/httpclient"
"github.com/grafana/tempo/pkg/model/trace"
"github.com/grafana/tempo/pkg/tempopb"
"github.com/grafana/tempo/pkg/util"
)
var (
prometheusListenAddress string
prometheusPath string
tempoQueryURL string
tempoPushURL string
tempoOrgID string
tempoWriteBackoffDuration time.Duration
tempoLongWriteBackoffDuration time.Duration
tempoReadBackoffDuration time.Duration
tempoSearchBackoffDuration time.Duration
tempoRetentionDuration time.Duration
tempoPushTLS bool
logger *zap.Logger
)
type traceMetrics struct {
incorrectResult int
missingSpans int
notFoundByID int
notFoundSearch int
notFoundTraceQL int
requested int
requestFailed int
notFoundSearchAttribute int
}
func init() {
flag.StringVar(&prometheusPath, "prometheus-path", "/metrics", "The path to publish Prometheus metrics to.")
flag.StringVar(&prometheusListenAddress, "prometheus-listen-address", ":80", "The address to listen on for Prometheus scrapes.")
flag.StringVar(&tempoQueryURL, "tempo-query-url", "", "The URL (scheme://hostname) at which to query Tempo.")
flag.StringVar(&tempoPushURL, "tempo-push-url", "", "The URL (scheme://hostname:port) at which to push traces to Tempo.")
flag.BoolVar(&tempoPushTLS, "tempo-push-tls", false, "Whether to use TLS when pushing spans to Tempo")
flag.StringVar(&tempoOrgID, "tempo-org-id", "", "The orgID to query in Tempo")
flag.DurationVar(&tempoWriteBackoffDuration, "tempo-write-backoff-duration", 15*time.Second, "The amount of time to pause between write Tempo calls")
flag.DurationVar(&tempoLongWriteBackoffDuration, "tempo-long-write-backoff-duration", 1*time.Minute, "The amount of time to pause between long write Tempo calls")
flag.DurationVar(&tempoReadBackoffDuration, "tempo-read-backoff-duration", 30*time.Second, "The amount of time to pause between read Tempo calls")
flag.DurationVar(&tempoSearchBackoffDuration, "tempo-search-backoff-duration", 60*time.Second, "The amount of time to pause between search Tempo calls. Set to 0s to disable search.")
flag.DurationVar(&tempoRetentionDuration, "tempo-retention-duration", 336*time.Hour, "The block retention that Tempo is using")
}
func main() {
flag.Parse()
config := zap.NewDevelopmentEncoderConfig()
logger = zap.New(zapcore.NewCore(
zaplogfmt.NewEncoder(config),
os.Stdout,
zapcore.DebugLevel,
))
logger.Info("Tempo Vulture starting")
actualStartTime := time.Now()
startTime := actualStartTime
tickerWrite := time.NewTicker(tempoWriteBackoffDuration)
r := rand.New(rand.NewSource(actualStartTime.Unix()))
var tickerRead *time.Ticker
if tempoReadBackoffDuration > 0 {
tickerRead = time.NewTicker(tempoReadBackoffDuration)
}
var tickerSearch *time.Ticker
if tempoSearchBackoffDuration > 0 {
tickerSearch = time.NewTicker(tempoSearchBackoffDuration)
}
if tickerRead == nil && tickerSearch == nil {
log.Fatalf("at least one of tempo-search-backoff-duration or tempo-read-backoff-duration must be set")
}
interval := tempoWriteBackoffDuration
ready := func(info *util.TraceInfo, now time.Time) bool {
// Don't attempt to read on the first itteration if we can't reasonably
// expect the write loop to have fired yet. Double the duration here to
// avoid a race.
if info.Timestamp().Before(actualStartTime.Add(2 * tempoWriteBackoffDuration)) {
return false
}
return info.Ready(now, tempoWriteBackoffDuration, tempoLongWriteBackoffDuration)
}
// Write
go func() {
client, err := newJaegerGRPCClient(tempoPushURL)
if err != nil {
panic(err)
}
for now := range tickerWrite.C {
timestamp := now.Round(interval)
info := util.NewTraceInfo(timestamp, tempoOrgID)
log := logger.With(
zap.String("org_id", tempoOrgID),
zap.Int64("seed", info.Timestamp().Unix()),
)
log.Info("sending trace")
err := info.EmitBatches(client)
if err != nil {
metricErrorTotal.Inc()
}
queueFutureBatches(client, info)
}
}()
// Read
if tickerRead != nil {
go func() {
for now := range tickerRead.C {
var seed time.Time
startTime, seed = selectPastTimestamp(startTime, now, interval, tempoRetentionDuration, r)
log := logger.With(
zap.String("org_id", tempoOrgID),
zap.Int64("seed", seed.Unix()),
)
info := util.NewTraceInfo(seed, tempoOrgID)
// Don't query for a trace we don't expect to be complete
if !ready(info, now) {
continue
}
client := httpclient.New(tempoQueryURL, tempoOrgID)
// query the trace
queryMetrics, err := queryTrace(client, info)
if err != nil {
metricErrorTotal.Inc()
log.Error("query for metrics failed",
zap.Error(err),
)
}
pushMetrics(queryMetrics)
}
}()
}
// Search
if tickerSearch != nil {
go func() {
for now := range tickerSearch.C {
_, seed := selectPastTimestamp(startTime, now, interval, tempoRetentionDuration, r)
log := logger.With(
zap.String("org_id", tempoOrgID),
zap.Int64("seed", seed.Unix()),
)
info := util.NewTraceInfo(seed, tempoOrgID)
if !ready(info, now) {
continue
}
client := httpclient.New(tempoQueryURL, tempoOrgID)
// query a tag we expect the trace to be found within
searchMetrics, err := searchTag(client, seed)
if err != nil {
metricErrorTotal.Inc()
log.Error("search tag for metrics failed",
zap.Error(err),
)
}
pushMetrics(searchMetrics)
// traceql query
traceqlSearchMetrics, err := searchTraceql(client, seed)
if err != nil {
metricErrorTotal.Inc()
log.Error("traceql query for metrics failed",
zap.Error(err),
)
}
pushMetrics(traceqlSearchMetrics)
}
}()
}
http.Handle(prometheusPath, promhttp.Handler())
log.Fatal(http.ListenAndServe(prometheusListenAddress, nil))
}
func queueFutureBatches(client *jaeger_grpc.Reporter, info *util.TraceInfo) {
if info.LongWritesRemaining() == 0 {
return
}
log := logger.With(
zap.String("org_id", tempoOrgID),
zap.String("write_trace_id", info.HexID()),
zap.Int64("seed", info.Timestamp().Unix()),
zap.Int64("longWritesRemaining", info.LongWritesRemaining()),
)
log.Info("queueing future batches")
info.Done()
go func() {
time.Sleep(tempoLongWriteBackoffDuration)
log := logger.With(
zap.String("org_id", tempoOrgID),
zap.String("write_trace_id", info.HexID()),
zap.Int64("seed", info.Timestamp().Unix()),
zap.Int64("longWritesRemaining", info.LongWritesRemaining()),
)
log.Info("sending trace")
err := info.EmitBatches(client)
if err != nil {
log.Error("failed to queue batches",
zap.Error(err),
)
}
queueFutureBatches(client, info)
}()
}
func pushMetrics(metrics traceMetrics) {
metricTracesInspected.Add(float64(metrics.requested))
metricTracesErrors.WithLabelValues("incorrectresult").Add(float64(metrics.incorrectResult))
metricTracesErrors.WithLabelValues("missingspans").Add(float64(metrics.missingSpans))
metricTracesErrors.WithLabelValues("notfound_search").Add(float64(metrics.notFoundSearch))
metricTracesErrors.WithLabelValues("notfound_traceql").Add(float64(metrics.notFoundTraceQL))
metricTracesErrors.WithLabelValues("notfound_byid").Add(float64(metrics.notFoundByID))
metricTracesErrors.WithLabelValues("requestfailed").Add(float64(metrics.requestFailed))
metricTracesErrors.WithLabelValues("notfound_search_attribute").Add(float64(metrics.notFoundSearchAttribute))
}
func selectPastTimestamp(start, stop time.Time, interval, retention time.Duration, r *rand.Rand) (newStart, ts time.Time) {
oldest := stop.Add(-retention)
if oldest.After(start) {
newStart = oldest
} else {
newStart = start
}
ts = time.Unix(generateRandomInt(newStart.Unix(), stop.Unix(), r), 0)
return newStart.Round(interval), ts.Round(interval)
}
func newJaegerGRPCClient(endpoint string) (*jaeger_grpc.Reporter, error) {
// remove scheme and port
u, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
logger.Info("dialing grpc",
zap.String("endpoint", fmt.Sprintf("%s:14250", u.Host)),
)
var dialOpts []grpc.DialOption
if tempoPushTLS {
dialOpts = []grpc.DialOption{
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
InsecureSkipVerify: true,
})),
}
} else {
dialOpts = []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
}
// new jaeger grpc exporter
conn, err := grpc.Dial(u.Host+":14250", dialOpts...)
if err != nil {
return nil, err
}
return jaeger_grpc.NewReporter(conn, nil, logger), err
}
func generateRandomInt(min, max int64, r *rand.Rand) int64 {
min++
number := min + r.Int63n(max-min)
return number
}
func searchTag(client *httpclient.Client, seed time.Time) (traceMetrics, error) {
tm := traceMetrics{
requested: 1,
}
info := util.NewTraceInfo(seed, tempoOrgID)
hexID := info.HexID()
// Get the expected
expected, err := info.ConstructTraceFromEpoch()
if err != nil {
logger.Error("unable to construct trace from epoch", zap.Error(err))
return traceMetrics{}, err
}
traceInTraces := func(traceID string, traces []*tempopb.TraceSearchMetadata) bool {
for _, t := range traces {
equal, err := util.EqualHexStringTraceIDs(t.TraceID, traceID)
if err != nil {
logger.Error("error comparing trace IDs", zap.Error(err))
continue
}
if equal {
return true
}
}
return false
}
attr := util.RandomAttrFromTrace(expected)
if attr == nil {
tm.notFoundSearchAttribute++
return tm, fmt.Errorf("no search attr selected from trace")
}
logger := logger.With(
zap.Int64("seed", seed.Unix()),
zap.String("hexID", hexID),
zap.Duration("ago", time.Since(seed)),
zap.String("key", attr.Key),
zap.String("value", util.StringifyAnyValue(attr.Value)),
)
logger.Info("searching Tempo via search tag")
// Use the search API to find details about the expected trace. give an hour range
// around the seed.
start := seed.Add(-30 * time.Minute).Unix()
end := seed.Add(30 * time.Minute).Unix()
resp, err := client.SearchWithRange(fmt.Sprintf("%s=%s", attr.Key, util.StringifyAnyValue(attr.Value)), start, end)
if err != nil {
logger.Error(fmt.Sprintf("failed to search traces with tag %s: %s", attr.Key, err.Error()))
tm.requestFailed++
return tm, err
}
if !traceInTraces(hexID, resp.Traces) {
tm.notFoundSearch++
return tm, fmt.Errorf("trace %s not found in search response: %+v", hexID, resp.Traces)
}
return tm, nil
}
func searchTraceql(client *httpclient.Client, seed time.Time) (traceMetrics, error) {
tm := traceMetrics{
requested: 1,
}
info := util.NewTraceInfo(seed, tempoOrgID)
hexID := info.HexID()
// Get the expected
expected, err := info.ConstructTraceFromEpoch()
if err != nil {
logger.Error("unable to construct trace from epoch", zap.Error(err))
return traceMetrics{}, err
}
traceInTraces := func(traceID string, traces []*tempopb.TraceSearchMetadata) bool {
for _, t := range traces {
equal, err := util.EqualHexStringTraceIDs(t.TraceID, traceID)
if err != nil {
logger.Error("error comparing trace IDs", zap.Error(err))
continue
}
if equal {
return true
}
}
return false
}
attr := util.RandomAttrFromTrace(expected)
if attr == nil {
tm.notFoundSearchAttribute++
return tm, fmt.Errorf("no search attr selected from trace")
}
logger := logger.With(
zap.Int64("seed", seed.Unix()),
zap.String("hexID", hexID),
zap.Duration("ago", time.Since(seed)),
zap.String("key", attr.Key),
zap.String("value", util.StringifyAnyValue(attr.Value)),
)
logger.Info("searching Tempo via traceql")
start := seed.Add(-30 * time.Minute).Unix()
end := seed.Add(30 * time.Minute).Unix()
resp, err := client.SearchTraceQLWithRange(fmt.Sprintf(`{.%s = "%s"}`, attr.Key, util.StringifyAnyValue(attr.Value)), start, end)
if err != nil {
logger.Error(fmt.Sprintf("failed to search traces with traceql %s: %s", attr.Key, err.Error()))
tm.requestFailed++
return tm, err
}
if !traceInTraces(hexID, resp.Traces) {
tm.notFoundTraceQL++
return tm, fmt.Errorf("trace %s not found in search traceql response: %+v", hexID, resp.Traces)
}
return tm, nil
}
func queryTrace(client *httpclient.Client, info *util.TraceInfo) (traceMetrics, error) {
tm := traceMetrics{
requested: 1,
}
hexID := info.HexID()
logger := logger.With(
zap.Int64("seed", info.Timestamp().Unix()),
zap.String("hexID", hexID),
zap.Duration("ago", time.Since(info.Timestamp())),
)
logger.Info("querying Tempo")
trace, err := client.QueryTrace(hexID)
if err != nil {
if errors.Is(err, util.ErrTraceNotFound) {
tm.notFoundByID++
} else {
tm.requestFailed++
}
logger.Error("error querying Tempo", zap.Error(err))
return tm, err
}
if len(trace.Batches) == 0 {
logger.Error("trace contains 0 batches")
tm.notFoundByID++
return tm, nil
}
// iterate through
if hasMissingSpans(trace) {
logger.Error("trace has missing spans")
tm.missingSpans++
return tm, nil
}
// Get the expected
expected, err := info.ConstructTraceFromEpoch()
if err != nil {
logger.Error("unable to construct trace from epoch", zap.Error(err))
return tm, err
}
match := equalTraces(expected, trace)
if !match {
tm.incorrectResult++
if diff := deep.Equal(expected, trace); diff != nil {
for _, d := range diff {
logger.Error("incorrect result",
zap.String("expected -> response", d),
)
}
}
return tm, nil
}
return tm, nil
}
func equalTraces(a, b *tempopb.Trace) bool {
trace.SortTraceAndAttributes(a)
trace.SortTraceAndAttributes(b)
return reflect.DeepEqual(a, b)
}
func hasMissingSpans(t *tempopb.Trace) bool {
// collect all parent span IDs
linkedSpanIDs := make([][]byte, 0)
for _, b := range t.Batches {
for _, ss := range b.ScopeSpans {
for _, s := range ss.Spans {
if len(s.ParentSpanId) > 0 {
linkedSpanIDs = append(linkedSpanIDs, s.ParentSpanId)
}
}
}
}
for _, id := range linkedSpanIDs {
found := false
B:
for _, b := range t.Batches {
for _, ss := range b.ScopeSpans {
for _, s := range ss.Spans {
if bytes.Equal(s.SpanId, id) {
found = true
break B
}
}
}
}
if !found {
return true
}
}
return false
}