|
|
|
@ -13,9 +13,11 @@ import (
|
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// OpenTracingInterceptor returns a grpc.UnaryClientInterceptor for opentelemetry.
|
|
|
|
|
func OpenTracingInterceptor() grpc.UnaryClientInterceptor {
|
|
|
|
|
propagator := otel.GetTextMapPropagator()
|
|
|
|
|
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
|
|
|
|
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
|
|
|
|
|
invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
|
|
|
|
if !opentelemetry.Enabled() {
|
|
|
|
|
return invoker(ctx, method, req, reply, cc, opts...)
|
|
|
|
|
}
|
|
|
|
@ -24,13 +26,8 @@ func OpenTracingInterceptor() grpc.UnaryClientInterceptor {
|
|
|
|
|
metadataCopy := requestMetadata.Copy()
|
|
|
|
|
tr := otel.Tracer(opentelemetry.TraceName)
|
|
|
|
|
name, attr := opentelemetry.SpanInfo(method, cc.Target())
|
|
|
|
|
|
|
|
|
|
var span trace.Span
|
|
|
|
|
ctx, span = tr.Start(ctx,
|
|
|
|
|
name,
|
|
|
|
|
trace.WithSpanKind(trace.SpanKindClient),
|
|
|
|
|
trace.WithAttributes(attr...),
|
|
|
|
|
)
|
|
|
|
|
ctx, span := tr.Start(ctx, name, trace.WithSpanKind(trace.SpanKindClient),
|
|
|
|
|
trace.WithAttributes(attr...))
|
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
|
|
opentelemetry.Inject(ctx, propagator, &metadataCopy)
|
|
|
|
@ -50,30 +47,23 @@ func OpenTracingInterceptor() grpc.UnaryClientInterceptor {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StreamOpenTracingInterceptor returns a grpc.StreamClientInterceptor for opentelemetry.
|
|
|
|
|
func StreamOpenTracingInterceptor() grpc.StreamClientInterceptor {
|
|
|
|
|
propagator := otel.GetTextMapPropagator()
|
|
|
|
|
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
|
|
|
|
|
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,
|
|
|
|
|
streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
|
|
|
|
|
if !opentelemetry.Enabled() {
|
|
|
|
|
return streamer(ctx, desc, cc, method, opts...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requestMetadata, _ := metadata.FromOutgoingContext(ctx)
|
|
|
|
|
metadataCopy := requestMetadata.Copy()
|
|
|
|
|
|
|
|
|
|
tr := otel.Tracer(opentelemetry.TraceName)
|
|
|
|
|
|
|
|
|
|
name, attr := opentelemetry.SpanInfo(method, cc.Target())
|
|
|
|
|
var span trace.Span
|
|
|
|
|
ctx, span = tr.Start(
|
|
|
|
|
ctx,
|
|
|
|
|
name,
|
|
|
|
|
trace.WithSpanKind(trace.SpanKindClient),
|
|
|
|
|
trace.WithAttributes(attr...),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
ctx, span := tr.Start(ctx, name, trace.WithSpanKind(trace.SpanKindClient),
|
|
|
|
|
trace.WithAttributes(attr...))
|
|
|
|
|
opentelemetry.Inject(ctx, propagator, &metadataCopy)
|
|
|
|
|
ctx = metadata.NewOutgoingContext(ctx, metadataCopy)
|
|
|
|
|
|
|
|
|
|
s, err := streamer(ctx, desc, cc, method, opts...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
grpcStatus, _ := status.FromError(err)
|
|
|
|
@ -82,12 +72,11 @@ func StreamOpenTracingInterceptor() grpc.StreamClientInterceptor {
|
|
|
|
|
span.End()
|
|
|
|
|
return s, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stream := opentelemetry.WrapClientStream(ctx, s, desc)
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
err := <-stream.Finished
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err := <-stream.Finished; err != nil {
|
|
|
|
|
s, _ := status.FromError(err)
|
|
|
|
|
span.SetStatus(codes.Error, s.Message())
|
|
|
|
|
span.SetAttributes(opentelemetry.StatusCodeAttr(s.Code()))
|
|
|
|
|