Signed-off-by: Andres Taylor <antaylor@squareup.com>
This commit is contained in:
Andres Taylor 2019-03-20 10:19:13 -07:00
Родитель c9dcd5a65b
Коммит 2b52464112
5 изменённых файлов: 105 добавлений и 107 удалений

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

@ -17,10 +17,10 @@ limitations under the License.
package trace
import (
"io"
"io"
"github.com/opentracing/opentracing-go"
"golang.org/x/net/context"
"github.com/opentracing/opentracing-go"
"golang.org/x/net/context"
)
type fakeSpanFactory struct{}
@ -36,7 +36,7 @@ func (fakeSpan) Finish() {}
func (fakeSpan) Annotate(string, interface{}) {}
func init() {
tracingBackendFactories["noop"] = func(_ string) (opentracing.Tracer, io.Closer, error) {
return opentracing.NoopTracer{}, &nilCloser{}, nil
}
tracingBackendFactories["noop"] = func(_ string) (opentracing.Tracer, io.Closer, error) {
return opentracing.NoopTracer{}, &nilCloser{}, nil
}
}

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

@ -1,27 +1,27 @@
package trace
import (
"io"
"io"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go/config"
"golang.org/x/net/context"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go/config"
"golang.org/x/net/context"
)
type JaegerSpan struct {
otSpan opentracing.Span
otSpan opentracing.Span
}
func (js JaegerSpan) Finish() {
js.otSpan.Finish()
js.otSpan.Finish()
}
func (js JaegerSpan) Annotate(key string, value interface{}) {
js.otSpan.SetTag(key, value)
js.otSpan.SetTag(key, value)
}
type OpenTracingFactory struct {
Tracer opentracing.Tracer
Tracer opentracing.Tracer
}
// NewJagerTracerFromEnv will instantiate a SpanFactory implemented by Jaeger,
@ -43,49 +43,48 @@ type OpenTracingFactory struct {
// JAEGER_AGENT_HOST
// JAEGER_AGENT_PORT
func NewJagerTracerFromEnv(serviceName string) (opentracing.Tracer, io.Closer, error) {
cfg, err := config.FromEnv()
if cfg.ServiceName == "" {
cfg.ServiceName = serviceName
}
cfg, err := config.FromEnv()
if cfg.ServiceName == "" {
cfg.ServiceName = serviceName
}
tracer, closer, err := cfg.NewTracer()
tracer, closer, err := cfg.NewTracer()
if err != nil {
return nil, &nilCloser{}, err
}
if err != nil {
return nil, &nilCloser{}, err
}
return tracer, closer, nil
return tracer, closer, nil
}
func (jf OpenTracingFactory) New(parent Span, label string, spanType SpanType) Span {
var innerSpan opentracing.Span
if parent == nil {
innerSpan = jf.Tracer.StartSpan(label)
} else {
jaegerParent := parent.(JaegerSpan)
span := jaegerParent.otSpan
innerSpan = jf.Tracer.StartSpan(label, opentracing.ChildOf(span.Context()))
}
return JaegerSpan{otSpan: innerSpan}
var innerSpan opentracing.Span
if parent == nil {
innerSpan = jf.Tracer.StartSpan(label)
} else {
jaegerParent := parent.(JaegerSpan)
span := jaegerParent.otSpan
innerSpan = jf.Tracer.StartSpan(label, opentracing.ChildOf(span.Context()))
}
return JaegerSpan{otSpan: innerSpan}
}
func (jf OpenTracingFactory) FromContext(ctx context.Context) (Span, bool) {
innerSpan := opentracing.SpanFromContext(ctx)
innerSpan := opentracing.SpanFromContext(ctx)
if innerSpan != nil {
return JaegerSpan{otSpan: innerSpan}, true
} else {
return nil, false
}
if innerSpan != nil {
return JaegerSpan{otSpan: innerSpan}, true
} else {
return nil, false
}
}
func (jf OpenTracingFactory) NewContext(parent context.Context, s Span) context.Context {
span, ok := s.(JaegerSpan)
if !ok {
return nil
}
return opentracing.ContextWithSpan(parent, span.otSpan)
span, ok := s.(JaegerSpan)
if !ok {
return nil
}
return opentracing.ContextWithSpan(parent, span.otSpan)
}
type nilCloser struct {
@ -94,5 +93,5 @@ type nilCloser struct {
func (c *nilCloser) Close() error { return nil }
func init() {
tracingBackendFactories["jaeger"] = NewJagerTracerFromEnv
tracingBackendFactories["jaeger"] = NewJagerTracerFromEnv
}

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

@ -20,15 +20,15 @@ limitations under the License.
package trace
import (
"flag"
"io"
"strings"
"flag"
"io"
"strings"
"github.com/opentracing/opentracing-go"
"golang.org/x/net/context"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
"github.com/opentracing/opentracing-go"
"golang.org/x/net/context"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)
// Span represents a unit of work within a trace. After creating a Span with
@ -45,26 +45,26 @@ type Span interface {
type SpanType int
const (
Local SpanType = iota
Client
Server
Local SpanType = iota
Client
Server
)
// NewSpan creates a new Span with the currently installed tracing plugin.
// If no tracing plugin is installed, it returns a fake Span that does nothing.
func NewSpan(inCtx context.Context, label string, spanType SpanType) (Span, context.Context) {
parent, _ := spanFactory.FromContext(inCtx)
span := spanFactory.New(parent, label, spanType)
outCtx := spanFactory.NewContext(inCtx, span)
parent, _ := spanFactory.FromContext(inCtx)
span := spanFactory.New(parent, label, spanType)
outCtx := spanFactory.NewContext(inCtx, span)
return span, outCtx
return span, outCtx
}
// NewClientSpan returns a span and a context to register calls to dependent services
func NewClientSpan(inCtx context.Context, serviceName, spanLabel string) (Span, context.Context) {
span, ctx := NewSpan(inCtx, spanLabel, Client)
span.Annotate("peer.service", serviceName)
return span, ctx
span, ctx := NewSpan(inCtx, spanLabel, Client)
span.Annotate("peer.service", serviceName)
return span, ctx
}
// FromContext returns the Span from a Context if present. The bool return
@ -89,9 +89,9 @@ func CopySpan(parentCtx, spanCtx context.Context) context.Context {
// SpanFactory is an interface for creating spans or extracting them from Contexts.
type SpanFactory interface {
New(parent Span, label string, spanType SpanType) Span
FromContext(ctx context.Context) (Span, bool)
NewContext(parent context.Context, span Span) context.Context
New(parent Span, label string, spanType SpanType) Span
FromContext(ctx context.Context) (Span, bool)
NewContext(parent context.Context, span Span) context.Context
}
type TracerFactory func(serviceName string) (opentracing.Tracer, io.Closer, error)
@ -110,40 +110,40 @@ func RegisterSpanFactory(sf SpanFactory) {
var spanFactory SpanFactory = fakeSpanFactory{}
var (
tracingServer = flag.String("tracer", "noop", "tracing service to use.")
tracingServer = flag.String("tracer", "noop", "tracing service to use.")
)
// StartTracing enables tracing for a named service
func StartTracing(serviceName string) io.Closer {
factory, ok := tracingBackendFactories[*tracingServer]
if !ok {
options := make([]string, len(tracingBackendFactories))
for k := range tracingBackendFactories {
options = append(options, k)
}
factory, ok := tracingBackendFactories[*tracingServer]
if !ok {
options := make([]string, len(tracingBackendFactories))
for k := range tracingBackendFactories {
options = append(options, k)
}
altStr := strings.Join(options, ", ")
log.Error(vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "no such tracing service found. alternatives are: %v", altStr))
return &nilCloser{}
}
altStr := strings.Join(options, ", ")
log.Error(vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "no such tracing service found. alternatives are: %v", altStr))
return &nilCloser{}
}
tracer, closer, err := factory(serviceName)
if err != nil {
log.Error(vterrors.Wrapf(err, "failed to create a %s tracer", *tracingServer))
return &nilCloser{}
}
tracer, closer, err := factory(serviceName)
if err != nil {
log.Error(vterrors.Wrapf(err, "failed to create a %s tracer", *tracingServer))
return &nilCloser{}
}
_, isNoopTracer := tracer.(opentracing.NoopTracer)
if isNoopTracer {
// We don't have a real tracer to work with. Let's leave early
return &nilCloser{}
}
_, isNoopTracer := tracer.(opentracing.NoopTracer)
if isNoopTracer {
// We don't have a real tracer to work with. Let's leave early
return &nilCloser{}
}
// Register it for all openTracing enabled plugins, mainly the grpc connections
opentracing.SetGlobalTracer(tracer)
// Register it for all openTracing enabled plugins, mainly the grpc connections
opentracing.SetGlobalTracer(tracer)
// Register it for the internal Vitess tracing system
RegisterSpanFactory(OpenTracingFactory{Tracer: tracer})
// Register it for the internal Vitess tracing system
RegisterSpanFactory(OpenTracingFactory{Tracer: tracer})
return closer
return closer
}

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

@ -81,4 +81,3 @@ func (fakeTracer) Inject(sm opentracing.SpanContext, format interface{}, carrier
func (fakeTracer) Extract(format interface{}, carrier interface{}) (opentracing.SpanContext, error) {
panic("implement me")
}

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

@ -17,9 +17,9 @@ limitations under the License.
package trace
import (
"io"
"io"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/log"
)
const extractSize = 10
@ -27,18 +27,18 @@ const extractSize = 10
// ExtractFirstCharacters returns the first few characters of a string.
// If the string had to be truncated, "..." is added to the end
func ExtractFirstCharacters(in string) string {
if len(in) < extractSize {
return in
}
runes := []rune(in)
return string(runes[0:extractSize]) + "..."
if len(in) < extractSize {
return in
}
runes := []rune(in)
return string(runes[0:extractSize]) + "..."
}
func LogErrorsWhenClosing(in io.Closer) func() {
return func() {
err := in.Close()
if err != nil {
log.Error(err)
}
}
}
return func() {
err := in.Close()
if err != nil {
log.Error(err)
}
}
}