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

Update gRPCContext wrapper class #420

Merged
merged 6 commits into from
Apr 13, 2021

Conversation

alertedsnake
Copy link
Contributor

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.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

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?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

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.
@alertedsnake alertedsnake requested a review from a team as a code owner April 7, 2021 02:24
@alertedsnake alertedsnake requested review from codeboten and srikanthccv and removed request for a team April 7, 2021 02:24
@alertedsnake alertedsnake changed the title Update Context wrapper class Update gRPCContext wrapper class Apr 7, 2021
Copy link
Contributor

@codeboten codeboten left a 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

Copy link
Contributor

@ocelotl ocelotl left a 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
Copy link
Contributor

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__.

Copy link
Contributor Author

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.

Copy link
Contributor

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. 👍

Copy link
Contributor Author

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!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✌️

@lzchen lzchen requested a review from ocelotl April 9, 2021 16:26
@codeboten codeboten merged commit e7d26a4 into open-telemetry:main Apr 13, 2021
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

Successfully merging this pull request may close these issues.

None yet

4 participants