diff --git a/instana/agent/host.py b/instana/agent/host.py index 215c55e9b..0dbbf4c00 100644 --- a/instana/agent/host.py +++ b/instana/agent/host.py @@ -41,7 +41,6 @@ class HostAgent(BaseAgent): """ AGENT_DISCOVERY_PATH = "com.instana.plugin.python.discovery" AGENT_DATA_PATH = "com.instana.plugin.python.%d" - AGENT_HEADER = "Instana Agent" def __init__(self): super(HostAgent, self).__init__() @@ -157,15 +156,16 @@ def is_agent_listening(self, host, port): result = False try: url = "http://%s:%s/" % (host, port) - response = self.client.get(url, timeout=0.8) + response = self.client.get(url, timeout=5) - server_header = response.headers["Server"] - if server_header == self.AGENT_HEADER: + if 200 <= response.status_code < 300: logger.debug("Instana host agent found on %s:%d", host, port) result = True else: - logger.debug("...something is listening on %s:%d but it's not the Instana Host Agent: %s", - host, port, server_header) + logger.debug("The attempt to connect to the Instana host "\ + "agent on %s:%d has failed with an unexpected " \ + "status code. Expected HTTP 200 but received: %d", + host, port, response.status_code) except Exception: logger.debug("Instana Host Agent not found on %s:%d", host, port) return result diff --git a/tests/platforms/test_host.py b/tests/platforms/test_host.py index f866f25eb..c82167ff9 100644 --- a/tests/platforms/test_host.py +++ b/tests/platforms/test_host.py @@ -10,6 +10,7 @@ from mock import MagicMock, patch import requests +import requests_mock from instana.agent.host import HostAgent from instana.fsm import Discovery @@ -236,3 +237,22 @@ def test_announce_fails_with_missing_uuid(self, mock_requests_session_put): self.assertEqual(len(log.output), 1) self.assertEqual(len(log.records), 1) self.assertIn('response payload has no agentUuid', log.output[0]) + + @patch.object(requests.Session, "get") + def test_agent_instana_is_agent_listening(self, mock_requests_session_get): + mock_response = MagicMock() + mock_response.status_code = 404 + mock_requests_session_get.return_value = mock_response + + self.create_agent_and_setup_tracer() + host = self.agent.options.agent_host + port = self.agent.options.agent_port + msg = "The attempt to connect to the Instana host agent on " \ + f"{host}:{port} has failed with an unexpected status code. " \ + f"Expected HTTP 200 but received: {mock_response.status_code}" + + with self.assertLogs(logger, level='DEBUG') as log: + result = self.agent.is_agent_listening(host, port) + + self.assertFalse(result) + self.assertIn(msg, log.output[0])