1 | 1 |
|
2 | 2 |
import (
|
3 | 3 |
"net/http"
|
|
4 |
"reflect"
|
4 | 5 |
"testing"
|
5 | 6 |
|
6 | 7 |
"github.com/opentracing/opentracing-go"
|
|
8 |
"github.com/opentracing/opentracing-go/ext"
|
7 | 9 |
"github.com/opentracing/opentracing-go/mocktracer"
|
8 | 10 |
"golang.org/x/net/context"
|
9 | 11 |
|
|
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 |
expectedTags := map[string]interface{}{
|
|
77 |
string(ext.HTTPMethod): "GET",
|
|
78 |
string(ext.HTTPUrl): "http://test.biz/path",
|
|
79 |
string(ext.PeerHostname): "test.biz",
|
|
80 |
}
|
|
81 |
if !reflect.DeepEqual(expectedTags, span.Tags()) {
|
|
82 |
t.Errorf("Want %q, have %q", expectedTags, span.Tags())
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
func TestFromHTTPRequestTags(t *testing.T) {
|
|
87 |
tracer := mocktracer.New()
|
|
88 |
parentSpan := tracer.StartSpan("to_extract").(*mocktracer.MockSpan)
|
|
89 |
defer parentSpan.Finish()
|
|
90 |
req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
|
|
91 |
tracer.Inject(parentSpan.Context(), opentracing.TextMap, opentracing.HTTPHeadersCarrier(req.Header))
|
|
92 |
|
|
93 |
ctx := kitot.FromHTTPRequest(tracer, "op", log.NewNopLogger())(context.Background(), req)
|
|
94 |
opentracing.SpanFromContext(ctx).Finish()
|
|
95 |
|
|
96 |
childSpan := tracer.FinishedSpans()[0]
|
|
97 |
expectedTags := map[string]interface{}{
|
|
98 |
string(ext.HTTPMethod): "GET",
|
|
99 |
string(ext.HTTPUrl): "http://test.biz/path",
|
|
100 |
string(ext.SpanKind): ext.SpanKindRPCServerEnum,
|
|
101 |
}
|
|
102 |
if !reflect.DeepEqual(expectedTags, childSpan.Tags()) {
|
|
103 |
t.Errorf("Want %q, have %q", expectedTags, childSpan.Tags())
|
|
104 |
}
|
|
105 |
if want, have := "op", childSpan.OperationName; want != have {
|
|
106 |
t.Errorf("Want %q, have %q", want, have)
|
|
107 |
}
|
|
108 |
}
|