Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Refactor context propagation to work with async#588

Merged
reyang merged 3 commits intomasterfrom
experimental
Apr 1, 2019
Merged

Refactor context propagation to work with async#588
reyang merged 3 commits intomasterfrom
experimental

Conversation

@reyang
Copy link
Copy Markdown
Contributor

@reyang reyang commented Mar 29, 2019

This is to merge #566 and #573 to master.

@reyang reyang requested review from a team, c24t and songy23 as code owners March 29, 2019 17:36
reyang added 2 commits April 1, 2019 10:41
Introduced the generic RuntimeContext.
Migrate execution_context to RuntimeContext
Copy link
Copy Markdown
Member

@c24t c24t left a comment

Choose a reason for hiding this comment

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

The context changes LGTM, and this is a huge improvement over threadlocals. We still have some work to do to change how the context is used in the library (e.g. removing the tracer, measure to view map, etc.) and adding/using context managers as in your Span example, but this is a problem for another PR.

I think it's possible that this PR will introduce bugs in instrumented code because of the different behavior between 2 and 3, especially in code that touches thread-local storage or changes the default thread creation behavior (like we do in opencensus-ext-threading). We may want to figure out exactly how this is likely to break instrumented code and document it before releasing.


from opencensus.common.runtime_context import RuntimeContext

RuntimeContext.register_slot('correlation_context', lambda: {})
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: I think it's more idiomatic to use dict than lambda: {}, but your call.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I picked explicitly lambda in order to tell ctor from a type name.
For example isinstance(x, Foo) and map(lambda x: Foo(x), range(10)).

# See the License for the specific language governing permissions and
# limitations under the License.

from multiprocessing.dummy import Pool as ThreadPool
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why multiprocessing...ThreadPool instead of concurrent.futures.ThreadPoolExecutor?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This former works with Python 2.7.


class Slot(object):
def __init__(self, name, default):
import contextvars
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why bury the import in here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was trying to reduce the visibility of contextvars to avoid accidental use.
Alternatively we could use if/else and import at the top level (which seems harder to maintain?)

@reyang
Copy link
Copy Markdown
Contributor Author

reyang commented Apr 1, 2019

I think it's possible that this PR will introduce bugs in instrumented code because of the different behavior between 2 and 3, especially in code that touches thread-local storage or changes the default thread creation behavior (like we do in opencensus-ext-threading). We may want to figure out exactly how this is likely to break instrumented code and document it before releasing.

We need to take a closer look at opencensus-ext-threading and how we propagate across process (multi-processing).
I plan to add doc, more test cases and samples to the context library in another PR.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants