Skip to content

Commit d5996d4

Browse files
authored
Merge pull request #180 from getappmap/record-by-default_20221017
Fix record-by-defauilt
2 parents a27ac29 + 5fd3797 commit d5996d4

File tree

13 files changed

+68
-33
lines changed

13 files changed

+68
-33
lines changed

appmap/_implementation/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
def initialize(**kwargs):
99
check_py_version()
1010
appmapenv.initialize(**kwargs)
11+
DetectEnabled.initialize()
1112
event.initialize()
1213
importer.initialize()
1314
recorder.initialize()
1415
configuration.initialize() # needs to be initialized after recorder
1516
metadata.initialize()
16-
DetectEnabled.initialize()
1717

1818

1919
initialize()

appmap/_implementation/configuration.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import yaml
1616
from yaml.parser import ParserError
1717

18+
from appmap._implementation.detect_enabled import DetectEnabled
19+
1820
from ..labeling import presets as label_presets
1921
from . import utils
2022
from .env import Env
@@ -226,6 +228,9 @@ def _load_config(self):
226228
Env.current.enabled = False
227229

228230
def write_config_file(self, filepath, config):
231+
# HACK: don't scribble on the repo when testing
232+
if DetectEnabled.is_appmap_repo():
233+
return
229234
basedir = filepath.parent
230235
if not basedir.exists():
231236
basedir.mkdir(parents=True, exist_ok=True)

appmap/_implementation/detect_enabled.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,22 @@ def should_enable(cls, recording_method):
5858
if recording_method in cls._detected_for_method:
5959
return cls._detected_for_method[recording_method]
6060
else:
61-
message, enabled = cls.detect_should_enable(recording_method)
61+
message, enabled = cls._detect_should_enable(recording_method)
6262
cls._detected_for_method[recording_method] = enabled
6363
if enabled:
6464
logger.warning(dedent(f"AppMap recording is enabled because {message}"))
6565
return enabled
6666

6767
@classmethod
68-
def detect_should_enable(cls, recording_method):
68+
def any_enabled(cls):
69+
for m in RECORDING_METHODS:
70+
_, enabled = cls._detect_should_enable(m)
71+
if enabled:
72+
return True
73+
return False
74+
75+
@classmethod
76+
def _detect_should_enable(cls, recording_method):
6977
if not recording_method:
7078
return ["no recording method is set", False]
7179

appmap/_implementation/env.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from os import environ
66
from pathlib import Path
77

8+
from .detect_enabled import DetectEnabled
9+
810
_cwd = Path.cwd()
911
_bootenv = environ.copy()
1012

@@ -34,6 +36,7 @@ def __init__(self, env=None, cwd=None):
3436
self._env = _bootenv.copy()
3537
if env:
3638
self._env.update(env)
39+
self._enabled = DetectEnabled.any_enabled()
3740

3841
self._root_dir = str(self._cwd) + "/"
3942
self._root_dir_len = len(self._root_dir)
@@ -66,20 +69,16 @@ def output_dir(self):
6669

6770
@property
6871
def enabled(self):
69-
return self.get("APPMAP", "false").lower() == "true"
72+
return self._enabled
7073

7174
@enabled.setter
7275
def enabled(self, value):
73-
self.set("APPMAP", "true" if value else "false")
76+
self._enabled = value
7477

7578
@property
7679
def display_params(self):
7780
return self.get("APPMAP_DISPLAY_PARAMS", "true").lower() == "true"
7881

79-
@property
80-
def record_all_requests(self):
81-
return self.get("APPMAP_RECORD_REQUESTS", "false").lower() == "true"
82-
8382
def _configure_logging(self):
8483
log_level = self.get("APPMAP_LOG_LEVEL", "warning").upper()
8584

appmap/_implementation/importer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,11 @@ def instrument_functions(filterable):
200200
classes = get_classes(mod)
201201
logger.debug(" classes %s", classes)
202202
for c in classes:
203-
instrument_functions(FilterableCls(c))
203+
fc = FilterableCls(c)
204+
if fc.fqname.startswith("appmap"):
205+
logger.debug(f" not instrumenting {fc.fqname}")
206+
continue
207+
instrument_functions(fc)
204208

205209

206210
def wrap_finder_function(fn, decorator):

appmap/flask.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ def init_app(self, app):
5757
# it may record requests but not remote (APPMAP=false)
5858
self.recorder = Recorder.get_current()
5959

