Last active
August 29, 2015 13:56
-
-
Save kgriffs/8852247 to your computer and use it in GitHub Desktop.
Thread local example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# solom/__init__.py | |
THREAD_LOCAL = threading.local() | |
# In some pecan helper module... | |
def before_hook(self): | |
# Use thread-local storage so we don't have to | |
# add two extra params to virtuall every single | |
# function in our app! | |
solum.THREAD_LOCAL.ctx = context.RequestContext(...) | |
solum.THREAD_LOCAL.trace = ApiTrace() | |
# Meanwhile... | |
def my_cool_function(self): | |
# We can add utils to reduce the amount of | |
# typing you have to do, but this would be | |
# the underlying interface... | |
trace = solum.THREAD_LOCAL.trace | |
# Stores under { ... "my_cool_function" : { "pulled_hash": github_hash } ... } | |
trace.user(pulled_hash=github_hash) | |
trace.support(useful={...}, json_serializable=[...]) | |
#.... | |
# We can consider creating a very thin adapter on top of oslo logging to DRY this up. | |
LOG.info('Doing this and that now...', | |
context=solum.THREAD_LOCAL.ctx, extra=solum.THREAD_LOCAL.trace) | |
# Another possibility | |
LOG.info('Doing this and that now...', *solum.log_kwargs()) | |
def another_function(self): | |
try: | |
# ... | |
except Exception as ex: | |
# Yah, I get the trace info from my_cool_function here. In the case | |
# that this error was in part caused by a side-affect of something | |
# that my_cool_function did, we will want that other info. | |
LOG.exception(ex, *solum.log_kwargs()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment