1package opencensus_test
2
3import (
4	"context"
5	"errors"
6	"testing"
7	"time"
8
9	"go.opencensus.io/trace"
10
11	"github.com/go-kit/kit/endpoint"
12	"github.com/go-kit/kit/sd"
13	"github.com/go-kit/kit/sd/lb"
14	"github.com/go-kit/kit/tracing/opencensus"
15)
16
17const (
18	span1 = ""
19	span2 = "SPAN-2"
20	span3 = "SPAN-3"
21	span4 = "SPAN-4"
22	span5 = "SPAN-5"
23)
24
25var (
26	err1 = errors.New("some error")
27	err2 = errors.New("other error")
28	err3 = errors.New("some business error")
29	err4 = errors.New("other business error")
30)
31
32// compile time assertion
33var _ endpoint.Failer = failedResponse{}
34
35type failedResponse struct {
36	err error
37}
38
39func (r failedResponse) Failed() error { return r.err }
40
41func passEndpoint(_ context.Context, req interface{}) (interface{}, error) {
42	if err, _ := req.(error); err != nil {
43		return nil, err
44	}
45	return req, nil
46}
47
48func TestTraceEndpoint(t *testing.T) {
49	ctx := context.Background()
50
51	e := &recordingExporter{}
52	trace.RegisterExporter(e)
53	trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
54
55	// span 1
56	span1Attrs := []trace.Attribute{
57		trace.StringAttribute("string", "value"),
58		trace.Int64Attribute("int64", 42),
59	}
60	mw := opencensus.TraceEndpoint(
61		span1, opencensus.WithEndpointAttributes(span1Attrs...),
62	)
63	mw(endpoint.Nop)(ctx, nil)
64
65	// span 2
66	opts := opencensus.EndpointOptions{}
67	mw = opencensus.TraceEndpoint(span2, opencensus.WithEndpointConfig(opts))
68	mw(passEndpoint)(ctx, err1)
69
70	// span3
71	mw = opencensus.TraceEndpoint(span3)
72	ep := lb.Retry(5, 1*time.Second, lb.NewRoundRobin(sd.FixedEndpointer{passEndpoint}))
73	mw(ep)(ctx, err2)
74
75	// span4
76	mw = opencensus.TraceEndpoint(span4)
77	mw(passEndpoint)(ctx, failedResponse{err: err3})
78
79	// span4
80	mw = opencensus.TraceEndpoint(span5, opencensus.WithIgnoreBusinessError(true))
81	mw(passEndpoint)(ctx, failedResponse{err: err4})
82
83	// check span count
84	spans := e.Flush()
85	if want, have := 5, len(spans); want != have {
86		t.Fatalf("incorrected number of spans, wanted %d, got %d", want, have)
87	}
88
89	// test span 1
90	span := spans[0]
91	if want, have := int32(trace.StatusCodeOK), span.Code; want != have {
92		t.Errorf("incorrect status code, wanted %d, got %d", want, have)
93	}
94
95	if want, have := opencensus.TraceEndpointDefaultName, span.Name; want != have {
96		t.Errorf("incorrect span name, wanted %q, got %q", want, have)
97	}
98
99	if want, have := 2, len(span.Attributes); want != have {
100		t.Fatalf("incorrect attribute count, wanted %d, got %d", want, have)
101	}
102
103	// test span 2
104	span = spans[1]
105	if want, have := int32(trace.StatusCodeUnknown), span.Code; want != have {
106		t.Errorf("incorrect status code, wanted %d, got %d", want, have)
107	}
108
109	if want, have := span2, span.Name; want != have {
110		t.Errorf("incorrect span name, wanted %q, got %q", want, have)
111	}
112
113	if want, have := 0, len(span.Attributes); want != have {
114		t.Fatalf("incorrect attribute count, wanted %d, got %d", want, have)
115	}
116
117	// test span 3
118	span = spans[2]
119	if want, have := int32(trace.StatusCodeUnknown), span.Code; want != have {
120		t.Errorf("incorrect status code, wanted %d, got %d", want, have)
121	}
122
123	if want, have := span3, span.Name; want != have {
124		t.Errorf("incorrect span name, wanted %q, got %q", want, have)
125	}
126
127	if want, have := 5, len(span.Attributes); want != have {
128		t.Fatalf("incorrect attribute count, wanted %d, got %d", want, have)
129	}
130
131	// test span 4
132	span = spans[3]
133	if want, have := int32(trace.StatusCodeUnknown), span.Code; want != have {
134		t.Errorf("incorrect status code, wanted %d, got %d", want, have)
135	}
136
137	if want, have := span4, span.Name; want != have {
138		t.Errorf("incorrect span name, wanted %q, got %q", want, have)
139	}
140
141	if want, have := 1, len(span.Attributes); want != have {
142		t.Fatalf("incorrect attribute count, wanted %d, got %d", want, have)
143	}
144
145	// test span 5
146	span = spans[4]
147	if want, have := int32(trace.StatusCodeOK), span.Code; want != have {
148		t.Errorf("incorrect status code, wanted %d, got %d", want, have)
149	}
150
151	if want, have := span5, span.Name; want != have {
152		t.Errorf("incorrect span name, wanted %q, got %q", want, have)
153	}
154
155	if want, have := 1, len(span.Attributes); want != have {
156		t.Fatalf("incorrect attribute count, wanted %d, got %d", want, have)
157	}
158
159}
160