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

CallOptions.withStreamTracerFactory api #954

Closed
313hemant313 opened this issue Aug 30, 2023 · 8 comments
Closed

CallOptions.withStreamTracerFactory api #954

313hemant313 opened this issue Aug 30, 2023 · 8 comments
Labels
enhancement A feature request or improvement

Comments

@313hemant313
Copy link
Contributor

Wanted to use below grpc-java api's to trace network issues / retry logging.

CallOptions.withStreamTracerFactory(ClientStreamTracer.Factory factory)
ClientStreamTracer.inboundTrailers(Metadata trailers)
StreamTracer.streamClosed(Status status)

How i can use these with spring grpc ?

@313hemant313 313hemant313 added the enhancement A feature request or improvement label Aug 30, 2023
@313hemant313
Copy link
Contributor Author

@ST-DDT please guide.

@ST-DDT
Copy link
Collaborator

ST-DDT commented Aug 30, 2023

The same way as in plain grpc java.
Although you could use a configurer bean to set the options globally.

Please don't ping me if you posted the issues just a few hours ago.

@313hemant313
Copy link
Contributor Author

@ST-DDT Sorry accidentally tagged you here... wanted to tag on https://github.com/yidongnan/grpc-spring-boot-starter/issues/951

Thanks for your reply.

@313hemant313
Copy link
Contributor Author

313hemant313 commented Aug 31, 2023

@ST-DDT please help

Not able to find withStreamTracerFactory in both the options GrpcChannelConfigurer or StubTransformer.

Tried this but not sure if this will replace any existing ClientStreamTracker or not.

@Bean
public StubTransformer customStubTransformer() {
    return (name, stub) -> stub.withOption(CallOptions.Key.create("streamTracerFactories"), List.of(new ClientStreamTracer.Factory() {
        @Override
        public ClientStreamTracer newClientStreamTracer(ClientStreamTracer.StreamInfo info, Metadata headers) {
            return new CustomClientStreamTracer(info);
        }
    }));
}

@313hemant313
Copy link
Contributor Author

@ST-DDT Grpc-java suggested to use newStub(channel, callOptions) grpc/grpc-java#2143,

How to go about it in grpc-spring-boot-starter ?

@ST-DDT
Copy link
Collaborator

ST-DDT commented Sep 16, 2023

Grpc-java suggested to use newStub(channel, callOptions) grpc/grpc-java#2143,

You can replace the stub with a stub transformer or provide a custom stub factory.

But it was also mentioned that you could use an interceptor to set it. Which I would recommend.

@313hemant313
Copy link
Contributor Author

Not sure how to attach Tracer with interceptor.

This is how we create stub.
AbstractStub<S> modifiedStub = stub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(metadata));

This is the client tracer that i need to attach.

public class CustomClientStreamTracer extends ClientStreamTracer {

    private final ClientStreamTracer.StreamInfo streamInfo;

    public CustomClientStreamTracer(ClientStreamTracer.StreamInfo streamInfo) {
        this.streamInfo = streamInfo;
    }

    @Override
    public void streamClosed(Status status) {
        if (status != Status.OK) {
            log.warn("For gRPC streamClosed with status: {}, isTransparentRetry: {} and previousAttempts: {}", status,
                    streamInfo.isTransparentRetry(), streamInfo.getPreviousAttempts());
        }
        super.streamClosed(status);
    }
}

@ST-DDT please guide.

@ST-DDT
Copy link
Collaborator

ST-DDT commented Sep 18, 2023

Somewhat like this:

@GrpcGlobalClientInterceptor
class MyInterceptor implements ClientInterceptor {

    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
      return next.newCall(method, callOptions.withStreamTracerFactory(tracerFactory));
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature request or improvement
Projects
None yet
Development

No branches or pull requests

2 participants