|
| 1 | +""" |
| 2 | +Detect if AppMap is enabled. |
| 3 | +""" |
| 4 | + |
| 5 | +import importlib |
| 6 | +import logging |
| 7 | +import os |
| 8 | +from textwrap import dedent |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | +RECORDING_METHODS = ["pytest", "unittest", "remote", "requests"] |
| 13 | + |
| 14 | +# Detects whether AppMap recording should be enabled. This test can be |
| 15 | +# performed generally, or for a particular recording method. Recording |
| 16 | +# can be enabled explicitly, for example via APPMAP=true, or it can be |
| 17 | +# enabled implicitly, by running in a dev or test web application |
| 18 | +# environment. Recording can also be disabled explicitly, using |
| 19 | +# environment variables. |
| 20 | +class DetectEnabled: |
| 21 | + _instance = None |
| 22 | + |
| 23 | + def __new__(cls): |
| 24 | + if cls._instance is None: |
| 25 | + logger.debug("Creating the DetectEnabled object") |
| 26 | + cls._instance = super(DetectEnabled, cls).__new__(cls) |
| 27 | + cls._instance._initialized = False |
| 28 | + return cls._instance |
| 29 | + |
| 30 | + def __init__(self): |
| 31 | + if self._initialized: |
| 32 | + return |
| 33 | + |
| 34 | + self._initialized = True |
| 35 | + self._detected_for_method = {} |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def initialize(cls): |
| 39 | + cls._instance = None |
| 40 | + # because apparently __new__ and __init__ don't get called |
| 41 | + cls._detected_for_method = {} |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def clear_cache(cls): |
| 45 | + cls._detected_for_method = {} |
| 46 | + |
| 47 | + @classmethod |
| 48 | + def is_appmap_repo(cls): |
| 49 | + return os.path.exists("appmap/__init__.py") and os.path.exists( |
| 50 | + "appmap/_implementation/__init__.py" |
| 51 | + ) |
| 52 | + |
| 53 | + @classmethod |
| 54 | + def should_enable(cls, recording_method): |
| 55 | + """ |
| 56 | + Should recording be enabled for the current recording method? |
| 57 | + """ |
| 58 | + if recording_method in cls._detected_for_method: |
| 59 | + return cls._detected_for_method[recording_method] |
| 60 | + else: |
| 61 | + message, enabled = cls.detect_should_enable(recording_method) |
| 62 | + cls._detected_for_method[recording_method] = enabled |
| 63 | + if enabled: |
| 64 | + logger.warning(dedent(f"AppMap recording is enabled because {message}")) |
| 65 | + return enabled |
| 66 | + |
| 67 | + @classmethod |
| 68 | + def detect_should_enable(cls, recording_method): |
| 69 | + if not recording_method: |
| 70 | + return ["no recording method is set", False] |
| 71 | + |
| 72 | + if recording_method not in RECORDING_METHODS: |
| 73 | + return ["invalid recording method", False] |
| 74 | + |
| 75 | + # explicitly disabled or enabled |
| 76 | + if "APPMAP" in os.environ: |
| 77 | + if os.environ["APPMAP"] == "false": |
| 78 | + return ["APPMAP=false", False] |
| 79 | + elif os.environ["APPMAP"] == "true": |
| 80 | + return ["APPMAP=true", True] |
| 81 | + |
| 82 | + # recording method explicitly disabled or enabled |
| 83 | + if recording_method: |
| 84 | + for one_recording_method in RECORDING_METHODS: |
| 85 | + if one_recording_method == recording_method.lower(): |
| 86 | + env_var = "_".join(["APPMAP", "RECORD", recording_method.upper()]) |
| 87 | + if env_var in os.environ: |
| 88 | + if os.environ[env_var] == "false": |
| 89 | + return [f"{env_var}=false", False] |
| 90 | + elif os.environ[env_var] == "true": |
| 91 | + return [f"{env_var}=true", True] |
| 92 | + |
| 93 | + # it's flask |
| 94 | + message, should_enable = cls.is_flask_and_should_enable() |
| 95 | + if should_enable == True or should_enable == False: |
| 96 | + return [message, should_enable] |
| 97 | + |
| 98 | + # it's django |
| 99 | + message, should_enable = cls.is_django_and_should_enable() |
| 100 | + if should_enable == True or should_enable == False: |
| 101 | + return [message, should_enable] |
| 102 | + |
| 103 | + if recording_method in RECORDING_METHODS: |
| 104 | + return ["will record by default", True] |
| 105 | + |
| 106 | + return ["it's not enabled by any configuration or framework", False] |
| 107 | + |
| 108 | + @classmethod |
| 109 | + def is_flask_and_should_enable(cls): |
| 110 | + if "FLASK_DEBUG" in os.environ: |
| 111 | + if os.environ["FLASK_DEBUG"] == "1": |
| 112 | + return [f"FLASK_DEBUG={os.environ['FLASK_DEBUG']}", True] |
| 113 | + elif os.environ["FLASK_DEBUG"] == "0": |
| 114 | + return [f"FLASK_DEBUG={os.environ['FLASK_DEBUG']}", False] |
| 115 | + |
| 116 | + if "FLASK_ENV" in os.environ: |
| 117 | + if os.environ["FLASK_ENV"] == "development": |
| 118 | + return [f"FLASK_ENV={os.environ['FLASK_ENV']}", True] |
| 119 | + elif os.environ["FLASK_ENV"] == "production": |
| 120 | + return [f"FLASK_ENV={os.environ['FLASK_ENV']}", False] |
| 121 | + |
| 122 | + return ["it's not Flask", None] |
| 123 | + |
| 124 | + @classmethod |
| 125 | + def is_django_and_should_enable(cls): |
| 126 | + if ( |
| 127 | + "DJANGO_SETTINGS_MODULE" in os.environ |
| 128 | + and os.environ["DJANGO_SETTINGS_MODULE"] != "" |
| 129 | + ): |
| 130 | + try: |
| 131 | + settings = importlib.import_module(os.environ["DJANGO_SETTINGS_MODULE"]) |
| 132 | + except Exception as exn: |
| 133 | + settings = None |
| 134 | + return [ |
| 135 | + "couldn't load DJANGO_SETTINGS_MODULE={os.environ['DJANGO_SETTINGS_MODULE']}", |
| 136 | + False, |
| 137 | + ] |
| 138 | + |
| 139 | + if settings: |
| 140 | + try: |
| 141 | + # don't crash if the settings file doesn't contain |
| 142 | + # a DEBUG variable |
| 143 | + if settings.DEBUG == True: |
| 144 | + return [ |
| 145 | + f"{os.environ['DJANGO_SETTINGS_MODULE']}.DEBUG={settings.DEBUG}", |
| 146 | + True, |
| 147 | + ] |
| 148 | + elif settings.DEBUG == False: |
| 149 | + return [ |
| 150 | + f"{os.environ['DJANGO_SETTINGS_MODULE']}.DEBUG={settings.DEBUG}", |
| 151 | + False, |
| 152 | + ] |
| 153 | + except AttributeError as exn: |
| 154 | + # it wasn't set. it's ok. don't crash |
| 155 | + # AttributeError: module 'app.settings_appmap_false' has no attribute 'DEBUG' |
| 156 | + pass |
| 157 | + |
| 158 | + if settings: |
| 159 | + try: |
| 160 | + # don't crash if the settings file doesn't contain |
| 161 | + # an APPMAP variable |
| 162 | + if ( |
| 163 | + settings.APPMAP == True |
| 164 | + or str(settings.APPMAP).upper() == "true".upper() |
| 165 | + ): |
| 166 | + return [ |
| 167 | + f"{os.environ['DJANGO_SETTINGS_MODULE']}.APPMAP={settings.APPMAP}", |
| 168 | + True, |
| 169 | + ] |
| 170 | + elif ( |
| 171 | + settings.APPMAP == False |
| 172 | + or str(settings.APPMAP).upper() == "false".upper() |
| 173 | + ): |
| 174 | + return [ |
| 175 | + f"{os.environ['DJANGO_SETTINGS_MODULE']}.APPMAP={settings.APPMAP}", |
| 176 | + False, |
| 177 | + ] |
| 178 | + except AttributeError as exn: |
| 179 | + # it wasn't set. it's ok. don't crash |
| 180 | + # AttributeError: module 'app.settings_appmap_false' has no attribute 'APPMAP' |
| 181 | + pass |
| 182 | + |
| 183 | + return ["it's not Django", None] |
0 commit comments