Skip to content

Commit

Permalink
Fix: remove the check for the Server header of the Instana agent.
Browse files Browse the repository at this point in the history
The check for the Server header of the Instana agent can break the announce
procedure in service meshes when there is a proxy between the tracer and the
Instana agent. This happens because some proxies do not forward the original
Server header. So instead, the collector checks whether the HTTP status code
is in the 2xx range.

Signed-off-by: Paulo Vital <paulo.vital@ibm.com>
  • Loading branch information
pvital committed May 22, 2023
1 parent b8c5179 commit 17d576d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
12 changes: 6 additions & 6 deletions instana/agent/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/platforms/test_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])

0 comments on commit 17d576d

Please sign in to comment.