Add a span to background processes
This commit is contained in:
@@ -526,6 +526,19 @@ def start_active_span_from_edu(
|
||||
return scope
|
||||
|
||||
|
||||
def get_active_span_context():
|
||||
"""Gets the active span's context, if any.
|
||||
"""
|
||||
if not opentracing:
|
||||
return None
|
||||
|
||||
active_span = opentracing.tracer.active_span
|
||||
if not active_span:
|
||||
return None
|
||||
|
||||
return active_span.context
|
||||
|
||||
|
||||
# Opentracing setters for tags, logs, etc
|
||||
|
||||
|
||||
|
||||
@@ -25,6 +25,10 @@ from twisted.internet import defer
|
||||
from twisted.python.failure import Failure
|
||||
|
||||
from synapse.logging.context import LoggingContext, PreserveLoggingContext
|
||||
from synapse.logging.opentracing import (
|
||||
get_active_span_context,
|
||||
start_active_span_follows_from,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import resource
|
||||
@@ -188,6 +192,8 @@ def run_as_background_process(desc, func, *args, **kwargs):
|
||||
follow the synapse logcontext rules.
|
||||
"""
|
||||
|
||||
previous_span_context = get_active_span_context()
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def run():
|
||||
with _bg_metrics_lock:
|
||||
@@ -197,7 +203,7 @@ def run_as_background_process(desc, func, *args, **kwargs):
|
||||
_background_process_start_count.labels(desc).inc()
|
||||
_background_process_in_flight_count.labels(desc).inc()
|
||||
|
||||
with BackgroundProcessLoggingContext(desc) as context:
|
||||
with BackgroundProcessLoggingContext(desc, previous_span_context) as context:
|
||||
context.request = "%s-%i" % (desc, count)
|
||||
|
||||
try:
|
||||
@@ -250,12 +256,14 @@ class BackgroundProcessLoggingContext(LoggingContext):
|
||||
processes.
|
||||
"""
|
||||
|
||||
__slots__ = ["_proc"]
|
||||
__slots__ = ["_proc", "_span", "_previous_spans"]
|
||||
|
||||
def __init__(self, name: str):
|
||||
def __init__(self, name: str, previous_span_context):
|
||||
super().__init__(name)
|
||||
|
||||
self._proc = _BackgroundProcess(name, self)
|
||||
self._span = None
|
||||
self._previous_spans = [previous_span_context] if previous_span_context else []
|
||||
|
||||
def start(self, rusage: "Optional[resource._RUsage]"):
|
||||
"""Log context has started running (again).
|
||||
@@ -269,10 +277,20 @@ class BackgroundProcessLoggingContext(LoggingContext):
|
||||
with _bg_metrics_lock:
|
||||
_background_processes_active_since_last_scrape.add(self._proc)
|
||||
|
||||
def __enter__(self) -> LoggingContext:
|
||||
context = super().__enter__()
|
||||
|
||||
self._span = start_active_span_follows_from(self.name, self._previous_spans)
|
||||
self._span.__enter__()
|
||||
|
||||
return context
|
||||
|
||||
def __exit__(self, type, value, traceback) -> None:
|
||||
"""Log context has finished.
|
||||
"""
|
||||
|
||||
self._span.__exit__(type, value, traceback)
|
||||
|
||||
super().__exit__(type, value, traceback)
|
||||
|
||||
# The background process has finished. We explictly remove and manually
|
||||
|
||||
Reference in New Issue
Block a user