Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The execution order of otelgrpc.StreamClientInterceptor() and otelgrpc.NewClientHandler() has changed. #745

Open
b-anastasiia opened this issue Jan 16, 2025 · 1 comment

Comments

@b-anastasiia
Copy link

After otelgrpc.StreamClientInterceptor() was deprecated, it is recommended to replace it with otelgrpc.NewClientHandler().

However, the execution order is different: interceptors run first, followed by StatsHandlers. From the code, this seems to be the intended behavior. But how should one proceed if it’s necessary for the trace_id and span_id fields to appear in the logs?

Before (logs with traicing fields):

    func SpanContextFields(ctx context.Context) logging.Fields {
        if spanCtx := trace.SpanContextFromContext(ctx); spanCtx.IsSampled() {
            return logging.Fields{
                traceIDKey, spanCtx.TraceID().String(),
                spanIDKey, spanCtx.SpanID().String(),
            }
         }

         return nil
     }

    streamInterceptors := []grpc.StreamClientInterceptor{
        otelgrpc.StreamClientInterceptor(),
        logging.StreamClientInterceptor(
            logger.GRPCLogger(zap.L()),
            logging.WithLevels(logger.GRPCClientCodeToLevel),
            logging.WithFieldsFromContext(logger.SpanContextFields),
        ),
     }
	
    return grpc.NewClient(
        target,
        grpc.WithTransportCredentials(insecure.NewCredentials()),
        grpc.WithChainUnaryInterceptor(append(unaryInterceptors, o.unaryInterceptors...)...),
        grpc.WithChainStreamInterceptor(append(streamInterceptors, o.streamInterceptors...)...),
     )

Now (logs without traicing fields):

    func SpanContextFields(ctx context.Context) logging.Fields {
        if spanCtx := trace.SpanContextFromContext(ctx); spanCtx.IsSampled() {
            return logging.Fields{
                traceIDKey, spanCtx.TraceID().String(),
                spanIDKey, spanCtx.SpanID().String(),
            }
         }

         return nil
     }

    streamInterceptors := []grpc.StreamClientInterceptor{
        logging.StreamClientInterceptor(
            logger.GRPCLogger(zap.L()),
            logging.WithLevels(logger.GRPCClientCodeToLevel),
            logging.WithFieldsFromContext(logger.SpanContextFields),
        ),
    }
	
    return grpc.NewClient(
        target,
        grpc.WithTransportCredentials(insecure.NewCredentials()),
        grpc.WithStatsHandler(otelgrpc.NewClientHandler()),
        grpc.WithChainUnaryInterceptor(append(unaryInterceptors, o.unaryInterceptors...)...),
        grpc.WithChainStreamInterceptor(append(streamInterceptors, o.streamInterceptors...)...),
     )
@johanbrandhorst
Copy link
Collaborator

The statshandler runs both before and after the interceptor. I don't know if it's possible to reproduce the exact same behavior using the stats handler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants