4 | 4 |
"testing"
|
5 | 5 |
|
6 | 6 |
"github.com/opentracing/opentracing-go"
|
|
7 |
"github.com/opentracing/opentracing-go/ext"
|
7 | 8 |
"github.com/opentracing/opentracing-go/mocktracer"
|
|
9 |
"github.com/stretchr/testify/assert"
|
8 | 10 |
"golang.org/x/net/context"
|
9 | 11 |
|
10 | 12 |
"github.com/go-kit/kit/log"
|
|
22 | 24 |
beforeCtx := opentracing.ContextWithSpan(context.Background(), beforeSpan)
|
23 | 25 |
|
24 | 26 |
toHTTPFunc := kitot.ToHTTPRequest(tracer, logger)
|
25 | |
req, _ := http.NewRequest("GET", "http://test.biz/url", nil)
|
|
27 |
req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
|
26 | 28 |
// Call the RequestFunc.
|
27 | 29 |
afterCtx := toHTTPFunc(beforeCtx, req)
|
28 | 30 |
|
29 | 31 |
// The Span should not have changed.
|
30 | 32 |
afterSpan := opentracing.SpanFromContext(afterCtx)
|
31 | 33 |
if beforeSpan != afterSpan {
|
32 | |
t.Errorf("Should not swap in a new span")
|
|
34 |
t.Error("Should not swap in a new span")
|
33 | 35 |
}
|
34 | 36 |
|
35 | 37 |
// No spans should have finished yet.
|
|
61 | 63 |
t.Errorf("Want %q, have %q", want, have)
|
62 | 64 |
}
|
63 | 65 |
}
|
|
66 |
|
|
67 |
func TestToHTTPRequestTags(t *testing.T) {
|
|
68 |
tracer := mocktracer.New()
|
|
69 |
span := tracer.StartSpan("to_inject").(*mocktracer.MockSpan)
|
|
70 |
defer span.Finish()
|
|
71 |
ctx := opentracing.ContextWithSpan(context.Background(), span)
|
|
72 |
req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
|
|
73 |
|
|
74 |
kitot.ToHTTPRequest(tracer, log.NewNopLogger())(ctx, req)
|
|
75 |
|
|
76 |
assert.Equal(t, map[string]interface{}{
|
|
77 |
string(ext.HTTPMethod): "GET",
|
|
78 |
string(ext.HTTPUrl): "http://test.biz/path",
|
|
79 |
string(ext.PeerHostname): "test.biz",
|
|
80 |
}, span.Tags())
|
|
81 |
}
|
|
82 |
|
|
83 |
func TestFromHTTPRequestTags(t *testing.T) {
|
|
84 |
tracer := mocktracer.New()
|
|
85 |
parentSpan := tracer.StartSpan("to_extract").(*mocktracer.MockSpan)
|
|
86 |
defer parentSpan.Finish()
|
|
87 |
req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
|
|
88 |
tracer.Inject(parentSpan.Context(), opentracing.TextMap, opentracing.HTTPHeadersCarrier(req.Header))
|
|
89 |
|
|
90 |
ctx := kitot.FromHTTPRequest(tracer, "op", log.NewNopLogger())(context.Background(), req)
|
|
91 |
opentracing.SpanFromContext(ctx).Finish()
|
|
92 |
|
|
93 |
childSpan := tracer.FinishedSpans()[0]
|
|
94 |
assert.Equal(t, "op", childSpan.OperationName)
|
|
95 |
assert.Equal(t, map[string]interface{}{
|
|
96 |
string(ext.HTTPMethod): "GET",
|
|
97 |
string(ext.HTTPUrl): "http://test.biz/path",
|
|
98 |
string(ext.SpanKind): ext.SpanKindRPCServerEnum,
|
|
99 |
}, childSpan.Tags())
|
|
100 |
}
|