...

Source file src/github.com/go-kit/kit/tracing/opentracing/http_test.go

Documentation: github.com/go-kit/kit/tracing/opentracing

     1  package opentracing_test
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"reflect"
     7  	"testing"
     8  
     9  	opentracing "github.com/opentracing/opentracing-go"
    10  	"github.com/opentracing/opentracing-go/ext"
    11  	"github.com/opentracing/opentracing-go/mocktracer"
    12  
    13  	kitot "github.com/go-kit/kit/tracing/opentracing"
    14  	"github.com/go-kit/log"
    15  )
    16  
    17  func TestTraceHTTPRequestRoundtrip(t *testing.T) {
    18  	logger := log.NewNopLogger()
    19  	tracer := mocktracer.New()
    20  
    21  	// Initialize the ctx with a Span to inject.
    22  	beforeSpan := tracer.StartSpan("to_inject").(*mocktracer.MockSpan)
    23  	defer beforeSpan.Finish()
    24  	beforeSpan.SetBaggageItem("baggage", "check")
    25  	beforeCtx := opentracing.ContextWithSpan(context.Background(), beforeSpan)
    26  
    27  	toHTTPFunc := kitot.ContextToHTTP(tracer, logger)
    28  	req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
    29  	// Call the RequestFunc.
    30  	afterCtx := toHTTPFunc(beforeCtx, req)
    31  
    32  	// The Span should not have changed.
    33  	afterSpan := opentracing.SpanFromContext(afterCtx)
    34  	if beforeSpan != afterSpan {
    35  		t.Error("Should not swap in a new span")
    36  	}
    37  
    38  	// No spans should have finished yet.
    39  	finishedSpans := tracer.FinishedSpans()
    40  	if want, have := 0, len(finishedSpans); want != have {
    41  		t.Errorf("Want %v span(s), found %v", want, have)
    42  	}
    43  
    44  	// Use HTTPToContext to verify that we can join with the trace given a req.
    45  	fromHTTPFunc := kitot.HTTPToContext(tracer, "joined", logger)
    46  	joinCtx := fromHTTPFunc(afterCtx, req)
    47  	joinedSpan := opentracing.SpanFromContext(joinCtx).(*mocktracer.MockSpan)
    48  
    49  	joinedContext := joinedSpan.Context().(mocktracer.MockSpanContext)
    50  	beforeContext := beforeSpan.Context().(mocktracer.MockSpanContext)
    51  
    52  	if joinedContext.SpanID == beforeContext.SpanID {
    53  		t.Error("SpanID should have changed", joinedContext.SpanID, beforeContext.SpanID)
    54  	}
    55  
    56  	// Check that the parent/child relationship is as expected for the joined span.
    57  	if want, have := beforeContext.SpanID, joinedSpan.ParentID; want != have {
    58  		t.Errorf("Want ParentID %q, have %q", want, have)
    59  	}
    60  	if want, have := "joined", joinedSpan.OperationName; want != have {
    61  		t.Errorf("Want %q, have %q", want, have)
    62  	}
    63  	if want, have := "check", joinedSpan.BaggageItem("baggage"); want != have {
    64  		t.Errorf("Want %q, have %q", want, have)
    65  	}
    66  }
    67  
    68  func TestContextToHTTPTags(t *testing.T) {
    69  	tracer := mocktracer.New()
    70  	span := tracer.StartSpan("to_inject").(*mocktracer.MockSpan)
    71  	defer span.Finish()
    72  	ctx := opentracing.ContextWithSpan(context.Background(), span)
    73  	req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
    74  
    75  	kitot.ContextToHTTP(tracer, log.NewNopLogger())(ctx, req)
    76  
    77  	expectedTags := map[string]interface{}{
    78  		string(ext.HTTPMethod):   "GET",
    79  		string(ext.HTTPUrl):      "http://test.biz/path",
    80  		string(ext.PeerHostname): "test.biz",
    81  	}
    82  	if !reflect.DeepEqual(expectedTags, span.Tags()) {
    83  		t.Errorf("Want %q, have %q", expectedTags, span.Tags())
    84  	}
    85  }
    86  
    87  func TestHTTPToContextTags(t *testing.T) {
    88  	tracer := mocktracer.New()
    89  	parentSpan := tracer.StartSpan("to_extract").(*mocktracer.MockSpan)
    90  	defer parentSpan.Finish()
    91  	req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
    92  	tracer.Inject(parentSpan.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
    93  
    94  	ctx := kitot.HTTPToContext(tracer, "op", log.NewNopLogger())(context.Background(), req)
    95  	opentracing.SpanFromContext(ctx).Finish()
    96  
    97  	childSpan := tracer.FinishedSpans()[0]
    98  	expectedTags := map[string]interface{}{
    99  		string(ext.HTTPMethod): "GET",
   100  		string(ext.HTTPUrl):    "http://test.biz/path",
   101  		string(ext.SpanKind):   ext.SpanKindRPCServerEnum,
   102  	}
   103  	if !reflect.DeepEqual(expectedTags, childSpan.Tags()) {
   104  		t.Errorf("Want %q, have %q", expectedTags, childSpan.Tags())
   105  	}
   106  	if want, have := "op", childSpan.OperationName; want != have {
   107  		t.Errorf("Want %q, have %q", want, have)
   108  	}
   109  }
   110  

View as plain text