summary refs log tree commit diff
diff options
context:
space:
mode:
authorOlivier Wilkinson (reivilibre) <oliverw@matrix.org>2022-01-19 10:27:49 +0000
committerOlivier Wilkinson (reivilibre) <oliverw@matrix.org>2022-01-19 10:27:49 +0000
commit6717a84678b04e849c9b7d8b41d835939a753b9d (patch)
treec2898e4741670d511df40048d19274ac8ad13400
parentWording fixes to 1.50.0/1 changelog entries (diff)
downloadsynapse-6717a84678b04e849c9b7d8b41d835939a753b9d.tar.xz
Allow installing VizTracer with SYNAPSE_VIZTRACER=1 env var
-rw-r--r--mypy.ini3
-rw-r--r--synapse/app/homeserver.py22
-rw-r--r--synapse/python_dependencies.py3
3 files changed, 28 insertions, 0 deletions
diff --git a/mypy.ini b/mypy.ini
index 85fa22d28f..070ce06556 100644
--- a/mypy.ini
+++ b/mypy.ini
@@ -349,5 +349,8 @@ ignore_missing_imports = True
 [mypy-twisted.*]
 ignore_missing_imports = True
 
+[mypy-viztracer]
+ignore_missing_imports = True
+
 [mypy-zope]
 ignore_missing_imports = True
diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py
index dd76e07321..57e57500ed 100644
--- a/synapse/app/homeserver.py
+++ b/synapse/app/homeserver.py
@@ -446,6 +446,28 @@ def run(hs: HomeServer) -> None:
     )
 
 
+def setup_viztracer() -> None:
+    """
+    If installed and enabled by environment variable SYNAPSE_VIZTRACER=1,
+    install VizTracer's hooks so that Synapse can be attached to by VizTracer
+    at some point during runtime.
+    """
+
+    if os.environ.get("SYNAPSE_VIZTRACER", ""):
+        logger.info("SYNAPSE_VIZTRACER is set. Installing VizTracer hooks.")
+
+        try:
+            from viztracer import VizTracer
+
+            VizTracer().install()
+        except ImportError:
+            logger.info("VizTracer could not be imported: can't install hooks.")
+    else:
+        logger.info(
+            "SYNAPSE_VIZTRACER not set (or is empty): won't install VizTracer hooks."
+        )
+
+
 def main() -> None:
     with LoggingContext("main"):
         # check base requirements
diff --git a/synapse/python_dependencies.py b/synapse/python_dependencies.py
index d844fbb3b3..96a4b1b529 100644
--- a/synapse/python_dependencies.py
+++ b/synapse/python_dependencies.py
@@ -116,6 +116,9 @@ CONDITIONAL_REQUIREMENTS = {
     "redis": ["txredisapi>=1.4.7", "hiredis"],
     # Required to use experimental `caches.track_memory_usage` config option.
     "cache_memory": ["pympler"],
+    # Required to install VizTracer hooks, which allows VizTracer to be attached
+    # at runtime.
+    "viztracer": ["viztracer"],
 }
 
 ALL_OPTIONAL_REQUIREMENTS: Set[str] = set()