def test_serverless_application_harvest(audit_log_file): app = Application('Python Agent Test (Serverless Harvest Loop)') app.connect_to_data_collector(None) app.harvest() # verify audit log is not empty assert audit_log_file.tell()
def test_http_gone_stops_connect(): app = Application('Python Agent Test (agent_unittests-connect)') app.connect_to_data_collector(None) # The agent must not reattempt a connection after a ForceAgentDisconnect. # If it does, we'll end up with a session here. assert not app._active_session
def test_default_events_harvested(allowlist_event): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) settings.event_harvest_config.allowlist = frozenset((allowlist_event, )) app._stats_engine.reset_stats(settings) app._stats_engine.transaction_events.add("transaction event") app._stats_engine.error_events.add("error event") app._stats_engine.custom_events.add("custom event") app._stats_engine.span_events.add("span event") assert app._stats_engine.transaction_events.num_seen == 1 assert app._stats_engine.error_events.num_seen == 1 assert app._stats_engine.custom_events.num_seen == 1 assert app._stats_engine.span_events.num_seen == 1 assert app._stats_engine.metrics_count() == 0 app.harvest(flexible=False) num_seen = 0 if (allowlist_event != "analytic_event_data") else 1 assert app._stats_engine.transaction_events.num_seen == num_seen num_seen = 0 if (allowlist_event != "error_event_data") else 1 assert app._stats_engine.error_events.num_seen == num_seen num_seen = 0 if (allowlist_event != "custom_event_data") else 1 assert app._stats_engine.custom_events.num_seen == num_seen num_seen = 0 if (allowlist_event != "span_event_data") else 1 assert app._stats_engine.span_events.num_seen == num_seen assert app._stats_engine.metrics_count() == 1
def test_default_events_harvested(whitelist_event): app = Application('Python Agent Test (Harvest Loop)') app.connect_to_data_collector(None) settings.event_harvest_config.whitelist = frozenset((whitelist_event, )) app._stats_engine.reset_stats(settings) app._stats_engine.transaction_events.add('transaction event') app._stats_engine.error_events.add('error event') app._stats_engine.custom_events.add('custom event') app._stats_engine.span_events.add('span event') assert app._stats_engine.transaction_events.num_seen == 1 assert app._stats_engine.error_events.num_seen == 1 assert app._stats_engine.custom_events.num_seen == 1 assert app._stats_engine.span_events.num_seen == 1 assert app._stats_engine.metrics_count() == 0 app.harvest(flexible=False) num_seen = 0 if (whitelist_event != 'analytic_event_data') else 1 assert app._stats_engine.transaction_events.num_seen == num_seen num_seen = 0 if (whitelist_event != 'error_event_data') else 1 assert app._stats_engine.error_events.num_seen == num_seen num_seen = 0 if (whitelist_event != 'custom_event_data') else 1 assert app._stats_engine.custom_events.num_seen == num_seen num_seen = 0 if (whitelist_event != 'span_event_data') else 1 assert app._stats_engine.span_events.num_seen == num_seen assert app._stats_engine.metrics_count() == 1
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_reservoir_size_zeros(harvest_name, event_name): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) setattr(settings.event_harvest_config.harvest_limits, harvest_name, 0) settings.event_harvest_config.allowlist = frozenset(()) app._stats_engine.reset_stats(settings) app._stats_engine.transaction_events.add("transaction event") app._stats_engine.error_events.add("error event") app._stats_engine.custom_events.add("custom event") app._stats_engine.span_events.add("span event") assert app._stats_engine.transaction_events.num_seen == 1 assert app._stats_engine.error_events.num_seen == 1 assert app._stats_engine.custom_events.num_seen == 1 assert app._stats_engine.span_events.num_seen == 1 stat_events = set( ("transaction_events", "error_events", "custom_events", "span_events")) for stat_event in stat_events: event = getattr(app._stats_engine, stat_event) if stat_event == event_name: assert event.num_samples == 0 else: assert event.num_samples == 1 app.harvest() assert app._stats_engine.transaction_events.num_seen == 0 assert app._stats_engine.error_events.num_seen == 0 assert app._stats_engine.custom_events.num_seen == 0 assert app._stats_engine.span_events.num_seen == 0
def _test(): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) for _ in range(spans_to_send): app._stats_engine.span_stream.put(None) app.harvest()
def _test(): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) app._stats_engine.transaction_events.add("transaction event") app._stats_engine.synthetics_events.add("synthetic event") app.harvest()
def _test(): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) for _ in range(events_seen): app._stats_engine.error_events.add("error") app.harvest()
def test_serverless_application_harvest(): app = Application("Python Agent Test (Serverless Harvest Loop)") app.connect_to_data_collector(None) app.harvest() # verify audit log is not empty with open(settings.audit_log_file, "rb") as f: assert f.read()
def test_get_agent_commands_returns_none(): original_return_value = _developer_mode_responses.get('get_agent_commands') _developer_mode_responses['get_agent_commands'] = None try: app = Application('Python Agent Test (Harvest Loop)') app.connect_to_data_collector(None) app.process_agent_commands() finally: _developer_mode_responses['get_agent_commands'] = original_return_value
def test_agent_shutdown(): # Get the application connected to the actual 8T endpoint app = Application('Python Agent Test (Infinite Tracing)') app.connect_to_data_collector(None) rpc = app._active_session._rpc # Store references to the original rpc and threads assert rpc.response_processing_thread.is_alive() app.internal_agent_shutdown(restart=False) assert not rpc.response_processing_thread.is_alive() assert not rpc.channel
def test_application_harvest(audit_log_file): app = Application('Python Agent Test (Harvest Loop)') app.connect_to_data_collector(None) app.harvest() # Verify that the metric_data endpoint is the 2nd to last endpoint called # Last endpoint called is get_agent_commands assert endpoints_called[-2] == 'metric_data' # verify audit log is not empty assert audit_log_file.tell()
def _test(): app = Application('Python Agent Test (Harvest Loop)') app.connect_to_data_collector(None) if has_transaction_events: app._stats_engine.transaction_events.add('transaction event') if has_synthetic_events: app._stats_engine.synthetics_events.add('synthetic event') app.harvest()
def test_infinite_merges(): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) app._stats_engine.transaction_events.add("transaction event") assert app._stats_engine.transaction_events.num_seen == 1 app.harvest() # the agent_limits.merge_stats_maximum is not respected assert app._stats_engine.transaction_events.num_seen == 1
def _test(): # Test that if an endpoint call that occurs after we successfully send # span data fails, we do not try to send span data again with the next # harvest. app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) app._stats_engine.span_events.add("event") assert app._stats_engine.span_events.num_samples == 1 app.harvest() assert app._stats_engine.span_events.num_samples == 0
def test_application_harvest(): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) app.harvest() # Verify that the metric_data endpoint is the 2nd to last endpoint called # Last endpoint called is get_agent_commands assert endpoints_called[-2] == "metric_data" # verify audit log is not empty with open(settings.audit_log_file, "rb") as f: assert f.read()
def test_reservoir_sizes(transaction_node): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) # Record a transaction with events app.record_transaction(transaction_node) # Test that the samples have been recorded assert app._stats_engine.custom_events.num_samples == 101 assert app._stats_engine.error_events.num_samples == 101 # Add 1 for the root span assert app._stats_engine.span_events.num_samples == 102
def test_reset_synthetics_events(): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) app._stats_engine.synthetics_events.add("synthetics event") app._stats_engine.transaction_events.add("transaction event") assert app._stats_engine.synthetics_events.num_seen == 1 assert app._stats_engine.transaction_events.num_seen == 1 app.harvest() assert app._stats_engine.synthetics_events.num_seen == 0 assert app._stats_engine.transaction_events.num_seen == 1
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_get_agent_commands_returns_none(): MISSING = object() original_return_value = DeveloperModeClient.RESPONSES.get( "get_agent_commands", MISSING) DeveloperModeClient.RESPONSES["get_agent_commands"] = None try: app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) app.process_agent_commands() finally: if original_return_value is MISSING: DeveloperModeClient.RESPONSES.pop("get_agent_commands") else: DeveloperModeClient.RESPONSES[ "get_agent_commands"] = original_return_value
def _test(): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) for _ in range(spans_created): app._stats_engine.span_events.add("event") assert app._stats_engine.span_events.num_samples == (min( spans_created, max_samples_stored)) app.harvest() assert app._stats_engine.span_events.num_samples == 0 # Verify that the metric_data endpoint is the 2nd to last and # span_event_data is the 3rd to last endpoint called assert span_endpoints_called[-2] == "metric_data" if span_events_enabled and spans_created > 0: assert span_endpoints_called[-3] == "span_event_data" else: assert span_endpoints_called[-3] != "span_event_data"
def test_flexible_events_harvested(allowlist_event): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) settings.event_harvest_config.allowlist = frozenset((allowlist_event, )) app._stats_engine.reset_stats(settings) app._stats_engine.transaction_events.add("transaction event") app._stats_engine.error_events.add("error event") app._stats_engine.custom_events.add("custom event") app._stats_engine.log_events.add( LogEventNode(1653609717, "WARNING", "A", {})) app._stats_engine.span_events.add("span event") app._stats_engine.record_custom_metric("CustomMetric/Int", 1) assert app._stats_engine.transaction_events.num_seen == 1 assert app._stats_engine.error_events.num_seen == 1 assert app._stats_engine.custom_events.num_seen == 1 assert app._stats_engine.log_events.num_seen == 1 assert app._stats_engine.span_events.num_seen == 1 assert app._stats_engine.record_custom_metric("CustomMetric/Int", 1) app.harvest(flexible=True) num_seen = 0 if (allowlist_event == "analytic_event_data") else 1 assert app._stats_engine.transaction_events.num_seen == num_seen num_seen = 0 if (allowlist_event == "error_event_data") else 1 assert app._stats_engine.error_events.num_seen == num_seen num_seen = 0 if (allowlist_event == "custom_event_data") else 1 assert app._stats_engine.custom_events.num_seen == num_seen num_seen = 0 if (allowlist_event == "log_event_data") else 1 assert app._stats_engine.log_events.num_seen == num_seen num_seen = 0 if (allowlist_event == "span_event_data") else 1 assert app._stats_engine.span_events.num_seen == num_seen assert ("CustomMetric/Int", "") in app._stats_engine.stats_table assert app._stats_engine.metrics_count() > 1
def test_transaction_count(transaction_node): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) app.record_transaction(transaction_node) # Harvest has not run yet assert app._transaction_count == 1 app.harvest() # Harvest resets the transaction count assert app._transaction_count == 0 # Record a transaction app.record_transaction(transaction_node) assert app._transaction_count == 1 app.harvest() # Harvest resets the transaction count assert app._transaction_count == 0
def test_flexible_harvest_rollback(): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) settings.event_harvest_config.allowlist = frozenset( ("analytic_event_data", )) app._stats_engine.reset_stats(settings) # Cause a transaction event to attempt sending app._stats_engine.transaction_events.add( "Custom/test_flexible_harvest_rollback") # Add a metric to the stats engine app._stats_engine.record_custom_metric( "Custom/test_flexible_harvest_rollback", 1) stats_key = ("Custom/test_flexible_harvest_rollback", "") assert stats_key in app._stats_engine.stats_table app.harvest(flexible=True) # The metric should not be changed after a rollback assert app._stats_engine.stats_table[stats_key].call_count == 1
def test(): app = Application('Python Agent Test (agent_unittests-connect)') app.connect_to_data_collector(None) assert app._active_session
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
def test_get_agent_commands_raises(): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) with pytest.raises(RetryDataForRequest): app.process_agent_commands()
def _test(): app = Application("Python Agent Test (Harvest Loop)") app.connect_to_data_collector(None) app.harvest()