def test_adaptive_sampling(transaction_node, monkeypatch): app = Application("Python Agent Test (Harvest Loop)") # Should always return false for sampling prior to connect assert app.compute_sampled() is False app.connect_to_data_collector(None) # First harvest, first N should be sampled for _ in range(settings.sampling_target): assert app.compute_sampled() is True assert app.compute_sampled() is False # fix random.randrange to return 0 monkeypatch.setattr(random, "randrange", lambda *args, **kwargs: 0) # Multiple resets should behave the same for _ in range(2): # Set the last_reset to longer than the period so a reset will occur. app.adaptive_sampler.last_reset = time.time( ) - app.adaptive_sampler.period # Subsequent harvests should allow sampling of 2X the target for _ in range(2 * settings.sampling_target): assert app.compute_sampled() is True # No further samples should be saved assert app.compute_sampled() is False
def test_serverless_mode_adaptive_sampling(time_to_next_reset, computed_count, computed_count_last, monkeypatch): # fix random.randrange to return 0 monkeypatch.setattr(random, "randrange", lambda *args, **kwargs: 0) app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) app.adaptive_sampler.computed_count = 123 app.adaptive_sampler.last_reset = time.time() - 60 + time_to_next_reset assert app.compute_sampled() is True assert app.adaptive_sampler.computed_count == computed_count assert app.adaptive_sampler.computed_count_last == computed_count_last
def test_compute_sampled_no_reset(): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) assert app.compute_sampled() is True