Use standard opentracing TextMap instead of custom backend code

Signed-off-by: Andres Taylor <antaylor@squareup.com>
This commit is contained in:
Andres Taylor 2019-09-16 05:42:08 +02:00
Родитель 9db6df7057
Коммит 978108243a
3 изменённых файлов: 65 добавлений и 6 удалений

Просмотреть файл

@ -16,10 +16,13 @@ limitations under the License.
package trace package trace
import ( import (
"strings"
otgrpc "github.com/opentracing-contrib/go-grpc" otgrpc "github.com/opentracing-contrib/go-grpc"
"github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/grpc" "google.golang.org/grpc"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/vt/vterrors"
) )
@ -43,7 +46,6 @@ var _ tracingService = (*openTracingService)(nil)
type tracer interface { type tracer interface {
GetOpenTracingTracer() opentracing.Tracer GetOpenTracingTracer() opentracing.Tracer
FromString(string) (opentracing.SpanContext, error)
} }
type openTracingService struct { type openTracingService struct {
@ -82,8 +84,28 @@ func (jf openTracingService) New(parent Span, label string) Span {
return openTracingSpan{otSpan: innerSpan} 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) { 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 { if err != nil {
return nil, vterrors.Wrap(err, "failed to deserialize span context") return nil, vterrors.Wrap(err, "failed to deserialize span context")
} }

Просмотреть файл

@ -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)
}

Просмотреть файл

@ -95,10 +95,6 @@ type jaegerTracer struct {
actual opentracing.Tracer actual opentracing.Tracer
} }
func (*jaegerTracer) FromString(s string) (opentracing.SpanContext, error) {
return jaeger.ContextFromString(s)
}
func (jt *jaegerTracer) GetOpenTracingTracer() opentracing.Tracer { func (jt *jaegerTracer) GetOpenTracingTracer() opentracing.Tracer {
return jt.actual return jt.actual
} }