@@ -12,10 +12,12 @@ import (
12
12
"github.com/stretchr/testify/assert"
13
13
"github.com/stretchr/testify/require"
14
14
"go.opentelemetry.io/collector/pdata/pcommon"
15
+ "go.opentelemetry.io/collector/pdata/ptrace"
15
16
"google.golang.org/grpc"
16
17
"google.golang.org/grpc/credentials/insecure"
17
18
18
19
"github.com/jaegertracing/jaeger/internal/jiter"
20
+ "github.com/jaegertracing/jaeger/internal/jptrace"
19
21
"github.com/jaegertracing/jaeger/internal/proto-gen/storage/v2"
20
22
"github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore"
21
23
)
@@ -25,37 +27,45 @@ import (
25
27
type testServer struct {
26
28
storage.UnimplementedTraceReaderServer
27
29
30
+ traces []* jptrace.TracesData
28
31
services []string
29
32
operations []* storage.Operation
30
33
traceIDs []* storage.FoundTraceID
31
34
err error
32
35
}
33
36
34
- func (s * testServer ) GetServices (
37
+ func (ts * testServer ) GetTraces (_ * storage.GetTracesRequest , s storage.TraceReader_GetTracesServer ) error {
38
+ for _ , trace := range ts .traces {
39
+ s .Send (trace )
40
+ }
41
+ return ts .err
42
+ }
43
+
44
+ func (ts * testServer ) GetServices (
35
45
context.Context ,
36
46
* storage.GetServicesRequest ,
37
47
) (* storage.GetServicesResponse , error ) {
38
48
return & storage.GetServicesResponse {
39
- Services : s .services ,
40
- }, s .err
49
+ Services : ts .services ,
50
+ }, ts .err
41
51
}
42
52
43
- func (s * testServer ) GetOperations (
53
+ func (ts * testServer ) GetOperations (
44
54
context.Context ,
45
55
* storage.GetOperationsRequest ,
46
56
) (* storage.GetOperationsResponse , error ) {
47
57
return & storage.GetOperationsResponse {
48
- Operations : s .operations ,
49
- }, s .err
58
+ Operations : ts .operations ,
59
+ }, ts .err
50
60
}
51
61
52
- func (s * testServer ) FindTraceIDs (
62
+ func (ts * testServer ) FindTraceIDs (
53
63
context.Context ,
54
64
* storage.FindTracesRequest ,
55
65
) (* storage.FindTraceIDsResponse , error ) {
56
66
return & storage.FindTraceIDsResponse {
57
- TraceIds : s .traceIDs ,
58
- }, s .err
67
+ TraceIds : ts .traceIDs ,
68
+ }, ts .err
59
69
}
60
70
61
71
func startTestServer (t * testing.T , testServer * testServer ) * grpc.ClientConn {
@@ -90,12 +100,122 @@ func startServer(t *testing.T, server *grpc.Server, listener net.Listener) *grpc
90
100
return conn
91
101
}
92
102
103
+ func makeTestTrace () ptrace.Traces {
104
+ trace := ptrace .NewTraces ()
105
+ resources := trace .ResourceSpans ().AppendEmpty ()
106
+ scopes := resources .ScopeSpans ().AppendEmpty ()
107
+
108
+ spanA := scopes .Spans ().AppendEmpty ()
109
+ spanA .SetName ("foobar" )
110
+ spanA .SetTraceID (pcommon .TraceID ([16 ]byte {1 }))
111
+ spanA .SetSpanID (pcommon .SpanID ([8 ]byte {2 }))
112
+ spanA .SetKind (ptrace .SpanKindServer )
113
+ spanA .Status ().SetCode (ptrace .StatusCodeError )
114
+
115
+ return trace
116
+ }
117
+
93
118
func TestTraceReader_GetTraces (t * testing.T ) {
94
- tr := & TraceReader {}
119
+ tests := []struct {
120
+ name string
121
+ testServer * testServer
122
+ traces []* jptrace.TracesData
123
+ expectedTraces []ptrace.Traces
124
+ expectedError string
125
+ }{
126
+ {
127
+ name : "single trace" ,
128
+ testServer : & testServer {
129
+ traces : func () []* jptrace.TracesData {
130
+ trace := makeTestTrace ()
131
+ traces := []* jptrace.TracesData {(* jptrace .TracesData )(& trace )}
132
+ return traces
133
+ }(),
134
+ },
135
+ expectedTraces : []ptrace.Traces {makeTestTrace ()},
136
+ },
137
+ {
138
+ name : "multiple traces" ,
139
+ testServer : & testServer {
140
+ traces : func () []* jptrace.TracesData {
141
+ traceA := makeTestTrace ()
142
+ traceB := makeTestTrace ()
143
+ traces := []* jptrace.TracesData {
144
+ (* jptrace .TracesData )(& traceA ),
145
+ (* jptrace .TracesData )(& traceB ),
146
+ }
147
+ return traces
148
+ }(),
149
+ },
150
+ expectedTraces : []ptrace.Traces {makeTestTrace (), makeTestTrace ()},
151
+ },
152
+ {
153
+ name : "error" ,
154
+ testServer : & testServer {
155
+ traces : func () []* jptrace.TracesData {
156
+ trace := ptrace .NewTraces ()
157
+ traces := []* jptrace.TracesData {(* jptrace .TracesData )(& trace )}
158
+ return traces
159
+ }(),
160
+ err : assert .AnError ,
161
+ },
162
+ expectedError : "received error from grpc stream" ,
163
+ },
164
+ }
95
165
96
- require .Panics (t , func () {
97
- tr .GetTraces (context .Background (), tracestore.GetTraceParams {})
166
+ for _ , test := range tests {
167
+ t .Run (test .name , func (t * testing.T ) {
168
+ conn := startTestServer (t , test .testServer )
169
+
170
+ reader := NewTraceReader (conn )
171
+ getTracesIter := reader .GetTraces (context .Background (), tracestore.GetTraceParams {})
172
+ traces , err := jiter .FlattenWithErrors (getTracesIter )
173
+
174
+ if test .expectedError != "" {
175
+ require .ErrorContains (t , err , test .expectedError )
176
+ } else {
177
+ require .NoError (t , err )
178
+ require .Equal (t , test .expectedTraces , traces )
179
+ }
180
+ })
181
+ }
182
+ }
183
+
184
+ func TestTraceReader_GetTraces_YieldStopsIteration (t * testing.T ) {
185
+ traceA := makeTestTrace ()
186
+ traceB := makeTestTrace ()
187
+ testServer := & testServer {
188
+ traces : []* jptrace.TracesData {
189
+ (* jptrace .TracesData )(& traceA ),
190
+ (* jptrace .TracesData )(& traceB ),
191
+ },
192
+ }
193
+
194
+ conn := startTestServer (t , testServer )
195
+ reader := NewTraceReader (conn )
196
+
197
+ getTracesIter := reader .GetTraces (context .Background (), tracestore.GetTraceParams {})
198
+ var gotTraces []ptrace.Traces
199
+ getTracesIter (func (traces []ptrace.Traces , _ error ) bool {
200
+ gotTraces = append (gotTraces , traces ... )
201
+ return false
202
+ })
203
+
204
+ require .Len (t , gotTraces , 1 )
205
+ }
206
+
207
+ func TestTraceReader_GetTraces_GRPCClientError (t * testing.T ) {
208
+ conn , err := grpc .NewClient (":0" ,
209
+ grpc .WithTransportCredentials (insecure .NewCredentials ()),
210
+ ) // create client without a started server
211
+ require .NoError (t , err )
212
+ t .Cleanup (func () {
213
+ conn .Close ()
98
214
})
215
+ reader := NewTraceReader (conn )
216
+ getTracesIter := reader .GetTraces (context .Background (), tracestore.GetTraceParams {})
217
+ _ , err = jiter .FlattenWithErrors (getTracesIter )
218
+ require .ErrorContains (t , err , "received error from grpc reader client" )
99
219
}
100
220
101
221
func TestTraceReader_GetServices (t * testing.T ) {
0 commit comments