Codebase list golang-github-go-kit-kit / run/0c243515-0bf2-4b25-8735-9c1038fa7c1b/main tracing / opentracing / endpoint_test.go
run/0c243515-0bf2-4b25-8735-9c1038fa7c1b/main

Tree @run/0c243515-0bf2-4b25-8735-9c1038fa7c1b/main (Download .tar.gz)

endpoint_test.go @run/0c243515-0bf2-4b25-8735-9c1038fa7c1b/mainraw · history · blame

  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
package opentracing_test

import (
	"context"
	"errors"
	"fmt"
	"reflect"
	"testing"
	"time"

	"github.com/opentracing/opentracing-go"
	otext "github.com/opentracing/opentracing-go/ext"
	otlog "github.com/opentracing/opentracing-go/log"
	"github.com/opentracing/opentracing-go/mocktracer"

	"github.com/go-kit/kit/endpoint"
	"github.com/go-kit/kit/sd"
	"github.com/go-kit/kit/sd/lb"
	kitot "github.com/go-kit/kit/tracing/opentracing"
)

const (
	span1 = "SPAN-1"
	span2 = "SPAN-2"
	span3 = "SPAN-3"
	span4 = "SPAN-4"
	span5 = "SPAN-5"
	span6 = "SPAN-6"
	span7 = "SPAN-7"
	span8 = "SPAN-8"
)

var (
	err1 = errors.New("some error")
	err2 = errors.New("some business error")
	err3 = errors.New("other business error")
)

// compile time assertion
var _ endpoint.Failer = failedResponse{}

type failedResponse struct {
	err error
}

func (r failedResponse) Failed() error {
	return r.err
}

func TestTraceEndpoint(t *testing.T) {
	tracer := mocktracer.New()

	// Initialize the ctx with a parent Span.
	parentSpan := tracer.StartSpan("parent").(*mocktracer.MockSpan)
	defer parentSpan.Finish()
	ctx := opentracing.ContextWithSpan(context.Background(), parentSpan)

	tracedEndpoint := kitot.TraceEndpoint(tracer, "testOp")(endpoint.Nop)
	if _, err := tracedEndpoint(ctx, struct{}{}); err != nil {
		t.Fatal(err)
	}

	// tracedEndpoint created a new Span.
	finishedSpans := tracer.FinishedSpans()
	if want, have := 1, len(finishedSpans); want != have {
		t.Fatalf("Want %v span(s), found %v", want, have)
	}

	endpointSpan := finishedSpans[0]
	if want, have := "testOp", endpointSpan.OperationName; want != have {
		t.Fatalf("Want %q, have %q", want, have)
	}

	parentContext := parentSpan.Context().(mocktracer.MockSpanContext)
	endpointContext := parentSpan.Context().(mocktracer.MockSpanContext)

	// ... and that the parent ID is set appropriately.
	if want, have := parentContext.SpanID, endpointContext.SpanID; want != have {
		t.Errorf("Want ParentID %q, have %q", want, have)
	}
}

func TestTraceEndpointNoContextSpan(t *testing.T) {
	tracer := mocktracer.New()

	// Empty/background context.
	tracedEndpoint := kitot.TraceEndpoint(tracer, "testOp")(endpoint.Nop)
	if _, err := tracedEndpoint(context.Background(), struct{}{}); err != nil {
		t.Fatal(err)
	}

	// tracedEndpoint created a new Span.
	finishedSpans := tracer.FinishedSpans()
	if want, have := 1, len(finishedSpans); want != have {
		t.Fatalf("Want %v span(s), found %v", want, have)
	}

	endpointSpan := finishedSpans[0]

	if want, have := "testOp", endpointSpan.OperationName; want != have {
		t.Fatalf("Want %q, have %q", want, have)
	}
}

