...

Source file src/github.com/go-kit/kit/tracing/zipkin/grpc_test.go

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

     1  package zipkin_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	zipkin "github.com/openzipkin/zipkin-go"
     8  	"github.com/openzipkin/zipkin-go/propagation/b3"
     9  	"github.com/openzipkin/zipkin-go/reporter/recorder"
    10  	"google.golang.org/grpc"
    11  	"google.golang.org/grpc/metadata"
    12  
    13  	"github.com/go-kit/kit/endpoint"
    14  	kitzipkin "github.com/go-kit/kit/tracing/zipkin"
    15  	grpctransport "github.com/go-kit/kit/transport/grpc"
    16  )
    17  
    18  type dummy struct{}
    19  
    20  func unaryInterceptor(
    21  	ctx context.Context, method string, req, reply interface{},
    22  	cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption,
    23  ) error {
    24  	return nil
    25  }
    26  
    27  func TestGRPCClientTrace(t *testing.T) {
    28  	rec := recorder.NewReporter()
    29  	defer rec.Close()
    30  
    31  	tr, _ := zipkin.NewTracer(rec)
    32  
    33  	clientTracer := kitzipkin.GRPCClientTrace(tr)
    34  
    35  	cc, err := grpc.Dial(
    36  		"",
    37  		grpc.WithUnaryInterceptor(unaryInterceptor),
    38  		grpc.WithInsecure(),
    39  	)
    40  	if err != nil {
    41  		t.Fatalf("unable to create gRPC dialer: %s", err.Error())
    42  	}
    43  
    44  	ep := grpctransport.NewClient(
    45  		cc,
    46  		"dummyService",
    47  		"dummyMethod",
    48  		func(context.Context, interface{}) (interface{}, error) { return nil, nil },
    49  		func(context.Context, interface{}) (interface{}, error) { return nil, nil },
    50  		dummy{},
    51  		clientTracer,
    52  	).Endpoint()
    53  
    54  	parentSpan := tr.StartSpan("test")
    55  	ctx := zipkin.NewContext(context.Background(), parentSpan)
    56  
    57  	if _, err = ep(ctx, nil); err != nil {
    58  		t.Errorf("unexpected error: %s", err.Error())
    59  	}
    60  
    61  	spans := rec.Flush()
    62  	if want, have := 1, len(spans); want != have {
    63  		t.Fatalf("incorrect number of spans, want %d, have %d", want, have)
    64  	}
    65  
    66  	if spans[0].SpanContext.ParentID == nil {
    67  		t.Fatalf("incorrect parent ID, want %s have nil", parentSpan.Context().ID)
    68  	}
    69  
    70  	if want, have := parentSpan.Context().ID, *spans[0].SpanContext.ParentID; want != have {
    71  		t.Fatalf("incorrect parent ID, want %s, have %s", want, have)
    72  	}
    73  }
    74  
    75  func TestGRPCServerTrace(t *testing.T) {
    76  	rec := recorder.NewReporter()
    77  	defer rec.Close()
    78  
    79  	tr, _ := zipkin.NewTracer(rec)
    80  
    81  	serverTracer := kitzipkin.GRPCServerTrace(tr)
    82  
    83  	server := grpctransport.NewServer(
    84  		endpoint.Nop,
    85  		func(context.Context, interface{}) (interface{}, error) { return nil, nil },
    86  		func(context.Context, interface{}) (interface{}, error) { return nil, nil },
    87  		serverTracer,
    88  	)
    89  
    90  	md := metadata.MD{}
    91  	parentSpan := tr.StartSpan("test")
    92  
    93  	b3.InjectGRPC(&md)(parentSpan.Context())
    94  
    95  	ctx := metadata.NewIncomingContext(context.Background(), md)
    96  	server.ServeGRPC(ctx, nil)
    97  
    98  	spans := rec.Flush()
    99  
   100  	if want, have := 1, len(spans); want != have {
   101  		t.Fatalf("incorrect number of spans, want %d, have %d", want, have)
   102  	}
   103  
   104  	if want, have := parentSpan.Context().TraceID, spans[0].SpanContext.TraceID; want != have {
   105  		t.Errorf("incorrect TraceID, want %+v, have %+v", want, have)
   106  	}
   107  
   108  	if want, have := parentSpan.Context().ID, spans[0].SpanContext.ID; want != have {
   109  		t.Errorf("incorrect span ID, want %d, have %d", want, have)
   110  	}
   111  }
   112  

View as plain text