Codebase list golang-github-go-kit-kit / 023cbf0
Correct http tags in tracing spans Set correct value of HTTP URL tag that is added to client span in ToHTTPRequest() function. Add new opentracing tags in tracing request functions. Daniel Furman 7 years ago
3 changed file(s) with 45 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
2929 // The Span should not have changed.
3030 afterSpan := opentracing.SpanFromContext(afterCtx)
3131 if beforeSpan != afterSpan {
32 t.Errorf("Should not swap in a new span")
32 t.Error("Should not swap in a new span")
3333 }
3434
3535 // No spans should have finished yet.
2020 // Try to find a Span in the Context.
2121 if span := opentracing.SpanFromContext(ctx); span != nil {
2222 // Add standard OpenTracing tags.
23 ext.HTTPMethod.Set(span, req.URL.RequestURI())
23 ext.HTTPMethod.Set(span, req.Method)
24 ext.HTTPUrl.Set(span, req.URL.String())
2425 host, portString, err := net.SplitHostPort(req.URL.Host)
2526 if err == nil {
2627 ext.PeerHostname.Set(span, host)
6061 if err != nil && err != opentracing.ErrSpanContextNotFound {
6162 logger.Log("err", err)
6263 }
64
6365 span = tracer.StartSpan(operationName, ext.RPCServerOption(wireContext))
66 ext.HTTPMethod.Set(span, req.Method)
67 ext.HTTPUrl.Set(span, req.URL.String())
6468 return opentracing.ContextWithSpan(ctx, span)
6569 }
6670 }
44 "testing"
55
66 "github.com/opentracing/opentracing-go"
7 "github.com/opentracing/opentracing-go/ext"
78 "github.com/opentracing/opentracing-go/mocktracer"
9 "github.com/stretchr/testify/assert"
810 "golang.org/x/net/context"
911
1012 "github.com/go-kit/kit/log"
2224 beforeCtx := opentracing.ContextWithSpan(context.Background(), beforeSpan)
2325
2426 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)
2628 // Call the RequestFunc.
2729 afterCtx := toHTTPFunc(beforeCtx, req)
2830
2931 // The Span should not have changed.
3032 afterSpan := opentracing.SpanFromContext(afterCtx)
3133 if beforeSpan != afterSpan {
32 t.Errorf("Should not swap in a new span")
34 t.Error("Should not swap in a new span")
3335 }
3436
3537 // No spans should have finished yet.
6163 t.Errorf("Want %q, have %q", want, have)
6264 }
6365 }
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 }