Codebase list golang-github-go-kit-kit / acf75de
Merge pull request #375 from Fenthick/tracing-tags Correct http tags in tracing spans Peter Bourgon authored 7 years ago GitHub committed 7 years ago
3 changed file(s) with 53 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 }
11
22 import (
33 "net/http"
4 "reflect"
45 "testing"
56
67 "github.com/opentracing/opentracing-go"
8 "github.com/opentracing/opentracing-go/ext"
79 "github.com/opentracing/opentracing-go/mocktracer"
810 "golang.org/x/net/context"
911
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 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 }