func TestTraceEndpointWithOptions(t *testing.T) {
	tracer := mocktracer.New()

	// span 1 without options
	mw := kitot.TraceEndpoint(tracer, span1)
	tracedEndpoint := mw(endpoint.Nop)
	_, _ = tracedEndpoint(context.Background(), struct{}{})

	// span 2 with options
	mw = kitot.TraceEndpoint(
		tracer,
		span2,
		kitot.WithOptions(kitot.EndpointOptions{}),
	)
	tracedEndpoint = mw(func(context.Context, interface{}) (interface{}, error) {
		return nil, err1
	})
	_, _ = tracedEndpoint(context.Background(), struct{}{})

	// span 3 with lb error
	mw = kitot.TraceEndpoint(
		tracer,
		span3,
		kitot.WithOptions(kitot.EndpointOptions{}),
	)
	tracedEndpoint = mw(
		lb.Retry(
			5,
			1*time.Second,
			lb.NewRoundRobin(
				sd.FixedEndpointer{
					func(context.Context, interface{}) (interface{}, error) {
						return nil, err1
					},
				},
			),
		),
	)
	_, _ = tracedEndpoint(context.Background(), struct{}{})

	// span 4 with disabled IgnoreBusinessError option
	mw = kitot.TraceEndpoint(
		tracer,
		span4,
		kitot.WithIgnoreBusinessError(false),
	)
	tracedEndpoint = mw(func(context.Context, interface{}) (interface{}, error) {
		return failedResponse{
			err: err2,
		}, nil
	})
	_, _ = tracedEndpoint(context.Background(), struct{}{})

	// span 5 with enabled IgnoreBusinessError option
	mw = kitot.TraceEndpoint(tracer, span5, kitot.WithIgnoreBusinessError(true))
	tracedEndpoint = mw(func(context.Context, interface{}) (interface{}, error) {
		return failedResponse{
			err: err3,
		}, nil
	})
	_, _ = tracedEndpoint(context.Background(), struct{}{})

	// span 6 with OperationNameFunc option
	mw = kitot.TraceEndpoint(
		tracer,
		span6,
		kitot.WithOperationNameFunc(func(ctx context.Context, name string) string {
			return fmt.Sprintf("%s-%s", "new", name)
		}),
	)
	tracedEndpoint = mw(endpoint.Nop)
	_, _ = tracedEndpoint(context.Background(), struct{}{})

	// span 7 with Tags options
	mw = kitot.TraceEndpoint(
		tracer,
		span7,
		kitot.WithTags(map[string]interface{}{
			"tag1": "tag1",
			"tag2": "tag2",
		}),
		kitot.WithTags(map[string]interface{}{
			"tag3": "tag3",
		}),
	)
	tracedEndpoint = mw(endpoint.Nop)
	_, _ = tracedEndpoint(context.Background(), struct{}{})

	// span 8 with TagsFunc options
	mw = kitot.TraceEndpoint(
		tracer,
		span8,
		kitot.WithTags(map[string]interface{}{
			"tag1": "tag1",
			"tag2": "tag2",
		}),
		kitot.WithTags(map[string]interface{}{
			"tag3": "tag3",
		}),
		kitot.WithTagsFunc(func(ctx context.Context) opentracing.Tags {
			return map[string]interface{}{
				"tag4": "tag4",
			}
		}),
	)
	tracedEndpoint = mw(endpoint.Nop)
	_, _ = tracedEndpoint(context.Background(), struct{}{})

	finishedSpans := tracer.FinishedSpans()
	if want, have := 8, len(finishedSpans); want != have {
		t.Fatalf("Want %v span(s), found %v", want, have)
	}

	// test span 1
	span := finishedSpans[0]

	if want, have := span1, span.OperationName; want != have {
		t.Fatalf("Want %q, have %q", want, have)
	}

	// test span 2
	span = finishedSpans[1]

	if want, have := span2, span.OperationName; want != have {
		t.Fatalf("Want %q, have %q", want, have)
	}

	if want, have := true, span.Tag("error"); want != have {
		t.Fatalf("Want %v, have %v", want, have)
	}

	// test span 3
	span = finishedSpans[2]

	if want, have := span3, span.OperationName; want != have {
		t.Fatalf("Want %q, have %q", want, have)
	}

	if want, have := true, span.Tag("error"); want != have {
		t.Fatalf("Want %v, have %v", want, have)
	}

	if want, have := 1, len(span.Logs()); want != have {
		t.Fatalf("incorrect logs count, wanted %d, got %d", want, have)
	}

	if want, have := []otlog.Field{
		otlog.String("event", "error"),
		otlog.String("error.object", "some error (previously: some error; some error; some error; some error)"),
		otlog.String("gokit.retry.error.1", "some error"),
		otlog.String("gokit.retry.error.2", "some error"),
		otlog.String("gokit.retry.error.3", "some error"),
		otlog.String("gokit.retry.error.4", "some error"),
		otlog.String("gokit.retry.error.5", "some error"),
	}, span.Logs()[0].Fields; reflect.DeepEqual(want, have) {
		t.Fatalf("Want %q, have %q", want, have)
	}

	// test span 4
	span = finishedSpans[3]

	if want, have := span4, span.OperationName; want != have {
		t.Fatalf("Want %q, have %q", want, have)
	}

	if want, have := true, span.Tag("error"); want != have {
		t.Fatalf("Want %v, have %v", want, have)
	}

	if want, have := 2, len(span.Logs()); want != have {
		t.Fatalf("incorrect logs count, wanted %d, got %d", want, have)
	}

	if want, have := []otlog.Field{
		otlog.String("gokit.business.error", "some business error"),
	}, span.Logs()[0].Fields; reflect.DeepEqual(want, have) {
		t.Fatalf("Want %q, have %q", want, have)
	}

	if want, have := []otlog.Field{
		otlog.String("event", "error"),
		otlog.String("error.object", "some business error"),
	}, span.Logs()[1].Fields; reflect.DeepEqual(want, have) {
		t.Fatalf("Want %q, have %q", want, have)
	}

	// test span 5
	span = finishedSpans[4]

	if want, have := span5, span.OperationName; want != have {
		t.Fatalf("Want %q, have %q", want, have)
	}

	if want, have := (interface{})(nil), span.Tag("error"); want != have {
		t.Fatalf("Want %q, have %q", want, have)
	}

	if want, have := 1, len(span.Logs()); want != have {
		t.Fatalf("incorrect logs count, wanted %d, got %d", want, have)
	}

	if want, have := []otlog.Field{
		otlog.String("gokit.business.error", "some business error"),
	}, span.Logs()[0].Fields; reflect.DeepEqual(want, have) {
		t.Fatalf("Want %q, have %q", want, have)
	}

	// test span 6
	span = finishedSpans[5]

	if want, have := fmt.Sprintf("%s-%s", "new", span6), span.OperationName; want != have {
		t.Fatalf("Want %q, have %q", want, have)
	}

	// test span 7
	span = finishedSpans[6]

	if want, have := span7, span.OperationName; want != have {
		t.Fatalf("Want %q, have %q", want, have)
	}

	if want, have := map[string]interface{}{
		"tag1": "tag1",
		"tag2": "tag2",
		"tag3": "tag3",
	}, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
		t.Fatalf("Want %q, have %q", want, have)
	}

	// test span 8
	span = finishedSpans[7]

	if want, have := span8, span.OperationName; want != have {
		t.Fatalf("Want %q, have %q", want, have)
	}

	if want, have := map[string]interface{}{
		"tag1": "tag1",
		"tag2": "tag2",
		"tag3": "tag3",
		"tag4": "tag4",
	}, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
		t.Fatalf("Want %q, have %q", want, have)
	}
}

