Create logcontext for new scope.
Instead of having a stack of scopes in a logcontext we create a new context for a new scope if the current logcontext already has a scope.pull/5544/head
parent
fc069fee9c
commit
227447dc7e
|
@ -164,7 +164,7 @@ class LoggingContext(object):
|
||||||
"usage_start",
|
"usage_start",
|
||||||
"main_thread", "alive",
|
"main_thread", "alive",
|
||||||
"request", "tag",
|
"request", "tag",
|
||||||
"active_scope"
|
"scope"
|
||||||
]
|
]
|
||||||
|
|
||||||
thread_local = threading.local()
|
thread_local = threading.local()
|
||||||
|
@ -216,7 +216,7 @@ class LoggingContext(object):
|
||||||
self.request = None
|
self.request = None
|
||||||
self.tag = ""
|
self.tag = ""
|
||||||
self.alive = True
|
self.alive = True
|
||||||
self.active_scope = None
|
self.scope = None
|
||||||
|
|
||||||
self.parent_context = parent_context
|
self.parent_context = parent_context
|
||||||
|
|
||||||
|
@ -304,8 +304,8 @@ class LoggingContext(object):
|
||||||
# we track the current request
|
# we track the current request
|
||||||
record.request = self.request
|
record.request = self.request
|
||||||
|
|
||||||
# we also track the current active_scope:
|
# we also track the current scope:
|
||||||
record.active_scope = self.active_scope
|
record.scope = self.scope
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
if threading.current_thread() is not self.main_thread:
|
if threading.current_thread() is not self.main_thread:
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from .logcontext import LoggingContext
|
from .logcontext import LoggingContext, nested_logging_context
|
||||||
from opentracing import ScopeManager, Scope
|
from opentracing import ScopeManager, Scope
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
@ -29,10 +29,10 @@ class LogContextScopeManager(ScopeManager):
|
||||||
available.
|
available.
|
||||||
"""
|
"""
|
||||||
ctx = LoggingContext.current_context()
|
ctx = LoggingContext.current_context()
|
||||||
if ctx is LoggingContext.sentinel or ctx.active_scope is None:
|
if ctx is LoggingContext.sentinel:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return ctx.active_scope
|
return ctx.scope
|
||||||
|
|
||||||
def activate(self, span, finish_on_close):
|
def activate(self, span, finish_on_close):
|
||||||
"""
|
"""
|
||||||
|
@ -47,23 +47,20 @@ class LogContextScopeManager(ScopeManager):
|
||||||
*span*. It is a programming error to neglect to call
|
*span*. It is a programming error to neglect to call
|
||||||
Scope.close() on the returned instance.
|
Scope.close() on the returned instance.
|
||||||
"""
|
"""
|
||||||
logger.info("activating scope")
|
|
||||||
|
enter_logcontext = False
|
||||||
ctx = LoggingContext.current_context()
|
ctx = LoggingContext.current_context()
|
||||||
if ctx is LoggingContext.sentinel:
|
if ctx is LoggingContext.sentinel:
|
||||||
# We don't want this scope to affect.
|
# We don't want this scope to affect.
|
||||||
logger.warning("Tried to activate scope outside of loggingcontext")
|
logger.warning("Tried to activate scope outside of loggingcontext")
|
||||||
return Scope(None, span)
|
return Scope(None, span)
|
||||||
|
elif ctx.scope is not None:
|
||||||
scope = _LogContextScope(self, span, finish_on_close)
|
ctx = nested_logging_context("scope")
|
||||||
self._set_logcontext_scope(scope, ctx)
|
enter_logcontext = True
|
||||||
|
scope = _LogContextScope(self, span, ctx, enter_logcontext, finish_on_close)
|
||||||
|
ctx.scope = scope
|
||||||
return scope
|
return scope
|
||||||
|
|
||||||
def _set_logcontext_scope(self, scope, ctx=None):
|
|
||||||
if ctx is None:
|
|
||||||
ctx = LoggingContext.current_context()
|
|
||||||
|
|
||||||
ctx.active_scope = scope
|
|
||||||
|
|
||||||
def request_from_whitelisted_homeserver(self, request):
|
def request_from_whitelisted_homeserver(self, request):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -71,17 +68,25 @@ class LogContextScopeManager(ScopeManager):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class _LogContextScope(Scope):
|
class _LogContextScope(Scope):
|
||||||
def __init__(self, manager, span, finish_on_close):
|
def __init__(self, manager, span, logcontext, enter_logcontext, finish_on_close):
|
||||||
super(_LogContextScope, self).__init__(manager, span)
|
super(_LogContextScope, self).__init__(manager, span)
|
||||||
|
self.logcontext = logcontext
|
||||||
self._finish_on_close = finish_on_close
|
self._finish_on_close = finish_on_close
|
||||||
self._to_restore = manager.active
|
self._enter_logcontext = enter_logcontext
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
if self._enter_logcontext:
|
||||||
|
self.logcontext.__enter__()
|
||||||
|
|
||||||
|
def __exit__(self, type, value, traceback):
|
||||||
|
super(_LogContextScope, self).__exit__(type, value, traceback)
|
||||||
|
if self._enter_logcontext:
|
||||||
|
self.logcontext.__exit__(type, value, traceback)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
if self.manager.active is not self:
|
if self.manager.active is not self:
|
||||||
logger.warning("Tried to close a none active scope!")
|
logger.warning("Tried to close a none active scope!")
|
||||||
return
|
return
|
||||||
|
|
||||||
self.manager._set_logcontext_scope(self._to_restore)
|
|
||||||
|
|
||||||
if self._finish_on_close:
|
if self._finish_on_close:
|
||||||
self.span.finish()
|
self.span.finish()
|
Loading…
Reference in New Issue