diff --git a/baseplate_test.go b/baseplate_test.go index db16cc67d..654614103 100644 --- a/baseplate_test.go +++ b/baseplate_test.go @@ -238,7 +238,10 @@ func TestServeClosers(t *testing.T) { defer store.Close() bp := baseplate.NewTestBaseplate(baseplate.NewTestBaseplateArgs{ - Config: baseplate.Config{StopTimeout: testTimeout}, + Config: baseplate.Config{ + StopTimeout: testTimeout, + StopDelay: -1, + }, Store: store, EdgeContextImpl: ecinterface.Mock(), }) @@ -266,7 +269,9 @@ func TestServeClosers(t *testing.T) { time.Sleep(time.Millisecond) p.Signal(os.Interrupt) - <-ch + if err := <-ch; err != nil { + t.Fatalf("Serve returned an unexpected error: %v", err) + } if got := pre.get(); len(got) != 1 { t.Fatalf("Unexpected number of PreShutdown calls: expected 1, got %v", len(got)) diff --git a/internalv2compat/server_span.go b/internalv2compat/server_span.go new file mode 100644 index 000000000..26b8bbc32 --- /dev/null +++ b/internalv2compat/server_span.go @@ -0,0 +1,45 @@ +package internalv2compat + +import ( + "context" + "sync" + "time" + + "github.com/opentracing/opentracing-go" +) + +// ServerSpan carries the real v2 span and the metadata needed by v0 span hooks. +// The returned context must contain the same span for both tracing APIs. +type ServerSpan struct { + Span opentracing.Span + TraceID, SpanID, ParentID string + Sampled bool + StartTime time.Time +} + +// ServerSpanProvider starts a v2 server span on behalf of a legacy helper. +type ServerSpanProvider func(context.Context, string) (context.Context, ServerSpan) + +var serverSpanProvider struct { + sync.RWMutex + provider ServerSpanProvider +} + +// SetV2TracingServerSpanProvider installs the v2 implementation of legacy server +// spans. Passing nil restores legacy behavior. The previous provider is returned +// so temporary tracing providers can restore it during cleanup. +func SetV2TracingServerSpanProvider(provider ServerSpanProvider) ServerSpanProvider { + serverSpanProvider.Lock() + defer serverSpanProvider.Unlock() + previous := serverSpanProvider.provider + serverSpanProvider.provider = provider + return previous +} + +// V2TracingServerSpanProvider returns the configured provider, or nil when v2 +// server-span interoperability has not been installed. +func V2TracingServerSpanProvider() ServerSpanProvider { + serverSpanProvider.RLock() + defer serverSpanProvider.RUnlock() + return serverSpanProvider.provider +} diff --git a/tracing/server_span_compat_test.go b/tracing/server_span_compat_test.go new file mode 100644 index 000000000..e9c6957b3 --- /dev/null +++ b/tracing/server_span_compat_test.go @@ -0,0 +1,125 @@ +package tracing + +import ( + "context" + "strconv" + "testing" + "time" + + "github.com/google/go-cmp/cmp" + "github.com/opentracing/opentracing-go" + "github.com/opentracing/opentracing-go/mocktracer" + + //lint:ignore SA1019 This library is internal only, not actually deprecated + "github.com/reddit/baseplate.go/internalv2compat" + "github.com/reddit/baseplate.go/log" +) + +func TestServerSpanCompatibility(t *testing.T) { + for _, useV2 := range []bool{false, true} { + t.Run(strconv.FormatBool(useV2), func(t *testing.T) { + previousTracer, previousGlobal := globalTracer, opentracing.GlobalTracer() + previousHooks := createServerSpanHooks + recorder := new(countingRecorder) + globalTracer = Tracer{sampleRate: 1, logger: log.NopWrapper, recorder: recorder} + opentracing.SetGlobalTracer(&globalTracer) + hook := new(compatibilityHook) + createServerSpanHooks = []CreateServerSpanHook{hook} + previousProvider := internalv2compat.SetV2TracingServerSpanProvider(nil) + t.Cleanup(func() { + internalv2compat.SetV2TracingServerSpanProvider(previousProvider) + globalTracer = previousTracer + opentracing.SetGlobalTracer(previousGlobal) + createServerSpanHooks = previousHooks + }) + foreign := mocktracer.New() + if useV2 { + internalv2compat.SetV2TracingServerSpanProvider(func(ctx context.Context, name string) (context.Context, internalv2compat.ServerSpan) { + s, ctx := opentracing.StartSpanFromContextWithTracer(ctx, foreign, name) + mock := s.(*mocktracer.MockSpan) + return ctx, internalv2compat.ServerSpan{ + Span: s, + TraceID: strconv.Itoa(mock.SpanContext.TraceID), + SpanID: strconv.Itoa(mock.SpanContext.SpanID), + Sampled: true, + StartTime: mock.StartTime, + } + }) + } + ctx, span := StartTopLevelServerSpan(context.Background(), "consumer.example") + span.SetTag("topic", "example") + span.AddCounter("messages", 2) + span.AddCounter("messages", 3) + finish := time.Now() + opts := FinishOptions{Ctx: ctx, Err: context.DeadlineExceeded}.Convert() + opts.FinishTime = finish + span.FinishWithOptions(opts) + if diff := cmp.Diff([]string{"create", "start", "tag", "counter", "counter", "stop"}, hook.calls); diff != "" { + t.Errorf("hooks (-want +got):\n%s", diff) + } + if hook.err != context.DeadlineExceeded || span.Name() != "consumer.example" || + span.SpanType() != SpanTypeServer || !span.StopTime().Equal(finish) { + + t.Error("legacy metadata or error hooks were lost") + } + if useV2 { + finished := foreign.FinishedSpans() + if len(finished) != 1 { + t.Fatalf("finished %d spans, want 1", len(finished)) + } + actual := finished[0] + if actual != opentracing.SpanFromContext(ctx) || + span.ID() != strconv.Itoa(actual.SpanContext.SpanID) || + span.TraceID() != strconv.Itoa(actual.SpanContext.TraceID) || + !span.StartTime().Equal(actual.StartTime) || !finish.Equal(actual.FinishTime) { + + t.Error("returned wrapper does not identify and finish the context span") + } + want := map[string]interface{}{ + "topic": "example", + "counter.messages": float64(5), + ZipkinBinaryAnnotationKeyError: true, + ZipkinBinaryAnnotationKeyTimeOut: true, + } + if diff := cmp.Diff(want, actual.Tags()); diff != "" { + t.Errorf("annotations (-want +got):\n%s", diff) + } + if recorder.calls != 0 { + t.Errorf("published %d duplicate legacy spans", recorder.calls) + } + } else if recorder.calls != 1 || opentracing.SpanFromContext(ctx) != span { + t.Error("legacy tracing changed without a v2 provider") + } + }) + } +} + +type countingRecorder struct{ calls int } + +func (r *countingRecorder) Close() error { return nil } +func (r *countingRecorder) Send(context.Context, []byte) error { r.calls++; return nil } + +type compatibilityHook struct { + calls []string + err error +} + +func (h *compatibilityHook) OnCreateServerSpan(s *Span) error { + h.calls = append(h.calls, "create") + s.AddHooks(h) + return nil +} +func (h *compatibilityHook) OnPostStart(*Span) error { h.calls = append(h.calls, "start"); return nil } +func (h *compatibilityHook) OnPreStop(_ *Span, err error) error { + h.calls = append(h.calls, "stop") + h.err = err + return nil +} +func (h *compatibilityHook) OnSetTag(*Span, string, interface{}) error { + h.calls = append(h.calls, "tag") + return nil +} +func (h *compatibilityHook) OnAddCounter(*Span, string, float64) error { + h.calls = append(h.calls, "counter") + return nil +} diff --git a/tracing/span.go b/tracing/span.go index e23f3dda3..82b6ef215 100644 --- a/tracing/span.go +++ b/tracing/span.go @@ -11,6 +11,8 @@ import ( "github.com/opentracing/opentracing-go" otlog "github.com/opentracing/opentracing-go/log" + //lint:ignore SA1019 This library is internal only, not actually deprecated + "github.com/reddit/baseplate.go/internalv2compat" "github.com/reddit/baseplate.go/log" ) @@ -92,8 +94,13 @@ func newSpan(tracer *Tracer, name string, spanType SpanType) *Span { } // Span defines a tracing span. +// When v2 interoperability is enabled, legacy server helpers return a Span that +// runs the legacy hooks and delegates tracing operations to the real v2 span. type Span struct { trace *trace + // A v2 span owns export when this Span is a compatibility wrapper. Legacy + // metadata and hooks are retained, but no second Zipkin span is published. + delegate opentracing.Span hooks []interface{} spanType SpanType @@ -190,14 +197,22 @@ func (s *Span) AddHooks(hooks ...interface{}) { } // SetDebug sets or unsets the debug flag of this Span. +// In v2 compatibility mode this records the debug annotation, but does not +// change the sampling decision already made by the v2 tracer. func (s *Span) SetDebug(v bool) { s.trace.setDebug(v) + if s.delegate != nil { + s.delegate.SetTag(ZipkinBinaryAnnotationKeyDebug, v) + } } // SetTag sets a binary tag annotation and calls all OnSetTag Hooks // registered to the Span. func (s *Span) SetTag(key string, value interface{}) opentracing.Span { s.trace.setTag(key, value) + if s.delegate != nil { + s.delegate.SetTag(key, value) + } for _, h := range s.hooks { if hook, ok := h.(SetSpanTagHook); ok { if err := hook.OnSetTag(s, key, value); err != nil { @@ -212,6 +227,9 @@ func (s *Span) SetTag(key string, value interface{}) opentracing.Span { // Hooks registered to the Span. func (s *Span) AddCounter(key string, delta float64) { s.trace.addCounter(key, delta) + if s.delegate != nil { + s.delegate.SetTag(counterKeyPrefix+key, s.trace.counters[key]) + } for _, h := range s.hooks { if hook, ok := h.(AddSpanCounterHook); ok { if err := hook.OnAddCounter(s, key, delta); err != nil { @@ -261,6 +279,8 @@ func (s Span) initChildSpan(child *Span) { // Stop stops the Span, calls all registered OnPreStop Hooks, // serializes the Span, // and sends the serialized Span to a back-end that records the Span. +// In v2 compatibility mode it finishes the delegated span instead of publishing +// another span through the legacy recorder. // // In most cases FinishWithOptions should be used instead, // which calls Stop and auto logs the error returned by Stop. @@ -277,6 +297,16 @@ func (s *Span) Stop(ctx context.Context, err error) error { if s.trace.stop.IsZero() { s.trace.stop = time.Now() } + if s.delegate != nil { + if err != nil { + s.delegate.SetTag(ZipkinBinaryAnnotationKeyError, true) + } + if s.spanType == SpanTypeServer && errors.Is(err, context.DeadlineExceeded) { + s.delegate.SetTag(ZipkinBinaryAnnotationKeyTimeOut, true) + } + s.delegate.FinishWithOptions(opentracing.FinishOptions{FinishTime: s.trace.stop}) + return nil + } return s.trace.publish(ctx) } @@ -307,21 +337,30 @@ func (s Span) getHub() *sentry.Hub { // ForeachBaggageItem implements opentracing.SpanContext. // -// We don't support any extra baggage items, so it's a noop. -func (s *Span) ForeachBaggageItem(handler func(k, v string) bool) {} +// Legacy spans do not support baggage; v2 wrappers delegate to their real span. +func (s *Span) ForeachBaggageItem(handler func(k, v string) bool) { + if s.delegate != nil { + s.delegate.Context().ForeachBaggageItem(handler) + } +} // SetBaggageItem implements opentracing.Span. // -// As we don't support any extra baggage items, -// it's a noop and just returns self. +// Legacy spans ignore baggage; v2 wrappers delegate to their real span. func (s *Span) SetBaggageItem(restrictedKey, value string) opentracing.Span { + if s.delegate != nil { + s.delegate.SetBaggageItem(restrictedKey, value) + } return s } // BaggageItem implements opentracing.Span. // -// As we don't support any extra baggage items, it always returns empty string. +// It returns an empty string for legacy spans and delegates for v2 wrappers. func (s *Span) BaggageItem(restrictedKey string) string { + if s.delegate != nil { + return s.delegate.BaggageItem(restrictedKey) + } return "" } @@ -373,52 +412,97 @@ func (s *Span) FinishWithOptions(opts opentracing.FinishOptions) { // Context implements opentracing.Span. // -// It returns self as opentracing.SpanContext. +// It returns self for legacy spans, or the underlying context for v2 wrappers. func (s *Span) Context() opentracing.SpanContext { + if s.delegate != nil { + return s.delegate.Context() + } return s } // SetOperationName implements opentracing.Span. func (s *Span) SetOperationName(operationName string) opentracing.Span { s.trace.name = operationName + if s.delegate != nil { + s.delegate.SetOperationName(operationName) + } return s } // Tracer implements opentracing.Span. func (s *Span) Tracer() opentracing.Tracer { + if s.delegate != nil { + return s.delegate.Tracer() + } return s.trace.tracer } // LogFields implements opentracing.Span. // -// In this implementation it's a no-op. -func (s *Span) LogFields(fields ...otlog.Field) {} +// It is a no-op for legacy spans and delegates for v2 wrappers. +func (s *Span) LogFields(fields ...otlog.Field) { + if s.delegate != nil { + s.delegate.LogFields(fields...) + } +} // LogKV implements opentracing.Span. // -// In this implementation it's a no-op. -func (s *Span) LogKV(alternatingKeyValues ...interface{}) {} +// It is a no-op for legacy spans and delegates for v2 wrappers. +func (s *Span) LogKV(alternatingKeyValues ...interface{}) { + if s.delegate != nil { + s.delegate.LogKV(alternatingKeyValues...) + } +} // LogEvent implements opentracing.Span. // -// it's deprecated in the interface and is a no-op here. -func (s *Span) LogEvent(event string) {} +// It is deprecated in the interface. Legacy spans ignore it; v2 wrappers delegate. +func (s *Span) LogEvent(event string) { + if s.delegate != nil { + s.delegate.LogFields(otlog.Event(event)) + } +} // LogEventWithPayload implements opentracing.Span. // -// it's deprecated in the interface and is a noop here. -func (s *Span) LogEventWithPayload(event string, payload interface{}) {} +// It is deprecated in the interface. Legacy spans ignore it; v2 wrappers delegate. +func (s *Span) LogEventWithPayload(event string, payload interface{}) { + if s.delegate != nil { + s.delegate.LogFields(otlog.Event(event), otlog.Object("payload", payload)) + } +} // Log implements opentracing.Span. // -// it's deprecated in the interface and is a no-op here. -func (s *Span) Log(data opentracing.LogData) {} +// It is deprecated in the interface. Legacy spans ignore it; v2 wrappers delegate. +func (s *Span) Log(data opentracing.LogData) { + if s.delegate != nil { + //lint:ignore SA1019 Preserve the timestamp of this legacy API when forwarding. + s.delegate.Log(data) + } +} // StartTopLevelServerSpan initializes a new, top level server span. // // This span will have a new TraceID and will be sampled based on your configured // sample rate. +// When v2 interoperability is configured, the v2 tracer owns sampling and export; +// finishing the returned Span still invokes the registered legacy server hooks. func StartTopLevelServerSpan(ctx context.Context, name string) (context.Context, *Span) { + if provider := internalv2compat.V2TracingServerSpanProvider(); provider != nil { + ctx, v2 := provider(ctx, name) + span := newSpan(nil, name, SpanTypeServer) + span.delegate = v2.Span + span.trace.traceID = v2.TraceID + span.trace.spanID = v2.SpanID + span.trace.parentID = v2.ParentID + span.trace.sampled = v2.Sampled + span.trace.start = v2.StartTime + onCreateServerSpan(span) + span.onStart() + return log.Attach(ctx, log.AttachArgs{TraceID: span.TraceID()}), span + } otSpan, ctx := opentracing.StartSpanFromContext( ctx, name,