diff --git a/instana/collector/helpers/runtime.py b/instana/collector/helpers/runtime.py index ca8b9f0a7..b27a859bd 100644 --- a/instana/collector/helpers/runtime.py +++ b/instana/collector/helpers/runtime.py @@ -18,6 +18,7 @@ from .base import BaseHelper +PATH_OF_DEPRECATED_INSTALLATION_VIA_HOST_AGENT = '/tmp/.instana/python' class RuntimeHelper(BaseHelper): """ Helper class to collect snapshot and metrics for this Python runtime """ @@ -181,7 +182,7 @@ def _collect_runtime_snapshot(self, plugin_data): if 'AUTOWRAPT_BOOTSTRAP' in os.environ: snapshot_payload['m'] = 'Autowrapt' - elif 'INSTANA_MAGIC' in os.environ: + elif PATH_OF_DEPRECATED_INSTALLATION_VIA_HOST_AGENT in sys.path: snapshot_payload['m'] = 'AutoTrace' else: snapshot_payload['m'] = 'Manual' diff --git a/tests/platforms/test_host_collector.py b/tests/platforms/test_host_collector.py index b7822926d..1f6b19fac 100644 --- a/tests/platforms/test_host_collector.py +++ b/tests/platforms/test_host_collector.py @@ -6,17 +6,18 @@ import os import json import unittest +import sys import mock from mock import patch from instana.tracer import InstanaTracer from instana.recorder import StanRecorder from instana.agent.host import HostAgent +from instana.collector.helpers.runtime import PATH_OF_DEPRECATED_INSTALLATION_VIA_HOST_AGENT from instana.collector.host import HostCollector from instana.singletons import get_agent, set_agent, get_tracer, set_tracer from instana.version import VERSION - class TestHostCollector(unittest.TestCase): def __init__(self, methodName='runTest'): super(TestHostCollector, self).__init__(methodName) @@ -36,7 +37,8 @@ def tearDown(self): "AWS_EXECUTION_ENV", "INSTANA_EXTRA_HTTP_HEADERS", "INSTANA_ENDPOINT_URL", "INSTANA_AGENT_KEY", "INSTANA_ZONE", "INSTANA_TAGS", "INSTANA_DISABLE_METRICS_COLLECTION", - "INSTANA_DISABLE_PYTHON_PACKAGE_COLLECTION" + "INSTANA_DISABLE_PYTHON_PACKAGE_COLLECTION", + "AUTOWRAPT_BOOTSTRAP" ) for variable_name in variable_names: @@ -45,6 +47,8 @@ def tearDown(self): set_agent(self.original_agent) set_tracer(self.original_tracer) + if PATH_OF_DEPRECATED_INSTALLATION_VIA_HOST_AGENT in sys.path: + sys.path.remove(PATH_OF_DEPRECATED_INSTALLATION_VIA_HOST_AGENT) def create_agent_and_setup_tracer(self): self.agent = HostAgent() @@ -74,6 +78,8 @@ def test_prepare_payload_basics(self): self.assertEqual(python_plugin['entityId'], str(os.getpid())) self.assertIn('data', python_plugin) self.assertIn('snapshot', python_plugin['data']) + self.assertIn('m', python_plugin['data']['snapshot']) + self.assertEqual('Manual', python_plugin['data']['snapshot']['m']) self.assertIn('metrics', python_plugin['data']) # Validate that all metrics are reported on the first run @@ -153,6 +159,8 @@ def test_prepare_payload_basics_disable_runtime_metrics(self): self.assertEqual(python_plugin['entityId'], str(os.getpid())) self.assertIn('data', python_plugin) self.assertIn('snapshot', python_plugin['data']) + self.assertIn('m', python_plugin['data']['snapshot']) + self.assertEqual('Manual', python_plugin['data']['snapshot']['m']) self.assertNotIn('metrics', python_plugin['data']) @patch.object(HostCollector, "should_send_snapshot_data") @@ -165,6 +173,8 @@ def test_prepare_payload_with_snapshot_with_python_packages(self, mock_should_se self.assertIn('snapshot', payload['metrics']['plugins'][0]['data']) snapshot = payload['metrics']['plugins'][0]['data']['snapshot'] self.assertTrue(snapshot) + self.assertIn('m', snapshot) + self.assertEqual('Manual', snapshot['m']) self.assertIn('version', snapshot) self.assertGreater(len(snapshot['versions']), 5) self.assertEqual(snapshot['versions']['instana'], VERSION) @@ -184,6 +194,52 @@ def test_prepare_payload_with_snapshot_disabled_python_packages(self, mock_shoul self.assertIn('snapshot', payload['metrics']['plugins'][0]['data']) snapshot = payload['metrics']['plugins'][0]['data']['snapshot'] self.assertTrue(snapshot) + self.assertIn('m', snapshot) + self.assertEqual('Manual', snapshot['m']) self.assertIn('version', snapshot) self.assertEqual(len(snapshot['versions']), 1) self.assertEqual(snapshot['versions']['instana'], VERSION) + + + @patch.object(HostCollector, "should_send_snapshot_data") + def test_prepare_payload_with_autowrapt(self, mock_should_send_snapshot_data): + mock_should_send_snapshot_data.return_value = True + os.environ["AUTOWRAPT_BOOTSTRAP"] = "instana" + self.create_agent_and_setup_tracer() + + payload = self.agent.collector.prepare_payload() + self.assertTrue(payload) + self.assertIn('snapshot', payload['metrics']['plugins'][0]['data']) + snapshot = payload['metrics']['plugins'][0]['data']['snapshot'] + self.assertTrue(snapshot) + self.assertIn('m', snapshot) + self.assertEqual('Autowrapt', snapshot['m']) + self.assertIn('version', snapshot) + self.assertGreater(len(snapshot['versions']), 5) + expected_packages = ('instana', 'wrapt', 'fysom', 'opentracing', 'basictracer') + for package in expected_packages: + self.assertIn(package, snapshot['versions'], f"{package} not found in snapshot['versions']") + self.assertEqual(snapshot['versions']['instana'], VERSION) + + + @patch.object(HostCollector, "should_send_snapshot_data") + def test_prepare_payload_with_autotrace(self, mock_should_send_snapshot_data): + mock_should_send_snapshot_data.return_value = True + + sys.path.append(PATH_OF_DEPRECATED_INSTALLATION_VIA_HOST_AGENT) + + self.create_agent_and_setup_tracer() + + payload = self.agent.collector.prepare_payload() + self.assertTrue(payload) + self.assertIn('snapshot', payload['metrics']['plugins'][0]['data']) + snapshot = payload['metrics']['plugins'][0]['data']['snapshot'] + self.assertTrue(snapshot) + self.assertIn('m', snapshot) + self.assertEqual('AutoTrace', snapshot['m']) + self.assertIn('version', snapshot) + self.assertGreater(len(snapshot['versions']), 5) + expected_packages = ('instana', 'wrapt', 'fysom', 'opentracing', 'basictracer') + for package in expected_packages: + self.assertIn(package, snapshot['versions'], f"{package} not found in snapshot['versions']") + self.assertEqual(snapshot['versions']['instana'], VERSION)