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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#387](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/387))
- Update redis instrumentation to follow semantic conventions
([#403](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/403))
- Update gRPC instrumentation to better wrap server context
([#420](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/420))

### Added
- `opentelemetry-instrumentation-urllib3` Add urllib3 instrumentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

@alertedsnake alertedsnake Apr 8, 2021

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.

✌️

def _state(self):
return self._servicer_context._state

@_state.setter
def _state(self, value):
self._servicer_context._state = value

@property
def _rpc_event(self):
return self._servicer_context._rpc_event

@_rpc_event.setter
def _rpc_event(self, value):
self._servicer_context._rpc_event = value

@property
def _request_deserializer(self):
return self._servicer_context._request_deserializer

@_request_deserializer.setter
def _request_deserializer(self, value):
self._servicer_context._request_deserializer = value

def is_active(self, *args, **kwargs):
return self._servicer_context.is_active(*args, **kwargs)

Expand Down