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

A newer, better thread-local API #225

Merged
merged 14 commits into from Sep 23, 2019

Conversation

@radix
Copy link
Contributor

radix commented Sep 21, 2019

Fixes #222

caveat: I didn't duplicate the greenlet support because it turns out greenlet.getcurrent() doesn't seem to return separate objects for different OS-level Python threads. That means that if you somehow have greenlet in your dependency tree, the existing thread-local stuff will just straight up not work.

If someone cares I'm sure it'd be possible to implement something that uses greenlets and threadlocal in a way that ensures that you get distinct values in different greenlets AND distinct values in different python threads.

radix added 3 commits Sep 21, 2019
… NOT return distinct values if you call it from separate python threads.

Also reformat.
@radix

This comment has been minimized.

Copy link
Contributor Author

radix commented Sep 21, 2019

I'm not sure why coverage isn't working. My tests should be covering everything 100% and I don't understand how to read the output of codecov.io

@hynek

This comment has been minimized.

Copy link
Owner

hynek commented Sep 22, 2019

This should be helpful: https://codecov.io/gh/hynek/structlog/src/222-thread-local/src/structlog/threadlocal.py#L178 Maybe you call clear before or something?


That said, could you make them both a try/except block? I'm not a fan of hasattr in my codebases.

Other that that, it looks super promising. When people asked me about support contextvars, something exactly like this came to my mind. Since the code is rather trivial, I suppose it doesn't make any sense to try to make something generic?

It's definitely the better approach than having everything global by default and offer temporary binds.

@hynek

This comment has been minimized.

Copy link
Owner

hynek commented Sep 22, 2019

OK cool. Do you need anything from me to finish this up? If you want, you can skip narrative docs for now. I can write them in a separate PR and you'll review it (timely! 🤪).

But please refer to CONTRIBUTING.rst, there's a bunch of transgressions. :)

@radix

This comment has been minimized.

Copy link
Contributor Author

radix commented Sep 22, 2019

@hynek I already started on the docs when I saw your message, so I figured I'd finish up what I have and put it up here.

Please let me know which contribution guidelines I have missed.

@radix radix marked this pull request as ready for review Sep 22, 2019
…owcase structlog.configure so I'll do that.
Copy link
Owner

hynek left a comment

Bit of bikeshedding but also please add the .. autofunctions to api.rst. Please put it at the top of the threadlocal module docs so people see it first.

tests/test_threadlocal.py Show resolved Hide resolved

def merge_threadlocal_context(logger, method_name, event_dict):
"""
A structlog processor that merges in a global (thread-local) context.

This comment has been minimized.

Copy link
@hynek

hynek Sep 22, 2019

Owner

You can drop the structlog here.

src/structlog/threadlocal.py Show resolved Hide resolved
@@ -51,7 +51,9 @@ Changes:
So far, the configuration proxy, ``structlog.processor.TimeStamper``, ``structlog.BoundLogger``, ``structlog.PrintLogger`` and ``structlog.dev.ConsoleLogger`` have been made pickelable.
Please report if you need any another class ported.
`#126 <https://github.com/hynek/structlog/issues/126>`_

- Added a new thread-local API that allows binding values to a thread-local context explicitly without affecting the default behavior of bind.

This comment has been minimized.

Copy link
@hynek

hynek Sep 22, 2019

Owner
``bind()``

please

@radix radix requested a review from hynek Sep 23, 2019
@radix

This comment has been minimized.

Copy link
Contributor Author

radix commented Sep 23, 2019

Thanks for the review @hynek, I've addressed your requests.

@hynek
hynek approved these changes Sep 23, 2019
@hynek hynek merged commit 895257b into hynek:master Sep 23, 2019
22 checks passed
22 checks passed
codecov/patch 100% of diff hit (target 100%)
Details
codecov/project 100% (target 100%)
Details
hynek.structlog Build #20190923.3 succeeded
Details
hynek.structlog (Test Docs) Test Docs succeeded
Details
hynek.structlog (Test Lint) Test Lint succeeded
Details
hynek.structlog (Test Manifest) Test Manifest succeeded
Details
hynek.structlog (Test PyPI-Description) Test PyPI-Description succeeded
Details
hynek.structlog (Test py27-colorama) Test py27-colorama succeeded
Details
hynek.structlog (Test py27-greenlets) Test py27-greenlets succeeded
Details
hynek.structlog (Test py27-oldtwisted) Test py27-oldtwisted succeeded
Details
hynek.structlog (Test py27-threads) Test py27-threads succeeded
Details
hynek.structlog (Test py35-threads) Test py35-threads succeeded
Details
hynek.structlog (Test py36-threads) Test py36-threads succeeded
Details
hynek.structlog (Test py37-colorama) Test py37-colorama succeeded
Details
hynek.structlog (Test py37-greenlets) Test py37-greenlets succeeded
Details
hynek.structlog (Test py37-oldtwisted) Test py37-oldtwisted succeeded
Details
hynek.structlog (Test py37-threads) Test py37-threads succeeded
Details
hynek.structlog (Test pypy-colorama) Test pypy-colorama succeeded
Details
hynek.structlog (Test pypy-greenlets) Test pypy-greenlets succeeded
Details
hynek.structlog (Test pypy-oldtwisted) Test pypy-oldtwisted succeeded
Details
hynek.structlog (Test pypy-threads) Test pypy-threads succeeded
Details
hynek.structlog (Test pypy3-threads) Test pypy3-threads succeeded
Details
@hynek

This comment has been minimized.

Copy link
Owner

hynek commented Sep 23, 2019

Thanks Chris, please come back soon! ;)

@hynek hynek mentioned this pull request Oct 15, 2019
@radix radix deleted the radix:222-thread-local branch Oct 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.