From ddb5908bd79a7c1fe0f290885540278aadb4a2ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20G=C3=A9czi?= Date: Fri, 20 Oct 2023 13:00:00 +0000 Subject: [PATCH 1/5] fix(collector): Update detection of AutoTrace (injector) to current method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ferenc Géczi --- instana/collector/helpers/runtime.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instana/collector/helpers/runtime.py b/instana/collector/helpers/runtime.py index ca8b9f0a7..d5c2254cd 100644 --- a/instana/collector/helpers/runtime.py +++ b/instana/collector/helpers/runtime.py @@ -181,7 +181,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 '/tmp/.instana/python' in sys.path: snapshot_payload['m'] = 'AutoTrace' else: snapshot_payload['m'] = 'Manual' From 9a6c8ac2e3a8e23ee7dd267220573e3d17c1021e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20G=C3=A9czi?= Date: Fri, 20 Oct 2023 13:00:00 +0000 Subject: [PATCH 2/5] test: Check instrumentation method in host collector tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ferenc Géczi --- tests/platforms/test_host_collector.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/platforms/test_host_collector.py b/tests/platforms/test_host_collector.py index b7822926d..e8b998b39 100644 --- a/tests/platforms/test_host_collector.py +++ b/tests/platforms/test_host_collector.py @@ -74,6 +74,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 +155,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 +169,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 +190,8 @@ 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) From a554924856fe9edcaa77cc1ca6345d6bf32089c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20G=C3=A9czi?= Date: Fri, 20 Oct 2023 13:00:00 +0000 Subject: [PATCH 3/5] test: Cover AutoTrace and Autowrap instrumentation methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ferenc Géczi --- tests/platforms/test_host_collector.py | 52 +++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/tests/platforms/test_host_collector.py b/tests/platforms/test_host_collector.py index e8b998b39..d58c32c10 100644 --- a/tests/platforms/test_host_collector.py +++ b/tests/platforms/test_host_collector.py @@ -6,6 +6,7 @@ import os import json import unittest +import sys import mock from mock import patch @@ -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 '/tmp/.instana/python' in sys.path: + sys.path.remove('/tmp/.instana/python') def create_agent_and_setup_tracer(self): self.agent = HostAgent() @@ -195,3 +199,49 @@ def test_prepare_payload_with_snapshot_disabled_python_packages(self, mock_shoul 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) + self.assertEqual(snapshot['versions']['instana'], VERSION) + self.assertIn('wrapt', snapshot['versions']) + self.assertIn('fysom', snapshot['versions']) + self.assertIn('opentracing', snapshot['versions']) + self.assertIn('basictracer', snapshot['versions']) + + + @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('/tmp/.instana/python') + + 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) + self.assertEqual(snapshot['versions']['instana'], VERSION) + self.assertIn('wrapt', snapshot['versions']) + self.assertIn('fysom', snapshot['versions']) + self.assertIn('opentracing', snapshot['versions']) + self.assertIn('basictracer', snapshot['versions']) From ea20ac7b978b74671ff5c45a3f4aafeb9c4f7d19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20G=C3=A9czi?= Date: Mon, 23 Oct 2023 13:00:00 +0000 Subject: [PATCH 4/5] refactor(test): Assert expected snapshot packages in a loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ferenc Géczi --- tests/platforms/test_host_collector.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/platforms/test_host_collector.py b/tests/platforms/test_host_collector.py index d58c32c10..feff844b7 100644 --- a/tests/platforms/test_host_collector.py +++ b/tests/platforms/test_host_collector.py @@ -216,11 +216,10 @@ def test_prepare_payload_with_autowrapt(self, mock_should_send_snapshot_data): 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) - self.assertIn('wrapt', snapshot['versions']) - self.assertIn('fysom', snapshot['versions']) - self.assertIn('opentracing', snapshot['versions']) - self.assertIn('basictracer', snapshot['versions']) @patch.object(HostCollector, "should_send_snapshot_data") @@ -240,8 +239,7 @@ def test_prepare_payload_with_autotrace(self, mock_should_send_snapshot_data): 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) - self.assertIn('wrapt', snapshot['versions']) - self.assertIn('fysom', snapshot['versions']) - self.assertIn('opentracing', snapshot['versions']) - self.assertIn('basictracer', snapshot['versions']) From a0d443420f09d113ad84718e96edde90af039470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20G=C3=A9czi?= Date: Wed, 25 Oct 2023 13:00:00 +0000 Subject: [PATCH 5/5] refactor(runtime): Use a constant for the deprecated installation path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ferenc Géczi --- instana/collector/helpers/runtime.py | 3 ++- tests/platforms/test_host_collector.py | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/instana/collector/helpers/runtime.py b/instana/collector/helpers/runtime.py index d5c2254cd..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 '/tmp/.instana/python' in sys.path: + 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 feff844b7..1f6b19fac 100644 --- a/tests/platforms/test_host_collector.py +++ b/tests/platforms/test_host_collector.py @@ -13,11 +13,11 @@ 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) @@ -47,8 +47,8 @@ def tearDown(self): set_agent(self.original_agent) set_tracer(self.original_tracer) - if '/tmp/.instana/python' in sys.path: - sys.path.remove('/tmp/.instana/python') + 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() @@ -226,7 +226,7 @@ def test_prepare_payload_with_autowrapt(self, mock_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('/tmp/.instana/python') + sys.path.append(PATH_OF_DEPRECATED_INSTALLATION_VIA_HOST_AGENT) self.create_agent_and_setup_tracer()