From 978108243af4c3404df59683af65323c27a3450b Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Mon, 16 Sep 2019 05:42:08 +0200 Subject: [PATCH] Use standard opentracing TextMap instead of custom backend code Signed-off-by: Andres Taylor --- go/trace/opentracing.go | 26 +++++++++++++++++++++-- go/trace/opentracing_test.go | 41 ++++++++++++++++++++++++++++++++++++ go/trace/plugin_jaeger.go | 4 ---- 3 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 go/trace/opentracing_test.go diff --git a/go/trace/opentracing.go b/go/trace/opentracing.go index 4413d808a1..6a33f12069 100644 --- a/go/trace/opentracing.go +++ b/go/trace/opentracing.go @@ -16,10 +16,13 @@ limitations under the License. package trace import ( + "strings" + otgrpc "github.com/opentracing-contrib/go-grpc" "github.com/opentracing/opentracing-go" "golang.org/x/net/context" "google.golang.org/grpc" + "vitess.io/vitess/go/vt/proto/vtrpc" "vitess.io/vitess/go/vt/vterrors" ) @@ -43,7 +46,6 @@ var _ tracingService = (*openTracingService)(nil) type tracer interface { GetOpenTracingTracer() opentracing.Tracer - FromString(string) (opentracing.SpanContext, error) } type openTracingService struct { @@ -82,8 +84,28 @@ func (jf openTracingService) New(parent Span, label string) Span { return openTracingSpan{otSpan: innerSpan} } +func extractMapFromString(in string) (opentracing.TextMapCarrier, error) { + m := make(opentracing.TextMapCarrier) + items := strings.Split(in, ":") + if len(items) < 2 { + return nil, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "expected transmitted context to contain at least span id and trace id") + } + for _, v := range items { + idx := strings.Index(v, "=") + if idx < 1 { + return nil, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "every element in the context string has to be in the form key=value") + } + m[v[0:idx]] = v[idx+1:] + } + return m, nil +} + func (jf openTracingService) NewFromString(parent, label string) (Span, error) { - spanContext, err := jf.Tracer.FromString(parent) + carrier, err := extractMapFromString(parent) + if err != nil { + return nil, err + } + spanContext, err := jf.Tracer.GetOpenTracingTracer().Extract(opentracing.TextMap, carrier) if err != nil { return nil, vterrors.Wrap(err, "failed to deserialize span context") } diff --git a/go/trace/opentracing_test.go b/go/trace/opentracing_test.go new file mode 100644 index 0000000000..9f6f9203ef --- /dev/null +++ b/go/trace/opentracing_test.go @@ -0,0 +1,41 @@ +/* +Copyright 2019 Google Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package trace + +import ( + "testing" + + "github.com/opentracing/opentracing-go" + "github.com/stretchr/testify/assert" +) + +func TestExtractMapFromString(t *testing.T) { + expected := make(opentracing.TextMapCarrier) + expected["apa"] = "12" + expected["banan"] = "x-tracing-backend-12" + result, err := extractMapFromString("apa=12:banan=x-tracing-backend-12") + assert.NoError(t, err) + assert.Equal(t, expected, result) +} + +func TestErrorConditions(t *testing.T) { + _, err := extractMapFromString("") + assert.Error(t, err) + + _, err = extractMapFromString("key=value:keywithnovalue") + assert.Error(t, err) +} diff --git a/go/trace/plugin_jaeger.go b/go/trace/plugin_jaeger.go index b8053a4e2c..efbfff8f02 100644 --- a/go/trace/plugin_jaeger.go +++ b/go/trace/plugin_jaeger.go @@ -95,10 +95,6 @@ type jaegerTracer struct { actual opentracing.Tracer } -func (*jaegerTracer) FromString(s string) (opentracing.SpanContext, error) { - return jaeger.ContextFromString(s) -} - func (jt *jaegerTracer) GetOpenTracingTracer() opentracing.Tracer { return jt.actual }