-
Notifications
You must be signed in to change notification settings - Fork 626
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
Update gRPCContext wrapper class #420
Conversation
There are a few cases where one needs to dig into `grpc.ServicerContext` objects, and these fields were missing from our wrapper, which can cause issues with implmementation. Yes, they're private attributes, but there's no way to get the data they hold otherwise, so we should properly map them so our wrapper Context behaves like a real one does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good, is it worth adding a test around this? @alertedsnake
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the main objective of this PR, @alertedsnake, but please consider reimplementing this with __getattr__
instead of relying on private attributes.
@@ -71,6 +71,30 @@ def __init__(self, servicer_context, active_span): | |||
self.details = None | |||
super().__init__() | |||
|
|||
@property |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still, we should not use someone else's private attributes. Also, next time we need to access a private attribute we would need to add yet another property.
I feel like this is a job for __getattr__
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could agree with that, though since this class is a wrapper instead of a subclass, this is always going to be a problem when the wrapped class gets changed and people expect those changes to propagate. In fact, now that I look again, we're missing a private function as well, _finalize_state()
.
Not sure who the original author was, but I wonder why the choice was made to wrap this rather than subclass it? Possibly because the grpc
implementation is also marked private.
(edited, after I refreshed my memory on how __getattr__
works) I'll try re-implementing this with your suggestion, but still curious about the original decision, if anyone knows the answer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, use the wrapped attribute if the attribute is not found in the wrapper. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I know the answer to my question - because the base class is a metadata class, the real class is that private _Context
in the private grpc._server
module.
Expect an update later today, and thanks for the tip!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✌️
Based on feedback from @ocelotl.
Description
There are a few cases where one needs to dig into
grpc.ServicerContext
objects, and these fields were missing from our wrapper, which can cause issues with implmementation.Yes, they're private attributes, but there's no way to get the data they hold otherwise, so we should properly map them so our wrapper Context behaves like a real one does.
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
There don't seem to be any test cases which cover this - I only noticed because I have an interceptor that reports exceptions to Sentry, and so it has to dig into the
grpc.ServicerContext
object for details, rather than wrapping it like we do here.Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.