60-
if Env.current.enabled:
61-
# the remote recording routes are enabled only if APPMAP=true
60+
if DetectEnabled.should_enable("remote"):
6261
app.add_url_rule(
6362
self.record_url,
6463
"appmap_record_get",

appmap/test/conftest.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import appmap._implementation
88
from appmap._implementation import utils
9+
from appmap._implementation.detect_enabled import RECORDING_METHODS, DetectEnabled
910
from appmap._implementation.env import Env
1011
from appmap._implementation.recorder import Recorder
1112

@@ -55,6 +56,12 @@ def pytest_runtest_setup(item):
5556
appmap_enabled = mark_enabled.kwargs.get("appmap_enabled", "true")
5657
if isinstance(appmap_enabled, str):
5758
env["APPMAP"] = appmap_enabled
59+
elif appmap_enabled is True:
60+
env["APPMAP"] = "true"
61+
elif appmap_enabled is False:
62+
env["APPMAP"] = "false"
63+
elif appmap_enabled is None:
64+
env.pop("APPMAP", None)
5865

5966
if mark_record_requests:
6067
appmap_record_requests = mark_record_requests.kwargs.get(
@@ -77,7 +84,7 @@ def git_directory_fixture(tmp_path_factory):
7784
(git_dir / "new_file").write_text("new_file")
7885

7986
git = utils.git(cwd=git_dir)
80-
git("init")
87+
git("init --initial-branch main")
8188
git("config --local user.email test@test")
8289
git("config --local user.name Test")
8390
git("add README.metadata")

appmap/test/test_configuration.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
"""Test Configuration"""
22
# pylint: disable=missing-function-docstring
33

4-
import sys
54
from distutils.dir_util import copy_tree
65
from pathlib import Path
76

87
import pytest
98

109
import appmap
1110
import appmap._implementation
12-
import appmap._implementation.env as impl_env
1311
from appmap._implementation.configuration import Config, ConfigFilter
1412
from appmap._implementation.env import Env
1513
from appmap._implementation.importer import Filterable, NullFilter
@@ -47,28 +45,21 @@ def test_reports_invalid():
4745
assert not Config().file_valid
4846

4947

48+
@pytest.mark.appmap_enabled(config="appmap-broken.yml", appmap_enabled=None)
5049
def test_is_disabled_when_unset():
5150
"""Test that recording is disabled when APPMAP is unset"""
5251
assert Env.current.get("APPMAP", None) is None
5352

5453
assert not appmap.enabled()
5554

5655

56+
@pytest.mark.appmap_enabled(config="appmap-broken.yml", appmap_enabled="false")
5757
def test_is_disabled_when_false():
5858
"""Test that recording is disabled when APPMAP=false"""
5959
Env.current.set("APPMAP", "false")
6060
assert not appmap.enabled()
6161

6262

63-
@pytest.mark.appmap_enabled(appmap_enabled=None)
64-
def test_is_disabled_with_valid_config():
65-
c = Config()
66-
assert c.file_present
67-
assert c.file_valid
68-
69-
assert not appmap.enabled()
70-
71-
7263
def test_config_not_found(caplog):
7364
appmap._implementation.initialize(
7465
env={ # pylint: disable=protected-access
@@ -86,15 +77,16 @@ def test_config_not_found(caplog):
8677
assert f'"{not_found}" is missing' in caplog.text
8778

8879

80+
@pytest.mark.appmap_enabled(appmap_enabled="false", config="notfound.yml")
8981
def test_config_no_message(caplog):
9082
"""
9183
Messages about a missing config should only be logged when
9284
recording is enabled
9385
"""
9486

95-
assert Config().name is None
9687
assert not appmap.enabled()
97-
assert caplog.text is ""
88+
assert Config().name is None
89+
assert caplog.text == ""
9890

9991

10092
cf = lambda: ConfigFilter(NullFilter())
@@ -200,6 +192,7 @@ def test_created_if_missing_and_enabled(self, git, data_dir, monkeypatch):
200192
assert path.is_file()
201193

202194
def test_not_created_if_missing_and_not_enabled(self, git, data_dir, monkeypatch):
195+
monkeypatch.setenv("APPMAP", "false")
203196
repo_root = git.cwd
204197
copy_tree(data_dir / "config", str(repo_root))
205198
monkeypatch.chdir(repo_root)
@@ -208,7 +201,7 @@ def test_not_created_if_missing_and_not_enabled(self, git, data_dir, monkeypatch
208201
assert not path.is_file()
209202

210203
# pylint: disable=protected-access
211-
appmap._implementation.initialize(cwd=repo_root, env={"APPMAP": "false"})
204+
appmap._implementation.initialize(cwd=repo_root)
212205

213206
c = Config()
214207
assert not path.is_file()

appmap/test/test_flask.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
from appmap.test.helpers import DictIncluding
1212

1313
from .._implementation.metadata import Metadata
14+
15+
# Make sure assertions in web_framework get rewritten (e.g. to show
16+
# diffs in generated appmaps)
17+
pytest.register_assert_rewrite("appmap.test.web_framework")
18+
1419
from .web_framework import ( # pylint: disable=unused-import
1520
TestRecording,
1621
TestRecordRequests,

appmap/test/test_metadata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_git_metadata(git):
2323
assert git_md == DictIncluding(
2424
{
2525
"repository": "https://www.example.test/repo.git",
26-
"branch": "master",
26+
"branch": "main",
2727
"status": ["?? new_file"],
2828
}
2929
)
@@ -55,7 +55,7 @@ def test_tags(git):
5555
assert git_md == DictIncluding(
5656
{
5757
"repository": "https://www.example.test/repo.git",
58-
"branch": "master",
58+
"branch": "main",
5959
"tag": tag,
6060
"annotated_tag": atag,
6161
"commits_since_tag": 1,

0 commit comments

Comments
 (0)