func TestTraceServer(t *testing.T) {
	tracer := mocktracer.New()

	// Empty/background context.
	tracedEndpoint := kitot.TraceServer(tracer, "testOp")(endpoint.Nop)
	if _, err := tracedEndpoint(context.Background(), struct{}{}); err != nil {
		t.Fatal(err)
	}

	// tracedEndpoint created a new Span.
	finishedSpans := tracer.FinishedSpans()
	if want, have := 1, len(finishedSpans); want != have {
		t.Fatalf("Want %v span(s), found %v", want, have)
	}

	span := finishedSpans[0]

	if want, have := "testOp", span.OperationName; want != have {
		t.Fatalf("Want %q, have %q", want, have)
	}

	if want, have := map[string]interface{}{
		otext.SpanKindRPCServer.Key: otext.SpanKindRPCServer.Value,
	}, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
		t.Fatalf("Want %q, have %q", want, have)
	}
}

func TestTraceClient(t *testing.T) {
	tracer := mocktracer.New()

	// Empty/background context.
	tracedEndpoint := kitot.TraceClient(tracer, "testOp")(endpoint.Nop)
	if _, err := tracedEndpoint(context.Background(), struct{}{}); err != nil {
		t.Fatal(err)
	}

	// tracedEndpoint created a new Span.
	finishedSpans := tracer.FinishedSpans()
	if want, have := 1, len(finishedSpans); want != have {
		t.Fatalf("Want %v span(s), found %v", want, have)
	}

	span := finishedSpans[0]

	if want, have := "testOp", span.OperationName; want != have {
		t.Fatalf("Want %q, have %q", want, have)
	}

	if want, have := map[string]interface{}{
		otext.SpanKindRPCClient.Key: otext.SpanKindRPCClient.Value,
	}, span.Tags(); fmt.Sprint(want) != fmt.Sprint(have) {
		t.Fatalf("Want %q, have %q", want, have)
	}
}