Skip to content
This repository was archived by the owner on Oct 29, 2021. It is now read-only.

Commit d4354ee

Browse files
committed
clean-up TestInfluxDBStore & adds TestFindTraceParent
1 parent a7fb78f commit d4354ee

File tree

1 file changed

+194
-69
lines changed

1 file changed

+194
-69
lines changed

influxdb_store_test.go

+194-69
Original file line numberDiff line numberDiff line change
@@ -55,82 +55,198 @@ func TestSchemasFromAnnotations(t *testing.T) {
5555
}
5656
}
5757

58+
func TestFindTraceParent(t *testing.T) {
59+
trace := Trace{
60+
Span: Span{
61+
ID: SpanID{Trace: 1, Span: 100, Parent: 0},
62+
},
63+
Sub: []*Trace{
64+
&Trace{
65+
Span: Span{
66+
ID: SpanID{Trace: 1, Span: 11, Parent: 100},
67+
},
68+
Sub: []*Trace{
69+
&Trace{
70+
Span: Span{
71+
ID: SpanID{Trace: 1, Span: 111, Parent: 11},
72+
},
73+
Sub: []*Trace{
74+
&Trace{
75+
Span: Span{
76+
ID: SpanID{Trace: 1, Span: 1111, Parent: 111},
77+
},
78+
},
79+
},
80+
},
81+
&Trace{
82+
Span: Span{
83+
ID: SpanID{Trace: 1, Span: 112, Parent: 11},
84+
},
85+
Sub: []*Trace{
86+
&Trace{
87+
Span: Span{
88+
ID: SpanID{Trace: 1, Span: 1112, Parent: 112},
89+
},
90+
},
91+
},
92+
},
93+
},
94+
},
95+
},
96+
}
97+
cases := []struct {
98+
Parent *Trace
99+
Child *Trace
100+
}{
101+
{nil, &trace},
102+
{nil, &Trace{}},
103+
{&trace, trace.Sub[0]},
104+
{trace.Sub[0], trace.Sub[0].Sub[0]},
105+
{trace.Sub[0], trace.Sub[0].Sub[1]},
106+
{trace.Sub[0].Sub[0], trace.Sub[0].Sub[0].Sub[0]},
107+
{trace.Sub[0].Sub[1], trace.Sub[0].Sub[1].Sub[0]},
108+
}
109+
for i, c := range cases {
110+
got := findTraceParent(&trace, c.Child)
111+
if got != c.Parent {
112+
t.Fatalf("case: %d - got: %v, want: %v", i, got, c.Parent)
113+
}
114+
}
115+
}
116+
58117
func TestInfluxDBStore(t *testing.T) {
59118
store := newStore(t)
60119
defer func() {
61120
if err := store.Close(); err != nil {
62121
t.Fatal(err)
63122
}
64123
}()
65-
trace := Trace{
66-
Span: Span{
67-
ID: SpanID{1, 2, ID(0)},
68-
Annotations: Annotations{
69-
Annotation{Key: "Name", Value: []byte("/")},
70-
Annotation{Key: "Server.Request.Method", Value: []byte("GET")},
71-
Annotation{Key: clientEventKey, Value: []byte("")},
72-
Annotation{Key: serverEventKey, Value: []byte("")},
124+
traces := []*Trace{
125+
&Trace{
126+
Span: Span{
127+
ID: SpanID{1, 100, 0},
128+
Annotations: Annotations{
129+
Annotation{Key: "Name", Value: []byte("/")},
130+
Annotation{Key: "Server.Request.Method", Value: []byte("GET")},
131+
Annotation{Key: clientEventKey, Value: []byte("")},
132+
Annotation{Key: serverEventKey, Value: []byte("")},
133+
},
73134
},
74-
},
75-
Sub: []*Trace{
76-
&Trace{
77-
Span: Span{
78-
ID: SpanID{Trace: 1, Span: 11, Parent: 1},
79-
Annotations: Annotations{
80-
Annotation{Key: "Name", Value: []byte("localhost:8699/endpoint")},
81-
Annotation{Key: "Server.Request.Method", Value: []byte("GET")},
82-
Annotation{Key: clientEventKey, Value: []byte("")},
83-
Annotation{Key: serverEventKey, Value: []byte("")},
135+
Sub: []*Trace{
136+
&Trace{
137+
Span: Span{
138+
ID: SpanID{Trace: 1, Span: 11, Parent: 100},
139+
Annotations: Annotations{
140+
Annotation{Key: "Name", Value: []byte("localhost:8699/endpoint")},
141+
Annotation{Key: "Server.Request.Method", Value: []byte("GET")},
142+
Annotation{Key: clientEventKey, Value: []byte("")},
143+
Annotation{Key: serverEventKey, Value: []byte("")},
144+
},
145+
},
146+
Sub: []*Trace{
147+
&Trace{
148+
Span: Span{
149+
ID: SpanID{Trace: 1, Span: 111, Parent: 11},
150+
Annotations: Annotations{
151+
Annotation{Key: "Name", Value: []byte("localhost:8699/sub1")},
152+
Annotation{Key: "Server.Request.Method", Value: []byte("GET")},
153+
Annotation{Key: clientEventKey, Value: []byte("")},
154+
Annotation{Key: serverEventKey, Value: []byte("")},
155+
},
156+
},
157+
Sub: []*Trace{
158+
&Trace{
159+
Span: Span{
160+
ID: SpanID{Trace: 1, Span: 1111, Parent: 111},
161+
Annotations: Annotations{
162+
Annotation{Key: "Name", Value: []byte("localhost:8699/sub2")},
163+
Annotation{Key: "Server.Request.Method", Value: []byte("GET")},
164+
Annotation{Key: clientEventKey, Value: []byte("")},
165+
Annotation{Key: serverEventKey, Value: []byte("")},
166+
},
167+
},
168+
},
169+
},
170+
},
84171
},
85172
},
86173
},
87174
},
175+
&Trace{
176+
Span: Span{
177+
ID: SpanID{2, 200, 0},
178+
Annotations: Annotations{
179+
Annotation{Key: "Name", Value: []byte("/")},
180+
Annotation{Key: "Server.Request.Method", Value: []byte("GET")},
181+
Annotation{Key: clientEventKey, Value: []byte("")},
182+
Annotation{Key: serverEventKey, Value: []byte("")},
183+
},
184+
},
185+
},
88186
}
89187

90-
// Collect root span.
91-
if err := store.Collect(trace.Span.ID, trace.Span.Annotations...); err != nil {
92-
t.Fatalf("unexpected error: %+v", err)
93-
}
188+
var (
189+
collect func(trace *Trace)
190+
collectAll func(trace *Trace)
191+
keys []string = []string{"time", "schemas"} // InfluxDB related annotations keys.
192+
tracesMap map[ID]*Trace = make(map[ID]*Trace, 0) // Trace ID -> Trace.
193+
)
94194

95-
// Collect first child span.
96-
if err := store.Collect(trace.Sub[0].Span.ID, trace.Sub[0].Span.Annotations...); err != nil {
97-
t.Fatalf("unexpected error: %+v", err)
195+
collect = func(trace *Trace) {
196+
if err := store.Collect(trace.Span.ID, trace.Span.Annotations...); err != nil {
197+
t.Fatalf("unexpected error: %+v", err)
198+
}
98199
}
99-
100-
// Find one trace.
101-
savedTrace, err := store.Trace(trace.Span.ID.Trace)
102-
if err != nil {
103-
t.Fatalf("unexpected error: %+v", err)
200+
collectAll = func(trace *Trace) {
201+
for _, sub := range trace.Sub {
202+
collect(sub)
203+
collectAll(sub)
204+
}
104205
}
105-
if savedTrace == nil {
106-
t.Fatalf("expected trace, got nil")
206+
for _, trace := range traces {
207+
tracesMap[trace.ID.Trace] = trace
107208
}
108-
// Non-deterministic keys:
109-
// - "time" added by InfluxDB.
110-
// - "schemas" see: `schemasFieldName` within `InfluxDBStore.Collect(...)`.
111-
keys := []string{"time", "schemas"}
112209

113-
// Using extendAnnotations in order to create a trace which includes those
114-
// non-deterministic annotations with keys included in `fields` and values
115-
// taken from `savedTrace`.
116-
wantTrace := extendAnnotations(trace, *savedTrace, keys)
210+
// InfluxDBStore.Collect(...) tests.
211+
for _, trace := range traces {
212+
collect(trace)
213+
collectAll(trace)
214+
}
117215

118-
sortAnnotations(*savedTrace, wantTrace)
119-
if !reflect.DeepEqual(*savedTrace, wantTrace) {
120-
t.Fatalf("got: %v, want: %v", savedTrace, wantTrace)
216+
mustEqual := func(gotTrace *Trace, t *testing.T) {
217+
want, found := tracesMap[gotTrace.ID.Trace]
218+
if !found {
219+
t.Fatal("trace not found")
220+
}
221+
removeInfluxDBAnnotations(gotTrace, keys)
222+
sortAnnotations(*gotTrace, *want)
223+
if !reflect.DeepEqual(gotTrace, want) {
224+
t.Fatalf("got: %v, want: %v", gotTrace, want)
225+
}
121226
}
122227

123-
// Find many traces.
124-
traces, err := store.Traces()
228+
// InfluxDBStore.Trace(...) tests.
229+
for _, trace := range traces {
230+
gotTrace, err := store.Trace(trace.ID.Trace)
231+
if err != nil {
232+
t.Fatalf("unexpected error: %+v", err)
233+
}
234+
if t == nil {
235+
t.Fatalf("expected a trace, got nil")
236+
}
237+
mustEqual(gotTrace, t)
238+
}
239+
240+
// InfluxDBStore.Traces(...) tests.
241+
gotTraces, err := store.Traces()
125242
if err != nil {
126243
t.Fatalf("unexpected error: %+v", err)
127244
}
128-
if len(traces) != 1 {
129-
t.Fatalf("unexpected quantity of traces, want: %v, got: %v", 1, len(traces))
245+
if len(gotTraces) != len(traces) {
246+
t.Fatalf("unexpected quantity of traces, got: %v, want: %v", len(gotTraces), len(traces))
130247
}
131-
sortAnnotations(*traces[0])
132-
if !reflect.DeepEqual(*traces[0], wantTrace) {
133-
t.Fatalf("got: %v, want: %v", traces[0], wantTrace)
248+
for _, gotTrace := range gotTraces {
249+
mustEqual(gotTrace, t)
134250
}
135251
}
136252

@@ -155,26 +271,30 @@ func newStore(t *testing.T) *InfluxDBStore {
155271
return store
156272
}
157273

158-
// extendAnnotations creates & returns a new Trace which is a copy of `dst`.
159-
// Trace returned has `annotations` copied from `src` but only those
160-
// with keys included on `keys`.
161-
func extendAnnotations(dst, src Trace, keys []string) Trace {
162-
t := dst
163-
for _, k := range keys {
164-
t.Span.Annotations = append(t.Span.Annotations, Annotation{
165-
Key: k,
166-
Value: src.Span.Annotations.get(k),
167-
})
168-
}
169-
for i, sub := range t.Sub {
170-
for _, k := range keys {
171-
sub.Span.Annotations = append(sub.Span.Annotations, Annotation{
172-
Key: k,
173-
Value: src.Sub[i].Span.Annotations.get(k),
174-
})
274+
// removeInfluxDBAnnotations removes annotations from `root` and it's subtraces; only those annotations that have as key present on `keys` will be removed.
275+
func removeInfluxDBAnnotations(root *Trace, keys []string) {
276+
var (
277+
walk func(root *Trace)
278+
removeFn func(trace *Trace, keys []string)
279+
)
280+
removeFn = func(trace *Trace, keys []string) {
281+
for i := len(trace.Annotations) - 1; i >= 0; i-- {
282+
for _, k := range keys {
283+
if trace.Annotations[i].Key == k {
284+
trace.Annotations = append(trace.Annotations[:i], trace.Annotations[i+1:]...)
285+
break
286+
}
287+
}
288+
}
289+
}
290+
walk = func(root *Trace) {
291+
removeFn(root, keys)
292+
for _, sub := range root.Sub {
293+
removeFn(sub, keys)
294+
walk(sub)
175295
}
176296
}
177-
return t
297+
walk(root)
178298
}
179299

180300
// sortSchemas sorts schemas(strings) within `s` which is
@@ -186,12 +306,17 @@ func sortSchemas(s string) string {
186306
}
187307

188308
func sortAnnotations(traces ...Trace) {
189-
for _, t := range traces {
309+
var walk func(t *Trace)
310+
walk = func(t *Trace) {
190311
sort.Sort(annotations(t.Span.Annotations))
191312
for _, s := range t.Sub {
192313
sort.Sort(annotations(s.Span.Annotations))
314+
walk(s)
193315
}
194316
}
317+
for _, t := range traces {
318+
walk(&t)
319+
}
195320
}
196321

197322
type bySchemaText []string

0 commit comments

Comments
 (0)