forked from sourcegraph/appdash
/
influxdb_store.go
1006 lines (899 loc) · 30.5 KB
/
influxdb_store.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package appdash
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/url"
"os"
"reflect"
"strings"
"sync"
"time"
influxDBClient "github.com/influxdata/influxdb/client"
influxDBServer "github.com/influxdata/influxdb/cmd/influxd/run"
influxDBModels "github.com/influxdata/influxdb/models"
influxDBErrors "github.com/influxdata/influxdb/services/meta"
"github.com/influxdata/influxdb/toml"
)
const (
defaultTracesPerPage int = 10 // Default number of traces per page.
releaseDBName string = "appdash" // InfluxDB release DB name.
schemasFieldName string = "schemas" // Span's measurement field name for schemas field.
schemasFieldSeparator string = "," // Span's measurement character separator for schemas field.
spanMeasurementName string = "spans" // InfluxDB container name for trace spans.
testDBName string = "appdash_test" // InfluxDB test DB name (will be deleted entirely in test mode).
)
type mode int
const (
releaseMode mode = iota // Default InfluxDBStore mode.
testMode // Used to setup InfluxDBStore for tests.
)
// Compile-time "implements" check.
var _ interface {
Store
Queryer
} = (*InfluxDBStore)(nil)
var (
errMultipleSeries = errors.New("unexpected multiple series")
zeroID string = ID(0).String() // zeroID is ID's zero value as string.
)
// pointFields -> influxDBClient.Point.Fields
type pointFields map[string]interface{}
type InfluxDBStore struct {
config *InfluxDBConfig
con *influxDBClient.Client // InfluxDB client connection.
dbName string // InfluxDB database name for this store.
clientTarget *url.URL // HTTP URL that the client should connect to,
// When set to `testMode` - `testDBName` will be dropped and created, so newly database is ready for tests.
server *influxDBServer.Server // InfluxDB API server.
tracesPerPage int // Number of traces per page.
batchMu sync.Mutex
batchSizeBytes int
batch []influxDBClient.Point
flusherStopChan chan struct{}
log *log.Logger
}
func (in *InfluxDBStore) Collect(id SpanID, anns ...Annotation) error {
// trace_id, span_id & parent_id are mostly used as part of the "where" part on queries so
// to have performant queries these are set as tags(InfluxDB indexes tags).
tags := map[string]string{
"trace_id": id.Trace.String(),
"span_id": id.Span.String(),
"parent_id": id.Parent.String(),
}
// Find the start and end time of the span.
var events []Event
if err := UnmarshalEvents(anns, &events); err != nil {
return err
}
var (
foundItems int
name string
duration time.Duration
)
for _, ev := range events {
switch v := ev.(type) {
case spanName:
foundItems++
name = v.Name
case TimespanEvent:
foundItems++
duration = v.End().Sub(v.Start())
}
}
// Annotations `anns` are set as fields(InfluxDB does not index fields).
fields := make(map[string]interface{}, len(anns))
for _, ann := range anns {
fields[ann.Key] = string(ann.Value)
}
// If we have span name and duration, set them as a tag and field.
if foundItems == 2 {
tags["name"] = name
fields["duration"] = float64(duration) / float64(time.Second)
}
// `schemasFieldName` field contains all the schemas found on `anns`.
// Eg. fields[schemasFieldName] = "HTTPClient,HTTPServer"
fields[schemasFieldName] = schemasFromAnnotations(anns)
// Tag values cannot contain newlines or they mess up the WRITE and cause
// errors.
//
// TODO: investigate why this is; possibly related to https://github.com/influxdata/influxdb/issues/3545
// which was only for fields (not tags).
for k, v := range tags {
tags[k] = strings.Replace(v, "\n", " ", -1)
}
p := &influxDBClient.Point{
Measurement: spanMeasurementName,
Tags: tags,
Fields: fields,
Time: time.Now().UTC(),
}
pointSizeBytes := pointMemoryUsage(reflect.ValueOf(p))
in.batchMu.Lock()
defer in.batchMu.Unlock()
if in.batchSizeBytes+pointSizeBytes > in.config.MaxBatchSizeBytes {
in.log.Println("InfluxDBStore: point batch entirely dropped (this should never happen, trace data will be missing)")
in.log.Printf("InfluxDBStore: batchSize:%v batchSizeBytes:%v + pointSize:%v\n", len(in.batch), in.batchSizeBytes, pointSizeBytes)
in.batch = nil
in.batchSizeBytes = 0
return ErrQueueDropped
}
in.batchSizeBytes += pointSizeBytes
in.batch = append(in.batch, *p)
return nil
}
// flush immediately sends all pending spans in the underlying batch to
// InfluxDB.
func (in *InfluxDBStore) flush() error {
// Grab what information we need and unlock quickly to avoid contention.
in.batchMu.Lock()
batch := in.batch
in.batch = nil
in.batchSizeBytes = 0
in.batchMu.Unlock()
if len(batch) == 0 {
return nil
}
// Write the batch to InfluxDB.
bps := influxDBClient.BatchPoints{
Points: batch,
Database: in.dbName,
}
_, writeErr := in.con.Write(bps)
return writeErr
}
// flusher constantly flushes batches to InfluxDB at an interval.
func (in *InfluxDBStore) flusher() {
in.flusherStopChan = make(chan struct{}, 1)
go func() {
for {
t := time.After(in.config.BatchFlushInterval)
select {
case <-t:
if err := in.flush(); err != nil {
in.log.Println("Flush:", err)
}
case <-in.flusherStopChan:
return // stop
}
}
}()
}
func (in *InfluxDBStore) Trace(id ID) (*Trace, error) {
trace := &Trace{}
q := fmt.Sprintf("SELECT * FROM spans WHERE trace_id='%s'", id)
result, err := in.executeOneQuery(q)
if err != nil {
return nil, err
}
if result.Err != nil {
return nil, result.Err
}
if len(result.Series) == 0 {
return nil, ErrTraceNotFound
}
// Expecting only one element, which contains the queried spans information.
if len(result.Series) > 1 {
return nil, errMultipleSeries
}
var (
rootSpanSet bool
children []*Trace
)
spans, err := spansFromRow(result.Series[0])
if err != nil {
return nil, err
}
// Iterate over spans to find and set `trace`'s root span & append children spans as sub-traces to `children` for later usage.
for _, span := range spans {
var isRootSpan bool
if span.ID.IsRoot() && rootSpanSet {
return nil, errors.New("unexpected multiple root spans")
}
if span.ID.IsRoot() && !rootSpanSet {
isRootSpan = true
}
if isRootSpan { // root span.
trace.Span = *span
rootSpanSet = true
} else { // children span.
children = append(children, &Trace{Span: *span})
}
}
if err := addChildren(trace, children); err != nil {
return nil, err
}
return trace, nil
}
func mustJSONFloat64(x interface{}) float64 {
n := x.(json.Number)
v, err := n.Float64()
if err != nil {
panic(err)
}
return v
}
func mustJSONInt64(x interface{}) int64 {
n := x.(json.Number)
v, err := n.Int64()
if err != nil {
panic(err)
}
return v
}
// Aggregate implements the Aggregator interface.
func (in *InfluxDBStore) Aggregate(start, end time.Duration) ([]*AggregatedResult, error) {
// Find the mean (average), minimum, maximum, std. deviation, and count of
// all spans. For details on how this works see the createContinuousQueries
// method.
q := `SELECT MEAN("mean"),MIN("min"),MAX("max"),MEAN("stddev"),SUM("count") from downsampled_spans`
q += fmt.Sprintf(
" WHERE time >= '%s' AND time <= '%s'",
time.Now().Add(start).UTC().Format(time.RFC3339Nano),
time.Now().Add(end).UTC().Format(time.RFC3339Nano),
)
q += ` GROUP BY "name"`
result, err := in.executeOneQuery(q)
if err != nil {
return nil, err
}
// Populate the results.
results := make([]*AggregatedResult, len(result.Series))
for i, row := range result.Series {
v := row.Values[0]
mean, min, max, stddev, count := v[1], v[2], v[3], v[4], v[5]
if stddev == nil {
// stddev will be nil when there were not enough items to be able
// to calculate a standard deviation.
stddev = json.Number("0")
}
results[i] = &AggregatedResult{
RootSpanName: row.Tags["name"],
Average: time.Duration(mustJSONFloat64(mean) * float64(time.Second)),
Min: time.Duration(mustJSONFloat64(min) * float64(time.Second)),
Max: time.Duration(mustJSONFloat64(max) * float64(time.Second)),
StdDev: time.Duration(mustJSONFloat64(stddev) * float64(time.Second)),
Samples: mustJSONInt64(count),
}
}
if len(result.Series) == 0 {
return nil, nil
}
n := 5
if n > len(result.Series) {
n = len(result.Series)
}
// Add in the N-slowest trace IDs for each span.
//
// TODO(slimsag): make N a pagination parameter instead.
q = fmt.Sprintf(`SELECT TOP("top",%d),trace_id FROM nslowest_spans GROUP BY "name"`, n)
result, err = in.executeOneQuery(q)
if err != nil {
return nil, err
}
for rowIndex, row := range result.Series {
if row.Tags["name"] != results[rowIndex].RootSpanName {
panic("expectation violated") // never happens, just for sanity.
}
for _, fields := range row.Values {
for i, field := range fields {
switch row.Columns[i] {
case "trace_id":
id, err := ParseID(field.(string))
if err != nil {
panic(err) // never happens, just for sanity.
}
results[rowIndex].Slowest = append(results[rowIndex].Slowest, id)
}
}
}
}
return results, nil
}
func (in *InfluxDBStore) Traces(opts TracesOpts) ([]*Trace, error) {
traces := make([]*Trace, 0)
rootSpansQuery := fmt.Sprintf("SELECT * FROM spans WHERE parent_id='%s'", zeroID)
// Extends `rootSpansQuery` to add time range filter using the start/end values from `opts.Timespan`.
if opts.Timespan != (Timespan{}) {
start := opts.Timespan.S.UTC().Format(time.RFC3339Nano)
end := opts.Timespan.E.UTC().Format(time.RFC3339Nano)
rootSpansQuery += fmt.Sprintf(" AND time >= '%s' AND time <= '%s'", start, end)
}
// Extends `rootSpansQuery` to add a filter to include only those traces present in `opts.TraceIDs`.
traceIDsLen := len(opts.TraceIDs)
if traceIDsLen > 0 {
rootSpansQuery += " AND ("
for i, traceID := range opts.TraceIDs {
rootSpansQuery += fmt.Sprintf("trace_id = '%s'", traceID)
lastIteration := (i+1 == traceIDsLen)
if !lastIteration {
rootSpansQuery += " OR "
}
}
rootSpansQuery += ")"
} else { // Otherwise continue limiting the number of traces to be returned.
rootSpansQuery += fmt.Sprintf(" LIMIT %d", in.tracesPerPage)
}
rootSpansResult, err := in.executeOneQuery(rootSpansQuery)
if err != nil {
return nil, err
}
if rootSpansResult.Err != nil {
return nil, rootSpansResult.Err
}
if len(rootSpansResult.Series) == 0 {
return traces, nil
}
// Expecting only one element, which contains the queried spans information.
if len(rootSpansResult.Series) > 1 {
return nil, errMultipleSeries
}
// Cache to keep track of traces to be returned.
tracesCache := make(map[ID]*Trace, 0)
rootSpans, err := spansFromRow(rootSpansResult.Series[0])
if err != nil {
return nil, err
}
for _, span := range rootSpans {
_, present := tracesCache[span.ID.Trace]
if !present {
tracesCache[span.ID.Trace] = &Trace{Span: *span}
} else {
return nil, errors.New("duplicated root span")
}
}
// Using 'OR' since 'IN' not supported yet.
where := `WHERE `
var i int = 1
for _, trace := range tracesCache {
where += fmt.Sprintf("(trace_id='%s' AND parent_id!='%s')", trace.Span.ID.Trace, zeroID)
// Adds 'OR' except for last iteration.
if i != len(tracesCache) && len(tracesCache) > 1 {
where += " OR "
}
i += 1
}
// Queries for all children spans of the root traces.
childrenSpansQuery := fmt.Sprintf("SELECT * FROM spans %s", where)
childrenSpansResult, err := in.executeOneQuery(childrenSpansQuery)
if err != nil {
return nil, err
}
// Cache to keep track all trace children of root traces to be returned.
children := make(map[ID][]*Trace, 0) // Span.ID.Trace -> []*Trace
childrenSpans, err := spansFromRow(childrenSpansResult.Series[0])
if err != nil {
return nil, err
}
// Iterates over `childrenSpans` to fill `children` cache.
for _, span := range childrenSpans {
trace, present := tracesCache[span.ID.Trace]
if !present { // Root trace not added.
return nil, errors.New("parent not found")
} else { // Root trace already added, append `child` to `children` for later usage.
child := &Trace{Span: *span}
t, found := children[trace.ID.Trace]
if !found {
children[trace.ID.Trace] = []*Trace{child}
} else {
children[trace.ID.Trace] = append(t, child)
}
}
}
// Iterates over `tracesCache` to find and set their trace children.
for _, trace := range tracesCache {
traceChildren, present := children[trace.ID.Trace]
if present {
if err := addChildren(trace, traceChildren); err != nil {
return nil, err
}
}
traces = append(traces, trace)
}
return traces, nil
}
// Close flushes the last batch to InfluxDB and shuts down the InfluxDBStore.
func (in *InfluxDBStore) Close() error {
close(in.flusherStopChan)
if err := in.flush(); err != nil {
in.log.Println("Flush:", err)
}
return in.server.Close()
}
func (in *InfluxDBStore) createDBIfNotExists() error {
q := fmt.Sprintf("CREATE DATABASE %s", in.dbName)
// If a default retention policy is provided, it's used to extend the query in order to create the database with
// a default retention policy.
rp := in.config.DefaultRP
if rp.Duration != "" {
q = fmt.Sprintf("%s WITH DURATION %s", q, rp.Duration)
// Retention policy name must be placed to the end of the query or it will be syntactically incorrect.
if rp.Name != "" {
q = fmt.Sprintf("%s NAME %s", q, rp.Name)
}
}
// If there are no errors, query execution was successfully - either DB was created or already exists.
return in.executeQueryNoResults(q)
}
// createContinuousQueries creates the continuous queries used by Appdash. If
// they already exist, no error is returned.
func (in *InfluxDBStore) createContinuousQueries() error {
// The 'GROUP BY' (or 'bucket', if you will) size is calculated as the
// maximum number of samples we want over 72hr. The more samples we have,
// the more precise the stats on the Dashboard are and the slower query
// time is. 5k data points on a 2015 MBP takes about 500ms to calculate
// our statistics currently, thus:
//
// 5000 points / (72h * 60m) == 1.157 (points per minute) == 1 point in 69s == GROUP BY 1m
//
// Because we're downsampling we can downsample different methods (mean,
// min, max, etc) for the most accurate query later on. Thus we have a
// mapping of aggregated -> real query likeso:
//
// | Dashboard | Aggregated | Real Query | Description |
// |----------------|--------------------|----------------|---------------------------------|
// | Average | MEAN("duration") | MEAN("mean") | average of all averages |
// | Min | MIN("duration") | MIN("min") | minimum of all minimums |
// | Max | MAX("duration") | MAX("max") | maximum of all maximums |
// | Std. Deviation | STDDEV("duration") | MEAN("stddev") | average of all std. deviations |
// | Timespans | COUNT("duration") | SUM("count") | total counted durations (spans) |
//
q := fmt.Sprintf(`CREATE CONTINUOUS QUERY cq_downsampled_spans_1m ON %s RESAMPLE EVERY 1m BEGIN SELECT MEAN("duration"),MIN("duration"),MAX("duration"),STDDEV("duration"),COUNT("duration") INTO downsampled_spans FROM spans GROUP BY time(1m), "name" END`, in.dbName)
if err := in.executeQueryNoResults(q); err != nil {
return err
}
// The above query doesn't give us a reference to the trace IDs because
// InfluxDB doesn't yet support mixing aggregate and non-aggregate queries
// together. To workaround this, we create a secondary CQ. This CQ
// downsamples the N-slowest span IDs by name so that we can efficiently
// link to them from the dashboard.
//
// Note: This actually only gives us 1 N-slowest trace over the GROUP BY
// timeframe (1 N-slowest trace over 1m), but this is good enough in
// general.
q = fmt.Sprintf(`CREATE CONTINUOUS QUERY cq_nslowest_spans_1m ON %s RESAMPLE EVERY 1m BEGIN SELECT TOP("duration",1),trace_id,span_id,parent_id INTO nslowest_spans FROM spans GROUP BY time(1m), "name" END`, in.dbName)
return in.executeQueryNoResults(q)
}
// createAdminUserIfNotExists finds admin user(`in.adminUser`) if not found it's created.
func (in *InfluxDBStore) createAdminUserIfNotExists() error {
admin := in.config.AdminUser
userInfo, err := in.server.MetaClient.Authenticate(admin.Username, admin.Password)
if err == influxDBErrors.ErrUserNotFound {
if _, createUserErr := in.server.MetaClient.CreateUser(admin.Username, admin.Password, true); createUserErr != nil {
return createUserErr
}
return nil
} else {
return err
}
if !userInfo.Admin { // must be admin user.
return errors.New("failed to validate InfluxDB user type, found non-admin user")
}
return nil
}
func (in *InfluxDBStore) executeOneQuery(command string) (*influxDBClient.Result, error) {
response, err := in.con.Query(influxDBClient.Query{
Command: command,
Database: in.dbName,
})
if err != nil {
return nil, err
}
if err := response.Error(); err != nil {
return nil, err
}
// Expecting one result, since a single query is executed.
if len(response.Results) != 1 {
return nil, errors.New("unexpected number of results for an influxdb single query")
}
return &response.Results[0], nil
}
// executeQueryNoResults is a helper function which executes a single query and
// expects no results. If any error occurs, it is returned.
func (in *InfluxDBStore) executeQueryNoResults(command string) error {
response, err := in.con.Query(influxDBClient.Query{
Command: command,
Database: in.dbName,
})
if err != nil {
return err
}
if err := response.Error(); err != nil {
return err
}
return nil
}
func (in *InfluxDBStore) init(server *influxDBServer.Server) error {
in.server = server
// TODO: Upgrade to client v2, see: github.com/influxdata/influxdb/blob/master/client/v2/client.go
// We're currently using v1.
con, err := influxDBClient.NewClient(influxDBClient.Config{
URL: *in.clientTarget,
Username: in.config.AdminUser.Username,
Password: in.config.AdminUser.Password,
})
if err != nil {
return err
}
in.con = con
if err := in.createAdminUserIfNotExists(); err != nil {
return err
}
switch in.config.Mode {
case testMode:
if err := in.setUpTestMode(); err != nil {
return err
}
default:
if err := in.setUpReleaseMode(); err != nil {
return err
}
}
if err := in.createDBIfNotExists(); err != nil {
return err
}
if err := in.createContinuousQueries(); err != nil {
return err
}
// TODO: let lib users decide `in.tracesPerPage` through InfluxDBConfig.
in.tracesPerPage = defaultTracesPerPage
go in.flusher()
return nil
}
func (in *InfluxDBStore) setUpReleaseMode() error {
in.dbName = releaseDBName
return nil
}
func (in *InfluxDBStore) setUpTestMode() error {
in.dbName = testDBName
return in.executeQueryNoResults(fmt.Sprintf("DROP DATABASE IF EXISTS %s", in.dbName))
}
func annotationsFromEvents(a Annotations) (Annotations, error) {
var (
annotations Annotations
events []Event
)
if err := UnmarshalEvents(a, &events); err != nil {
return nil, err
}
for _, e := range events {
anns, err := MarshalEvent(e)
if err != nil {
return nil, err
}
annotations = append(annotations, anns...)
}
return annotations, nil
}
// fieldToSpanID converts given field to span ID.
func fieldToSpanID(field interface{}, errFieldType error) (*ID, error) {
f, ok := field.(string)
if !ok {
return nil, errFieldType
}
id, err := ParseID(f)
if err != nil {
return nil, err
}
return &id, nil
}
// filterSchemas returns `Annotations` which contains items taken from `anns`.
// Some items from `anns` won't be included(those which were not saved by `InfluxDBStore.Collect(...)`).
func filterSchemas(anns []Annotation) Annotations {
var annotations Annotations
// Finds an annotation which: `Annotation.Key` is equal to `schemasFieldName`.
schemasAnn := findSchemasAnnotation(anns)
// Converts `schemasAnn.Value` into slice of strings, each item is a schema.
// Eg. schemas := []string{"HTTPClient", "HTTPServer"}
schemas := strings.Split(string(schemasAnn.Value), schemasFieldSeparator)
// Iterates over `anns` to check if each annotation should be included or not to the `annotations` be returned.
for _, a := range anns {
if strings.HasPrefix(a.Key, schemaPrefix) { // Check if current annotation is schema related one.
schema := a.Key[len(schemaPrefix):] // Excludes the schema prefix part.
// Checks if `schema` exists in `schemas`, if so means current annotation was saved by `InfluxDBStore.Collect(...)`.
// If does not exist it means current annotation is empty on `InfluxDBStore.dbName` but still included within a query result.
// Eg. If point "f" with a field "foo" & point "b" with a field "bar" are written to the same InfluxDB measurement
// and later queried, the result will include two fields: "foo" & "bar" for both points, even though each was written with one field.
if schemaExists(schema, schemas) { // Saved by `InfluxDBStore.Collect(...)` so should be added.
annotations = append(annotations, a)
} else { // Do not add current annotation, is empty & not saved by `InfluxDBStore.Collect(...)`.
continue
}
} else {
// Not a schema related annotation so just add it.
annotations = append(annotations, a)
}
}
return annotations
}
// schemaExists checks if `schema` is present on `schemas`.
func schemaExists(schema string, schemas []string) bool {
for _, s := range schemas {
if schema == s {
return true
}
}
return false
}
// findSchemasAnnotation finds & returns an annotation which: `Annotation.Key` is equal to `schemasFieldName`.
func findSchemasAnnotation(anns []Annotation) *Annotation {
for _, a := range anns {
if a.Key == schemasFieldName {
return &a
}
}
return nil
}
// findTraceParent walks through `rootTrace` to look for `child`; once found — it's trace parent is returned.
func findTraceParent(root, child *Trace) *Trace {
var walkToParent func(root, child *Trace) *Trace
walkToParent = func(root, child *Trace) *Trace {
if root.ID.Span == child.ID.Parent {
return root
}
for _, sub := range root.Sub {
if sub.ID.Span == child.ID.Parent {
return sub
}
if r := walkToParent(sub, child); r != nil {
return r
}
}
return nil
}
return walkToParent(root, child)
}
// schemasFromAnnotations returns a string(a set of schemas(strings) separated by `schemasFieldSeparator`) - eg. "HTTPClient,HTTPServer,name".
// Each schema is extracted from each `Annotation.Key` from `anns`.
func schemasFromAnnotations(anns []Annotation) string {
var schemas []string
for _, ann := range anns {
// Checks if current annotation is schema related.
if strings.HasPrefix(ann.Key, schemaPrefix) {
schemas = append(schemas, ann.Key[len(schemaPrefix):])
}
}
return strings.Join(schemas, schemasFieldSeparator)
}
// addChildren adds `children` to `root`; each child is appended to it's trace parent.
func addChildren(root *Trace, children []*Trace) error {
var (
addFn func() // Handles children appending to it's trace parent.
retries int = len(children) // Maximum number of retries to add `children` elements to `root`.
try int // Current number of try to add `children` elements to `root`.
)
addFn = func() {
for i := len(children) - 1; i >= 0; i-- {
child := children[i]
t := findTraceParent(root, child)
if t != nil { // Trace found.
if t.Sub == nil { // Empty sub-traces slice.
t.Sub = []*Trace{child}
} else { // Non-empty sub-traces slice.
t.Sub = append(t.Sub, child)
}
// Removes current child since was added to it's parent.
children = append(children[:i], children[i+1:]...)
}
}
}
// Loops until all `children` elements were added to it's trace parent or when maximum number of retries reached.
for {
if len(children) == 0 {
break
}
// At this point, all children were added to their parent spans. Any children
// left over in the children slice do not have parents. This could happen if,
// for example, a parent service fails to record its span information to the
// collection server but its downstream services do send their span information
// properly. In this case, we gracefully degrade by adding these orphan spans to
// the root span.
if try == retries {
// Iterates over children(without parent found on `root`) and appends them as sub-traces to `root`.
for _, child := range children {
if child.ID.Trace == root.ID.Trace {
root.Sub = append(root.Sub, child)
}
}
return nil
}
addFn()
try++
}
return nil
}
// pointMemoryUsage returns an estimate of the memory usage of the given *influxDBClient.Point
// object in bytes.
func pointMemoryUsage(v reflect.Value) int {
s := int(v.Type().Size())
switch v.Kind() {
case reflect.String:
s += v.Len()
case reflect.Ptr:
if !v.IsNil() {
s += pointMemoryUsage(reflect.Indirect(v))
}
case reflect.Map:
for _, key := range v.MapKeys() {
s += pointMemoryUsage(v.MapIndex(key))
}
}
return s
}
// withoutEmptyFields filters `pf` and returns `pointFields` excluding those that have empty values.
func withoutEmptyFields(pf pointFields) pointFields {
r := make(pointFields, 0)
for k, v := range pf {
switch v.(type) {
case string:
if v.(string) == "" {
continue
}
r[k] = v
case nil:
continue
default:
r[k] = v
}
}
return r
}
// spansFromRow rebuilds spans from given InfluxDB row.
func spansFromRow(row influxDBModels.Row) ([]*Span, error) {
var spans []*Span
// Iterates through all `row.Values`, each represents a set of single span information(ids and annotations).
for _, fields := range row.Values {
span := &Span{
Annotations: make(Annotations, 0),
}
// Iterates over fields, each field might be a span's annotation value or span's ID(either a TraceID, SpanID or ParentID).
for i, field := range fields {
var (
// column is current field column's name (Eg. field ='Server.Request.Method', columns[i] = 'GET').
column string = row.Columns[i]
// errFieldType is an error for unexpected field type.
errFieldType error = fmt.Errorf("unexpected field type: %v", reflect.TypeOf(field))
)
// Checks if current column is some span's ID, if so set to the span & continue with next field.
switch column {
case "name", "duration":
continue // aggregation
case "trace_id":
traceID, err := fieldToSpanID(field, errFieldType)
if err != nil {
return spans, err
}
span.ID.Trace = *traceID
continue
case "span_id":
spanID, err := fieldToSpanID(field, errFieldType)
if err != nil {
return spans, err
}
span.ID.Span = *spanID
continue
case "parent_id":
parentID, err := fieldToSpanID(field, errFieldType)
if err != nil {
return spans, err
}
span.ID.Parent = *parentID
continue
}
// At this point the current field is a span's annotation value.
var value []byte // Annotation value.
switch field.(type) {
case string:
value = []byte(field.(string))
case nil:
default:
return nil, errFieldType
}
span.Annotations = append(span.Annotations, Annotation{
Key: column,
Value: value,
})
}
anns, err := annotationsFromEvents(filterSchemas(span.Annotations))
if err != nil {
return nil, err
}
span.Annotations = anns
spans = append(spans, span)
}
return spans, nil
}
// NewInfluxDBStore returns a new InfluxDB-backed store. It starts an
// in-process / embedded InfluxDB server.
func NewInfluxDBStore(config *InfluxDBConfig) (*InfluxDBStore, error) {
s, err := influxDBServer.NewServer(config.Server, config.BuildInfo)
if err != nil {
return nil, err
}
// If the user specified a log output location, configure everything to use that.
if config.LogOutput != nil {
s.SetLogOutput(config.LogOutput)
}
if err := s.Open(); err != nil {
return nil, err
}
httpdAddr := config.Server.HTTPD.BindAddress
if httpdAddr == "" {
httpdAddr = fmt.Sprintf("%s:%d", influxDBClient.DefaultHost, influxDBClient.DefaultPort)
}
clientTarget, err := url.Parse("http://" + httpdAddr)
if err != nil {
return nil, err
}
in := InfluxDBStore{
config: config,
clientTarget: clientTarget,
log: log.New(os.Stderr, "appdash: InfluxDBStore: ", log.LstdFlags),
}
if err := in.init(s); err != nil {
return nil, err
}
return &in, nil
}
// NewInfluxDBConfig returns a new InfluxDBConfig with the default values.
func NewInfluxDBConfig() (*InfluxDBConfig, error) {
// Create the default InfluxDB server configuration.
server, err := influxDBServer.NewDemoConfig()
if err != nil {
return nil, err
}
// Enables retention policies which will be executed within an interval of 30 minutes.
server.Retention.Enabled = true
server.Retention.CheckInterval = toml.Duration(30 * time.Minute)
return &InfluxDBConfig{
Server: server,
// Specify the branch as "appdash" just for identification purposes.
BuildInfo: &influxDBServer.BuildInfo{
Branch: "appdash",
},
// Create a retention policy which keeps data for only three days, this is
// because the Dashboard is hard-coded to displaying a 72hr timeline.
//
// Minimum duration time is 1 hour ("1h") - See: github.com/influxdata/influxdb/issues/5198
DefaultRP: InfluxDBRetentionPolicy{
Name: "three_days_only",
Duration: "3d",
},
MaxBatchSizeBytes: 128 * 1024 * 1024, // 128 MB
BatchFlushInterval: 500 * time.Millisecond,
}, nil
}
type InfluxDBConfig struct {
AdminUser InfluxDBAdminUser
BuildInfo *influxDBServer.BuildInfo
DefaultRP InfluxDBRetentionPolicy
Mode mode
Server *influxDBServer.Config
// LogOutput, if specified, controls where all InfluxDB logs are written to.
LogOutput io.Writer
// MaxBatchSizeBytes specifies the maximum size (estimated memory usage) in
// bytes that a batch may grow to become before being entirely dropped (and
// inherently, trace data lost). This prevents any potential memory leak in
// the case of an unresponsive or too slow InfluxDB server / pending flush
// operation.
//
// The default value used by NewInfluxDBConfig is 128*1024*1024 (128 MB).
MaxBatchSizeBytes int
// BatchFlushInterval specifies the minimum interval between flush calls by
// the background goroutine in order to flush point batches out to
// InfluxDB. That is, after each batch flush the goroutine will sleep for
// this amount of time to prevent CPU overutilization.
//
// The default value used by NewInfluxDBConfig is 500 * time.Millisecond.
BatchFlushInterval time.Duration
}
type InfluxDBRetentionPolicy struct {
Name string // Name used to indentify this retention policy.
Duration string // How long InfluxDB keeps the data. Eg: "1h", "1d", "1w".