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

Support gRPC channel options #453

Closed
alertedsnake opened this issue Sep 11, 2020 · 3 comments · Fixed by #457
Closed

Support gRPC channel options #453

alertedsnake opened this issue Sep 11, 2020 · 3 comments · Fixed by #457
Labels
C-bug Category: Something isn't working

Comments

@alertedsnake
Copy link

I also work with gRPC in Go and Python, and both have a way to use the channel options to do a number of things, in my specific case I'd like to set the user-agent header, which cannot be done via metadata.

In Python it looks like this:

connect = 'localhost:50041'
user_agent = "monkey 1.0.0"
options = (
    ('grpc.primary_user_agent', user_agent),
)
channel = grpc.insecure_channel(connect, options=options)

In Go, it looks like this:

endpoint = 'localhost:50041'
user_agent := "monkey 1.0.0"
var options []grpc.DialOption
options = append(options, grpc.WithInsecure())
options = append(options, grpc.WithUserAgent(user_agent)
grpc.Dial(endpoint, options)

I cannot find a way to do this with Tonic - am I missing this? Also, I unfortunately don't know anything about Hyper or Tower, so I don't know where to start implementing it.

@alce
Copy link
Collaborator

alce commented Sep 11, 2020

You can use an interceptor to set the user agent:

let channel = Channel::from_static("http://[::1]:50051").connect().await?;

let mut client = GreeterClient::with_interceptor(channel, |mut req: Request<()>| {
    req.metadata_mut().insert(
        "x-user-agent",
        MetadataValue::from_str("some-user-agent").unwrap(),
    );
    Ok(req)
});

@alertedsnake
Copy link
Author

You can't set the metadata key user-agent as that's specifically marked as reserved, and setting a totally different key doesn't allow compatibility with other languages which use that key internally via the methods I described.

@alce alce added the C-bug Category: Something isn't working label Sep 11, 2020
@alce
Copy link
Collaborator

alce commented Sep 11, 2020

You are right, this is a bug. user-agent header should be whitelisted. I'm not sure at this point if we can just remove it from the reserved headers list but will look into it. Thanks.

alce added a commit to alce/tonic that referenced this issue Sep 18, 2020
* Adds a default user-agent to tonic `Channel`.
* The user agent can be configured through the `Channel` builder.

fixes hyperium#453
@alce alce closed this as completed in #457 Sep 23, 2020
alce added a commit that referenced this issue Sep 23, 2020
* Add a default user-agent header to outgoing requests.
* The user agent can be configured through the `Channel` builder.

fixes #453
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment