diff --git a/httpbp/client_middlewares.go b/httpbp/client_middlewares.go index 3944a1fab..2ffc19f95 100644 --- a/httpbp/client_middlewares.go +++ b/httpbp/client_middlewares.go @@ -12,14 +12,12 @@ import ( "time" "github.com/avast/retry-go" - "github.com/opentracing/opentracing-go" "github.com/prometheus/client_golang/prometheus" "github.com/reddit/baseplate.go/breakerbp" //lint:ignore SA1019 This library is internal only, not actually deprecated "github.com/reddit/baseplate.go/internalv2compat" "github.com/reddit/baseplate.go/retrybp" - "github.com/reddit/baseplate.go/tracing" "github.com/reddit/baseplate.go/transport" ) @@ -266,28 +264,16 @@ func MaxConcurrency(maxConcurrency int64) ClientMiddleware { // MonitorClient is an HTTP client middleware that wraps HTTP requests in a // client span. +// +// This middleware always use the injected v2 tracing http client middleware. +// If there's no v2 tracing http client middleware injected, it's no-op. func MonitorClient(slug string) ClientMiddleware { if mw := internalv2compat.V2TracingHTTPClientMiddleware(); mw != nil { return mw } return func(next http.RoundTripper) http.RoundTripper { - return roundTripperFunc(func(req *http.Request) (resp *http.Response, err error) { - span, ctx := opentracing.StartSpanFromContext( - req.Context(), - slug+".request", - tracing.SpanTypeOption{Type: tracing.SpanTypeClient}, - ) - span.SetTag("http.method", req.Method) - span.SetTag("http.url", req.URL) - - defer func() { - span.FinishWithOptions(tracing.FinishOptions{ - Ctx: req.Context(), - Err: err, - }.Convert()) - }() - return next.RoundTrip(req.WithContext(ctx)) - }) + // no-op + return next } } diff --git a/httpbp/client_middlewares_test.go b/httpbp/client_middlewares_test.go index bfd9efc59..735d2fe9f 100644 --- a/httpbp/client_middlewares_test.go +++ b/httpbp/client_middlewares_test.go @@ -2,8 +2,6 @@ package httpbp import ( "bytes" - "context" - "encoding/json" "errors" "io" "net/http" @@ -18,8 +16,6 @@ import ( "github.com/sony/gobreaker" "github.com/reddit/baseplate.go/breakerbp" - "github.com/reddit/baseplate.go/mqsend" - "github.com/reddit/baseplate.go/tracing" ) func TestNewClient(t *testing.T) { @@ -57,18 +53,6 @@ func TestNewClient(t *testing.T) { })) defer server.Close() - recorder := mqsend.OpenMockMessageQueue(mqsend.MessageQueueConfig{ - MaxQueueSize: tracing.MaxQueueSize, - MaxMessageSize: tracing.MaxSpanSize, - }) - err := tracing.InitGlobalTracer(tracing.Config{ - SampleRate: 1, - TestOnlyMockMessageQueue: recorder, - }) - if err != nil { - t.Fatal(err) - } - client, err := NewClient(ClientConfig{ Slug: "test", }) @@ -85,21 +69,6 @@ func TestNewClient(t *testing.T) { if !errors.As(err, &e) { t.Errorf("expected error wrap error of type %T", *e) } - - // MonitorClient is applied - b, err := recorder.Receive(context.Background()) - if err != nil { - t.Fatal(err) - } - var span tracing.ZipkinSpan - err = json.Unmarshal(b, &span) - if err != nil { - t.Fatal(err) - } - expected := "test.request" - if span.Name != expected { - t.Errorf("expected %s, actual: %q", expected, span.Name) - } }) } @@ -149,47 +118,6 @@ func TestNewClientConcurrency(t *testing.T) { wg.Wait() } -func TestMonitorClient(t *testing.T) { - recorder := mqsend.OpenMockMessageQueue(mqsend.MessageQueueConfig{ - MaxQueueSize: tracing.MaxQueueSize, - MaxMessageSize: tracing.MaxSpanSize, - }) - err := tracing.InitGlobalTracer(tracing.Config{ - SampleRate: 1, - TestOnlyMockMessageQueue: recorder, - }) - if err != nil { - t.Fatal(err) - } - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - })) - defer server.Close() - - middleware := MonitorClient("test") - client := &http.Client{ - Transport: middleware(http.DefaultTransport), - } - _, err = client.Get(server.URL) - if err != nil { - t.Fatal(err) - } - - b, err := recorder.Receive(context.Background()) - if err != nil { - t.Fatal(err) - } - var span tracing.ZipkinSpan - err = json.Unmarshal(b, &span) - if err != nil { - t.Fatal(err) - } - - expected := "test.request" - if span.Name != expected { - t.Errorf("expected %s, actual: %q", expected, span.Name) - } -} - func TestClientErrorWrapper(t *testing.T) { t.Run("HTTP 200", func(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/httpbp/fixtures_test.go b/httpbp/fixtures_test.go index 5d7b18324..6803352cc 100644 --- a/httpbp/fixtures_test.go +++ b/httpbp/fixtures_test.go @@ -6,7 +6,6 @@ import ( "net/http" "strings" "testing" - "time" "github.com/reddit/baseplate.go/ecinterface" "github.com/reddit/baseplate.go/httpbp" @@ -17,10 +16,6 @@ type jsonResponseBody struct { X int `json:"x"` } -const ( - testTimeout = time.Millisecond * 100 -) - var testSecrets = map[string]secrets.GenericSecret{ "secret/http/edge-context-signature": { Type: secrets.VersionedType, diff --git a/httpbp/middlewares.go b/httpbp/middlewares.go index 49c116006..2d4b7ea88 100644 --- a/httpbp/middlewares.go +++ b/httpbp/middlewares.go @@ -70,15 +70,13 @@ type DefaultMiddlewareArgs struct { // DefaultMiddleware returns a slice of all the default Middleware for a // Baseplate HTTP server. The default middleware are (in order): // -// 1. InjectServerSpan -// 2. InjectEdgeRequestContext -// 3. PrometheusServerMetrics +// 1. InjectEdgeRequestContext +// 2. PrometheusServerMetrics func DefaultMiddleware(args DefaultMiddlewareArgs) []Middleware { if args.TrustHandler == nil { args.TrustHandler = NeverTrustHeaders{} } return []Middleware{ - InjectServerSpan(args.TrustHandler), InjectEdgeRequestContext(InjectEdgeRequestContextArgs(args)), PrometheusServerMetrics(""), } diff --git a/httpbp/middlewares_test.go b/httpbp/middlewares_test.go index 03b00406d..764712cd0 100644 --- a/httpbp/middlewares_test.go +++ b/httpbp/middlewares_test.go @@ -3,7 +3,6 @@ package httpbp_test import ( "bufio" "context" - "encoding/json" "errors" "net" "net/http" @@ -16,8 +15,6 @@ import ( "github.com/reddit/baseplate.go/ecinterface" "github.com/reddit/baseplate.go/httpbp" "github.com/reddit/baseplate.go/log" - "github.com/reddit/baseplate.go/mqsend" - "github.com/reddit/baseplate.go/tracing" ) func TestWrap(t *testing.T) { @@ -40,125 +37,6 @@ func TestWrap(t *testing.T) { } } -func TestInjectServerSpan(t *testing.T) { - t.Parallel() - - defer func() { - tracing.CloseTracer() - tracing.InitGlobalTracer(tracing.Config{}) - }() - mmq := mqsend.OpenMockMessageQueue(mqsend.MessageQueueConfig{ - MaxQueueSize: 100, - MaxMessageSize: 1024, - }) - logger, startFailing := tracing.TestWrapper(t) - tracing.InitGlobalTracer(tracing.Config{ - SampleRate: 0, - MaxRecordTimeout: testTimeout, - Logger: logger, - TestOnlyMockMessageQueue: mmq, - }) - startFailing() - - req := newRequest(t, "") - - cases := []struct { - name string - truster httpbp.HeaderTrustHandler - err error - hasAnnotations bool - }{ - { - name: "trust/no-err", - truster: httpbp.AlwaysTrustHeaders{}, - hasAnnotations: true, - }, - { - name: "trust/err", - truster: httpbp.AlwaysTrustHeaders{}, - hasAnnotations: true, - err: errors.New("test"), - }, - { - name: "trust/http-err/4xx", - truster: httpbp.AlwaysTrustHeaders{}, - hasAnnotations: true, - err: httpbp.JSONError(httpbp.BadRequest(), nil), - }, - { - name: "trust/http-err/5xx", - truster: httpbp.AlwaysTrustHeaders{}, - hasAnnotations: true, - err: httpbp.JSONError(httpbp.InternalServerError(), nil), - }, - { - name: "no-trust/no-err", - truster: httpbp.NeverTrustHeaders{}, - hasAnnotations: false, - }, - { - name: "no-trust/err", - truster: httpbp.NeverTrustHeaders{}, - hasAnnotations: false, - err: errors.New("test"), - }, - } - for _, _c := range cases { - c := _c - t.Run( - c.name, - func(t *testing.T) { - handle := httpbp.Wrap( - "test", - newTestHandler(testHandlerPlan{err: c.err}), - httpbp.InjectServerSpan(c.truster), - ) - handle(req.Context(), httptest.NewRecorder(), req) - - ctx, cancel := context.WithTimeout(context.Background(), testTimeout) - defer cancel() - msg, err := mmq.Receive(ctx) - if !c.hasAnnotations && err == nil { - t.Fatal("expected error, got nil") - } else if c.hasAnnotations && err != nil { - t.Fatal(err) - } - if !c.hasAnnotations { - return - } - - var trace tracing.ZipkinSpan - err = json.Unmarshal(msg, &trace) - if err != nil { - t.Fatal(err) - } - if len(trace.BinaryAnnotations) == 0 { - t.Fatal("no binary annotations") - } - t.Logf("%#v", trace.BinaryAnnotations) - hasError := false - for _, annotation := range trace.BinaryAnnotations { - if annotation.Key == "error" { - hasError = true - } - } - expectedErr := c.err - var httpErr httpbp.HTTPError - if errors.As(c.err, &httpErr) { - if httpErr.Response().Code < 500 { - expectedErr = nil - } - } - if expectedErr != nil && !hasError { - t.Error("error binary annotation was not present.") - } else if expectedErr == nil && hasError { - t.Error("unexpected error binary annotation") - } - }, - ) - } -} - func TestInjectEdgeRequestContext(t *testing.T) { t.Parallel() diff --git a/httpbp/server_test.go b/httpbp/server_test.go index bbed05ffd..a5a278465 100644 --- a/httpbp/server_test.go +++ b/httpbp/server_test.go @@ -14,8 +14,6 @@ import ( "github.com/reddit/baseplate.go/ecinterface" "github.com/reddit/baseplate.go/httpbp" "github.com/reddit/baseplate.go/log" - "github.com/reddit/baseplate.go/mqsend" - "github.com/reddit/baseplate.go/tracing" ) func TestEndpoint(t *testing.T) { @@ -289,23 +287,6 @@ func TestServerArgsSetupEndpoints(t *testing.T) { t.Fatalf("no handler at %q: %#v", pattern, registry.registry) } - defer func() { - tracing.CloseTracer() - tracing.InitGlobalTracer(tracing.Config{}) - }() - mmq := mqsend.OpenMockMessageQueue(mqsend.MessageQueueConfig{ - MaxQueueSize: 100, - MaxMessageSize: 1024, - }) - logger, startFailing := tracing.TestWrapper(t) - tracing.InitGlobalTracer(tracing.Config{ - SampleRate: 1, - MaxRecordTimeout: testTimeout, - Logger: logger, - TestOnlyMockMessageQueue: mmq, - }) - startFailing() - req := newRequest(t, "foo") req.Method = http.MethodGet handle.ServeHTTP(httptest.NewRecorder(), req) @@ -314,23 +295,6 @@ func TestServerArgsSetupEndpoints(t *testing.T) { if recorder.header == "" { t.Error("edge request context not set") } - - // Test that the Span middleware was set up - ctx, cancel := context.WithTimeout(context.Background(), testTimeout) - defer cancel() - msg, err := mmq.Receive(ctx) - if err != nil { - t.Fatal(err) - } - - var trace tracing.ZipkinSpan - err = json.Unmarshal(msg, &trace) - if err != nil { - t.Fatal(err) - } - if len(trace.BinaryAnnotations) == 0 { - t.Fatal("no binary annotations") - } }, ) } diff --git a/thriftbp/client_middlewares.go b/thriftbp/client_middlewares.go index 182f97621..987051889 100644 --- a/thriftbp/client_middlewares.go +++ b/thriftbp/client_middlewares.go @@ -8,7 +8,6 @@ import ( "github.com/apache/thrift/lib/go/thrift" "github.com/avast/retry-go" - "github.com/opentracing/opentracing-go" "github.com/prometheus/client_golang/prometheus" "github.com/reddit/baseplate.go/breakerbp" @@ -20,7 +19,6 @@ import ( "github.com/reddit/baseplate.go/internalv2compat" "github.com/reddit/baseplate.go/prometheusbp" "github.com/reddit/baseplate.go/retrybp" - "github.com/reddit/baseplate.go/tracing" "github.com/reddit/baseplate.go/transport" ) @@ -191,39 +189,15 @@ type MonitorClientArgs struct { // MonitorClient is a ClientMiddleware that wraps the inner thrift.TClient.Call // in a thrift client span. // -// If you are using a thrift ClientPool created by NewBaseplateClientPool, -// this will be included automatically and should not be passed in as a -// ClientMiddleware to NewBaseplateClientPool. +// This middleware always use the injected v2 tracing thrift client middleware. +// If there's no v2 tracing thrift client middleware injected, it's no-op. func MonitorClient(args MonitorClientArgs) thrift.ClientMiddleware { if mw := internalv2compat.V2TracingThriftClientMiddleware(); mw != nil { return mw } - prefix := args.ServiceSlug + "." - s := args.ErrorSpanSuppressor - if s == nil { - s = IDLExceptionSuppressor - } return func(next thrift.TClient) thrift.TClient { - return thrift.WrappedTClient{ - Wrapped: func(ctx context.Context, method string, args, result thrift.TStruct) (_ thrift.ResponseMeta, err error) { - span, ctx := opentracing.StartSpanFromContext( - ctx, - prefix+method, - tracing.SpanTypeOption{ - Type: tracing.SpanTypeClient, - }, - ) - ctx = CreateThriftContextFromSpan(ctx, tracing.AsSpan(span)) - defer func() { - span.FinishWithOptions(tracing.FinishOptions{ - Ctx: ctx, - Err: s.Wrap(getClientError(result, err)), - }.Convert()) - }() - - return next.Call(ctx, method, args, result) - }, - } + // no-op + return next } } diff --git a/thriftbp/client_middlewares_test.go b/thriftbp/client_middlewares_test.go index af623bfd6..7d3f24c0e 100644 --- a/thriftbp/client_middlewares_test.go +++ b/thriftbp/client_middlewares_test.go @@ -2,26 +2,22 @@ package thriftbp_test import ( "context" - "encoding/json" "errors" "testing" "time" "github.com/apache/thrift/lib/go/thrift" "github.com/avast/retry-go" - "github.com/opentracing/opentracing-go" "github.com/prometheus/client_golang/prometheus" "github.com/reddit/baseplate.go" "github.com/reddit/baseplate.go/ecinterface" baseplatethrift "github.com/reddit/baseplate.go/internal/gen-go/reddit/baseplate" "github.com/reddit/baseplate.go/internal/prometheusbpint/spectest" - "github.com/reddit/baseplate.go/mqsend" "github.com/reddit/baseplate.go/prometheusbp" "github.com/reddit/baseplate.go/retrybp" "github.com/reddit/baseplate.go/thriftbp" "github.com/reddit/baseplate.go/thriftbp/thrifttest" - "github.com/reddit/baseplate.go/tracing" "github.com/reddit/baseplate.go/transport" ) @@ -48,152 +44,6 @@ func initClients(ecImpl ecinterface.Interface) (*thrifttest.MockClient, *thriftt return mock, recorder, client } -func initServerSpan(ctx context.Context, t *testing.T) (context.Context, *mqsend.MockMessageQueue) { - t.Helper() - - recorder := mqsend.OpenMockMessageQueue(mqsend.MessageQueueConfig{ - MaxQueueSize: 100, - MaxMessageSize: 1024, - }) - tracing.InitGlobalTracer(tracing.Config{ - SampleRate: 1.0, - TestOnlyMockMessageQueue: recorder, - }) - - span, ctx := opentracing.StartSpanFromContext( - ctx, - "test-service", - tracing.SpanTypeOption{Type: tracing.SpanTypeServer}, - ) - tracing.AsSpan(span).SetDebug(true) - return ctx, recorder -} - -func initLocalSpan(ctx context.Context, t *testing.T) (context.Context, *mqsend.MockMessageQueue) { - t.Helper() - - ctx, recorder := initServerSpan(ctx, t) - span := opentracing.SpanFromContext(ctx) - if span == nil { - t.Fatal("server span was nill") - } - _, ctx = opentracing.StartSpanFromContext( - ctx, - "local-test", - tracing.SpanTypeOption{ - Type: tracing.SpanTypeLocal, - }, - ) - return ctx, recorder -} - -func drainRecorder(recorder *mqsend.MockMessageQueue) ([]byte, error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond) - defer cancel() - return recorder.Receive(ctx) -} - -func TestWrapMonitoredClient(t *testing.T) { - cases := []struct { - name string - call thrifttest.MockCall - errorExpected bool - initSpan func(context.Context, *testing.T) (context.Context, *mqsend.MockMessageQueue) - }{ - { - name: "server span: success", - call: func(ctx context.Context, args, result thrift.TStruct) (meta thrift.ResponseMeta, err error) { - return - }, - errorExpected: false, - initSpan: initServerSpan, - }, - { - name: "server span: error", - call: func(ctx context.Context, args, result thrift.TStruct) (thrift.ResponseMeta, error) { - return thrift.ResponseMeta{}, errors.New("test error") - }, - errorExpected: true, - initSpan: initServerSpan, - }, - { - name: "local span: success", - call: func(ctx context.Context, args, result thrift.TStruct) (thrift.ResponseMeta, error) { - return thrift.ResponseMeta{}, nil - }, - errorExpected: false, - initSpan: initLocalSpan, - }, - { - name: "local span: error", - call: func(ctx context.Context, args, result thrift.TStruct) (thrift.ResponseMeta, error) { - return thrift.ResponseMeta{}, errors.New("test error") - }, - errorExpected: true, - initSpan: initLocalSpan, - }, - } - for _, c := range cases { - t.Run( - c.name, - func(t *testing.T) { - defer func() { - tracing.CloseTracer() - tracing.InitGlobalTracer(tracing.Config{}) - }() - - mock, recorder, client := initClients(nil) - mock.AddMockCall(method, c.call) - - ctx, mmq := c.initSpan(context.Background(), t) - if _, err := client.Call(ctx, method, nil, nil); !c.errorExpected && err != nil { - t.Fatal(err) - } else if c.errorExpected && err == nil { - t.Fatal("expected an error, got nil") - } - call := recorder.Calls()[0] - s := opentracing.SpanFromContext(call.Ctx) - if s == nil { - t.Fatal("span was nil") - } - spanName := service + "." + method - span := tracing.AsSpan(s) - if span.Name() != spanName { - t.Errorf("span name mismatch, expected %q, got %q", spanName, span.Name()) - } - if span.SpanType() != tracing.SpanTypeClient { - t.Errorf("span type mismatch, expected %s, got %s", tracing.SpanTypeClient, span.SpanType()) - } - if call.Method != method { - t.Errorf("method mismatch, expected %q, got %q", method, call.Method) - } - - msg, err := drainRecorder(mmq) - if err != nil { - t.Fatal(err) - } - - var trace tracing.ZipkinSpan - err = json.Unmarshal(msg, &trace) - if err != nil { - t.Fatal(err) - } - hasError := false - for _, annotation := range trace.BinaryAnnotations { - if annotation.Key == "error" { - hasError = true - } - } - if !c.errorExpected && hasError { - t.Error("error binary annotation present") - } else if c.errorExpected && !hasError { - t.Error("error binary annotation not present") - } - }, - ) - } -} - func TestForwardEdgeRequestContext(t *testing.T) { const expectedHeader = "dummy-edge-context" diff --git a/thriftbp/server_middlewares.go b/thriftbp/server_middlewares.go index c34c62f64..d92c0bcb5 100644 --- a/thriftbp/server_middlewares.go +++ b/thriftbp/server_middlewares.go @@ -129,43 +129,15 @@ func wrapErrorForServerSpan(err error, suppressor errorsbp.Suppressor) error { // InjectServerSpan implements thrift.ProcessorMiddleware and injects a server // span into the `next` context. // -// Starts the server span before calling the `next` TProcessorFunction and stops -// the span after it finishes. -// If the function returns an error that's not suppressed by the suppressor, -// that will be passed to span.Stop. -// -// Please note that if suppressor passed in is nil, -// it will be changed to IDLExceptionSuppressor instead. -// Please use errorsbp.SuppressNone explicitly instead if that's what's wanted. -// -// If "User-Agent" (HeaderUserAgent) THeader is set, -// the created server span will also have -// "peer.service" (tracing.TagKeyPeerService) tag set to its value. -// -// Note, the span will be created according to tracing related headers already -// being set on the context object. -// These should be automatically injected by your thrift.TSimpleServer. -func InjectServerSpan(suppressor errorsbp.Suppressor) thrift.ProcessorMiddleware { +// This middleware always use the injected v2 tracing thrift server middleware. +// If there's no v2 tracing thrift server middleware injected, it's no-op. +func InjectServerSpan(_ errorsbp.Suppressor) thrift.ProcessorMiddleware { if mw := internalv2compat.V2TracingThriftServerMiddleware(); mw != nil { return mw } return func(name string, next thrift.TProcessorFunction) thrift.TProcessorFunction { - return thrift.WrappedTProcessorFunction{ - Wrapped: func(ctx context.Context, seqID int32, in, out thrift.TProtocol) (success bool, err thrift.TException) { - ctx, span := StartSpanFromThriftContext(ctx, name) - if userAgent, ok := header(ctx, transport.HeaderUserAgent); ok { - span.SetTag(tracing.TagKeyPeerService, userAgent) - } - defer func() { - span.FinishWithOptions(tracing.FinishOptions{ - Ctx: ctx, - Err: wrapErrorForServerSpan(err, suppressor), - }.Convert()) - }() - - return next.Process(ctx, seqID, in, out) - }, - } + // no-op + return next } } diff --git a/thriftbp/server_middlewares_test.go b/thriftbp/server_middlewares_test.go index 22d26e312..6e89a9924 100644 --- a/thriftbp/server_middlewares_test.go +++ b/thriftbp/server_middlewares_test.go @@ -2,7 +2,6 @@ package thriftbp_test import ( "context" - "encoding/json" "errors" "testing" "time" @@ -10,17 +9,12 @@ import ( "github.com/apache/thrift/lib/go/thrift" "github.com/reddit/baseplate.go/ecinterface" - "github.com/reddit/baseplate.go/mqsend" "github.com/reddit/baseplate.go/thriftbp" "github.com/reddit/baseplate.go/thriftbp/thrifttest" "github.com/reddit/baseplate.go/tracing" "github.com/reddit/baseplate.go/transport" ) -const ( - testTimeout = time.Millisecond * 100 -) - type edgecontextRecorder struct { header string } @@ -36,84 +30,6 @@ func edgecontextRecorderMiddleware(impl ecinterface.Interface, recorder *edgecon } } -func TestInjectServerSpan(t *testing.T) { - const ua = "foo" - - defer func() { - tracing.CloseTracer() - tracing.InitGlobalTracer(tracing.Config{}) - }() - mmq := mqsend.OpenMockMessageQueue(mqsend.MessageQueueConfig{ - MaxQueueSize: 100, - MaxMessageSize: 1024, - }) - logger, startFailing := tracing.TestWrapper(t) - tracing.InitGlobalTracer(tracing.Config{ - SampleRate: 1, - MaxRecordTimeout: testTimeout, - Logger: logger, - TestOnlyMockMessageQueue: mmq, - }) - startFailing() - name := "test" - processor := thrifttest.NewMockTProcessor( - t, - map[string]thrift.TProcessorFunction{ - name: thrift.WrappedTProcessorFunction{ - Wrapped: func(ctx context.Context, seqID int32, in, out thrift.TProtocol) (bool, thrift.TException) { - return false, thrift.WrapTException(errors.New("TError")) - }, - }, - }, - ) - ctx := context.Background() - ctx = thrift.SetHeader(ctx, transport.HeaderTracingSampled, transport.HeaderTracingSampledTrue) - ctx = thrift.SetHeader(ctx, transport.HeaderUserAgent, ua) - ctx = thrifttest.SetMockTProcessorName(ctx, name) - - wrapped := thrift.WrapProcessor(processor, thriftbp.InjectServerSpan(nil)) - wrapped.Process(ctx, nil, nil) - ctx, cancel := context.WithTimeout(context.Background(), testTimeout) - defer cancel() - msg, err := mmq.Receive(ctx) - if err != nil { - t.Fatal(err) - } - t.Logf("Encoded span: %s", msg) - - var trace tracing.ZipkinSpan - err = json.Unmarshal(msg, &trace) - if err != nil { - t.Fatal(err) - } - var hasError, hasPeerService bool - for _, annotation := range trace.BinaryAnnotations { - if annotation.Key == "error" { - hasError = true - } - if annotation.Key == tracing.TagKeyPeerService { - hasPeerService = true - v, ok := annotation.Value.(string) - if !ok || v != ua { - t.Errorf( - "Expected binary annotation of %q to be %q, got %#v, %q, %v", - tracing.TagKeyPeerService, - ua, - annotation.Value, - v, - ok, - ) - } - } - } - if !hasError { - t.Error("Error binary annotation was not present.") - } - if !hasPeerService { - t.Errorf("%q binary annotation was not present.", tracing.TagKeyPeerService) - } -} - func TestStartSpanFromThriftContext(t *testing.T) { const ( name = "foo"