Codebase list golang-github-go-kit-kit / 756104ab-e3ba-487b-8d5b-cc84465cfc29/main tracing / opentracing / grpc_test.go
756104ab-e3ba-487b-8d5b-cc84465cfc29/main

Tree @756104ab-e3ba-487b-8d5b-cc84465cfc29/main (Download .tar.gz)

grpc_test.go @756104ab-e3ba-487b-8d5b-cc84465cfc29/main

2a81195
 
 
4a29204
2a81195
 
28a1b0d
 
2a81195
 
060a8ce
2a81195
 
 
 
060a8ce
2a81195
 
 
 
 
c718700
2a81195
 
0ecbed2
2a81195
 
 
 
 
 
 
023cbf0
2a81195
 
 
f8893ab
142db49
2a81195
 
 
0ecbed2
 
8864ce8
2a81195
 
c718700
 
060a8ce
 
 
2a81195
 
 
060a8ce
2a81195
 
 
 
 
c718700
79f8b0c
 
2a81195
package opentracing_test

import (
	"context"
	"testing"

	"github.com/opentracing/opentracing-go"
	"github.com/opentracing/opentracing-go/mocktracer"
	"google.golang.org/grpc/metadata"

	"github.com/go-kit/kit/log"
	kitot "github.com/go-kit/kit/tracing/opentracing"
)

func TestTraceGRPCRequestRoundtrip(t *testing.T) {
	logger := log.NewNopLogger()
	tracer := mocktracer.New()

	// Initialize the ctx with a Span to inject.
	beforeSpan := tracer.StartSpan("to_inject").(*mocktracer.MockSpan)
	defer beforeSpan.Finish()
	beforeSpan.SetBaggageItem("baggage", "check")
	beforeCtx := opentracing.ContextWithSpan(context.Background(), beforeSpan)

	toGRPCFunc := kitot.ContextToGRPC(tracer, logger)
	md := metadata.Pairs()
	// Call the RequestFunc.
	afterCtx := toGRPCFunc(beforeCtx, &md)

	// The Span should not have changed.
	afterSpan := opentracing.SpanFromContext(afterCtx)
	if beforeSpan != afterSpan {
		t.Error("Should not swap in a new span")
	}

	// No spans should have finished yet.
	finishedSpans := tracer.FinishedSpans()
	if want, have := 0, len(finishedSpans); want != have {
		t.Errorf("Want %v span(s), found %v", want, have)
	}

	// Use GRPCToContext to verify that we can join with the trace given MD.
	fromGRPCFunc := kitot.GRPCToContext(tracer, "joined", logger)
	joinCtx := fromGRPCFunc(afterCtx, md)
	joinedSpan := opentracing.SpanFromContext(joinCtx).(*mocktracer.MockSpan)

	joinedContext := joinedSpan.Context().(mocktracer.MockSpanContext)
	beforeContext := beforeSpan.Context().(mocktracer.MockSpanContext)

	if joinedContext.SpanID == beforeContext.SpanID {
		t.Error("SpanID should have changed", joinedContext.SpanID, beforeContext.SpanID)
	}

	// Check that the parent/child relationship is as expected for the joined span.
	if want, have := beforeContext.SpanID, joinedSpan.ParentID; want != have {
		t.Errorf("Want ParentID %q, have %q", want, have)
	}
	if want, have := "joined", joinedSpan.OperationName; want != have {
		t.Errorf("Want %q, have %q", want, have)
	}
	if want, have := "check", joinedSpan.BaggageItem("baggage"); want != have {
		t.Errorf("Want %q, have %q", want, have)
	}
}