Codebase list golang-github-go-kit-kit / 479593c
Add tests for the HTTP-OT bridge Ben Sigelman 7 years ago
1 changed file(s) with 54 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 package opentracing_test
1
2 import (
3 "net/http"
4 "testing"
5
6 kitot "github.com/go-kit/kit/tracing/opentracing"
7 kithttp "github.com/go-kit/kit/transport/http"
8 "github.com/opentracing/opentracing-go"
9 "github.com/opentracing/opentracing-go/mocktracer"
10 "golang.org/x/net/context"
11 )
12
13 func TestTraceHTTPRequestRoundtrip(t *testing.T) {
14 tracer := mocktracer.New()
15
16 // Initialize the ctx with a Span to inject.
17 beforeSpan := tracer.StartSpan("to_inject").(*mocktracer.MockSpan)
18 defer beforeSpan.Finish()
19 beforeCtx := opentracing.ContextWithSpan(context.Background(), beforeSpan)
20
21 var toHTTPFunc kithttp.RequestFunc = kitot.ToHTTPRequest(tracer)
22 req, _ := http.NewRequest("GET", "http://test.biz/url", nil)
23 // Call the RequestFunc.
24 afterCtx := toHTTPFunc(beforeCtx, req)
25
26 // The Span should not have changed.
27 afterSpan := opentracing.SpanFromContext(afterCtx)
28 if beforeSpan != afterSpan {
29 t.Errorf("Should not swap in a new span")
30 }
31
32 // No spans should have finished yet.
33 if want, have := 0, len(tracer.FinishedSpans); want != have {
34 t.Errorf("Want %v span(s), found %v", want, have)
35 }
36
37 // Use FromHTTPRequest to verify that we can join with the trace given a req.
38 var fromHTTPFunc kithttp.RequestFunc = kitot.FromHTTPRequest(tracer, "joined")
39 joinCtx := fromHTTPFunc(afterCtx, req)
40 joinedSpan := opentracing.SpanFromContext(joinCtx).(*mocktracer.MockSpan)
41
42 if joinedSpan.SpanID == beforeSpan.SpanID {
43 t.Error("SpanID should have changed", joinedSpan.SpanID, beforeSpan.SpanID)
44 }
45
46 // Check that the parent/child relationship is as expected for the joined span.
47 if want, have := beforeSpan.SpanID, joinedSpan.ParentID; want != have {
48 t.Errorf("Want ParentID %q, have %q", want, have)
49 }
50 if want, have := "joined", joinedSpan.OperationName; want != have {
51 t.Errorf("Want %q, have %q", want, have)
52 }
53 }