Coverage for app/backend/src/tests/test_profiling.py: 100%

84 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-10 09:48 +0000

1import pyroscope 

2import pytest 

3 

4from couchers import profiling 

5from couchers.config import config 

6from tests.fixtures.feature_flags import FeatureFlags 

7 

8TAGS = {"role": "api", "instance": "api-1761"} 

9 

10 

11@pytest.fixture 

12def fake_agent(monkeypatch): 

13 """Replace the native pyroscope agent with recorders and reset the per-process agent state.""" 

14 configure_calls: list[dict[str, object]] = [] 

15 shutdown_calls: list[None] = [] 

16 monkeypatch.setattr(pyroscope, "configure", lambda **kw: configure_calls.append(kw)) 

17 monkeypatch.setattr(pyroscope, "shutdown", lambda: shutdown_calls.append(None)) 

18 monkeypatch.setattr(profiling, "_tags", TAGS) 

19 monkeypatch.setattr(profiling, "_running", False) 

20 monkeypatch.setattr(profiling, "_sample_rate", None) 

21 monkeypatch.setattr(profiling, "_oncpu", None) 

22 monkeypatch.setitem(config, "PYROSCOPE_SERVER", "https://localhost") 

23 monkeypatch.setitem(config, "PYROSCOPE_AUTH_TOKEN", "token") 

24 return configure_calls, shutdown_calls 

25 

26 

27def _enable(feature_flags: FeatureFlags, *, rate=20, mode="wall"): 

28 feature_flags.set("profiling_enabled", True) 

29 feature_flags.set("profiling_sample_rate", rate) 

30 feature_flags.set("profiling_mode", mode) 

31 

32 

33def test_disabled_does_nothing(feature_flags: FeatureFlags, fake_agent): 

34 configure_calls, shutdown_calls = fake_agent 

35 feature_flags.set("profiling_enabled", False) 

36 profiling._reconcile() 

37 assert configure_calls == [] 

38 assert shutdown_calls == [] 

39 assert profiling._running is False 

40 

41 

42def test_enables_with_flag_driven_params(feature_flags: FeatureFlags, fake_agent, monkeypatch): 

43 monkeypatch.setitem(config, "PYROSCOPE_SERVER", "https://pyroscope.couchershq.org") 

44 monkeypatch.setitem(config, "PYROSCOPE_AUTH_TOKEN", "secret-token") 

45 configure_calls, _ = fake_agent 

46 _enable(feature_flags, rate=50, mode="cpu") 

47 profiling._reconcile() 

48 

49 assert len(configure_calls) == 1 

50 kw = configure_calls[0] 

51 assert kw["sample_rate"] == 50 

52 assert kw["oncpu"] is True 

53 assert kw["server_address"] == "https://pyroscope.couchershq.org" 

54 assert kw["http_headers"] == {"Authorization": "Bearer secret-token"} 

55 assert kw["tags"] == TAGS 

56 # enable_logging must never be True on a reconfigure (re-init aborts the process) 

57 assert kw["enable_logging"] is False 

58 assert (profiling._running, profiling._sample_rate, profiling._oncpu) == (True, 50, True) 

59 

60 

61def test_idempotent_when_unchanged(feature_flags: FeatureFlags, fake_agent): 

62 configure_calls, shutdown_calls = fake_agent 

63 _enable(feature_flags) 

64 profiling._reconcile() 

65 profiling._reconcile() 

66 assert len(configure_calls) == 1 

67 assert shutdown_calls == [] 

68 

69 

70def test_rate_change_restarts_agent(feature_flags: FeatureFlags, fake_agent): 

71 configure_calls, shutdown_calls = fake_agent 

72 _enable(feature_flags, rate=20) 

73 profiling._reconcile() 

74 feature_flags.set("profiling_sample_rate", 73) 

75 profiling._reconcile() 

76 assert len(configure_calls) == 2 

77 assert len(shutdown_calls) == 1 

78 assert configure_calls[-1]["sample_rate"] == 73 

79 assert profiling._sample_rate == 73 

80 

81 

82def test_mode_change_restarts_agent(feature_flags: FeatureFlags, fake_agent): 

83 configure_calls, shutdown_calls = fake_agent 

84 _enable(feature_flags, mode="wall") 

85 profiling._reconcile() 

86 feature_flags.set("profiling_mode", "cpu") 

87 profiling._reconcile() 

88 assert len(shutdown_calls) == 1 

89 assert configure_calls[-1]["oncpu"] is True 

90 assert profiling._oncpu is True 

91 

92 

93def test_disable_after_running_shuts_down(feature_flags: FeatureFlags, fake_agent): 

94 _, shutdown_calls = fake_agent 

95 _enable(feature_flags) 

96 profiling._reconcile() 

97 feature_flags.set("profiling_enabled", False) 

98 profiling._reconcile() 

99 assert len(shutdown_calls) == 1 

100 assert profiling._running is False 

101 

102 

103def test_sample_rate_is_clamped(feature_flags: FeatureFlags, fake_agent): 

104 configure_calls, _ = fake_agent 

105 _enable(feature_flags, rate=100000) 

106 profiling._reconcile() 

107 assert configure_calls[0]["sample_rate"] == 250