Move tracing grpc code to the trace package

Signed-off-by: Andres Taylor <antaylor@squareup.com>
This commit is contained in:
Andres Taylor 2019-03-20 10:55:24 -07:00
Родитель f0934d0e71
Коммит ec483e45d3
4 изменённых файлов: 31 добавлений и 17 удалений

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

@ -24,8 +24,10 @@ import (
"io"
"strings"
"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/log"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
@ -80,6 +82,30 @@ func CopySpan(parentCtx, spanCtx context.Context) context.Context {
return parentCtx
}
func GetGrpcClientOptions() []grpc.DialOption {
tracer := opentracing.GlobalTracer()
_, isNoopTracer := tracer.(opentracing.NoopTracer)
if isNoopTracer {
return []grpc.DialOption{}
} else {
return []grpc.DialOption{
grpc.WithUnaryInterceptor(otgrpc.OpenTracingClientInterceptor(tracer)),
grpc.WithStreamInterceptor(otgrpc.OpenTracingStreamClientInterceptor(tracer)),
}
}
}
func AddGrpcServerOptions(addInterceptors func(s grpc.StreamServerInterceptor, u grpc.UnaryServerInterceptor)) {
tracer := opentracing.GlobalTracer()
_, isNoopTracer := tracer.(*opentracing.NoopTracer)
if isNoopTracer {
return
}
addInterceptors(otgrpc.OpenTracingStreamServerInterceptor(tracer), otgrpc.OpenTracingServerInterceptor(tracer))
}
// SpanFactory is an interface for creating spans or extracting them from Contexts.
type SpanFactory interface {
// New creates a new span from an existing one, if provided. The parent can also be nil

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

@ -21,11 +21,10 @@ package grpcclient
import (
"flag"
"github.com/opentracing-contrib/go-grpc"
"github.com/opentracing/opentracing-go"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
"vitess.io/vitess/go/trace"
"github.com/grpc-ecosystem/go-grpc-prometheus"
@ -100,13 +99,7 @@ func Dial(target string, failFast FailFast, opts ...grpc.DialOption) (*grpc.Clie
newopts = append(newopts, grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor))
}
// Enable trace information, if we have a real tracer
tracer := opentracing.GlobalTracer()
_, isNoopTracer := tracer.(opentracing.NoopTracer)
if !isNoopTracer {
newopts = append(newopts, grpc.WithUnaryInterceptor(otgrpc.OpenTracingClientInterceptor(tracer)))
newopts = append(newopts, grpc.WithStreamInterceptor(otgrpc.OpenTracingStreamClientInterceptor(tracer)))
}
newopts = append(newopts, trace.GetGrpcClientOptions()...)
return grpc.Dial(target, newopts...)
}

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

@ -308,7 +308,7 @@ func (schemaSwap *Swap) executeSwap() error {
}
errHealthWatchers := schemaSwap.runOnAllShards(
func(shard *shardSchemaSwap) error {
return shard.startHealthWatchers(schemaSwap.ctx )
return shard.startHealthWatchers(schemaSwap.ctx)
})
// Note: this defer statement is before the error is checked because some shards may
// succeed while others fail. We should try to stop health watching on all shards no

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

@ -25,8 +25,7 @@ import (
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/opentracing-contrib/go-grpc"
"github.com/opentracing/opentracing-go"
"vitess.io/vitess/go/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
@ -194,11 +193,7 @@ func interceptors() []grpc.ServerOption {
interceptors.Add(grpc_prometheus.StreamServerInterceptor, grpc_prometheus.UnaryServerInterceptor)
}
tracer := opentracing.GlobalTracer()
_, isNoopTracer := tracer.(*opentracing.NoopTracer)
if !isNoopTracer {
interceptors.Add(otgrpc.OpenTracingStreamServerInterceptor(tracer), otgrpc.OpenTracingServerInterceptor(tracer))
}
trace.AddGrpcServerOptions(interceptors.Add)
if interceptors.NonEmpty() {
return []grpc.ServerOption{