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