Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autotrace detection #462

Merged
merged 5 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion instana/collector/helpers/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down Expand Up @@ -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:
Ferenc- marked this conversation as resolved.
Show resolved Hide resolved
elif PATH_OF_DEPRECATED_INSTALLATION_VIA_HOST_AGENT in sys.path:
snapshot_payload['m'] = 'AutoTrace'
else:
snapshot_payload['m'] = 'Manual'
Expand Down
60 changes: 58 additions & 2 deletions tests/platforms/test_host_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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)
Ferenc- marked this conversation as resolved.
Show resolved Hide resolved
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)
Ferenc- marked this conversation as resolved.
Show resolved Hide resolved
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)
Loading