Skip to content

Commit e0d5bc5

Browse files
pvitalFerenc-
authored andcommitted
Fix: remove the check for the Server header of the Instana agent.
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 <[email protected]>
1 parent b8c5179 commit e0d5bc5

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

instana/agent/host.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class HostAgent(BaseAgent):
4141
"""
4242
AGENT_DISCOVERY_PATH = "com.instana.plugin.python.discovery"
4343
AGENT_DATA_PATH = "com.instana.plugin.python.%d"
44-
AGENT_HEADER = "Instana Agent"
4544

4645
def __init__(self):
4746
super(HostAgent, self).__init__()
@@ -157,15 +156,16 @@ def is_agent_listening(self, host, port):
157156
result = False
158157
try:
159158
url = "http://%s:%s/" % (host, port)
160-
response = self.client.get(url, timeout=0.8)
159+
response = self.client.get(url, timeout=5)
161160

162-
server_header = response.headers["Server"]
163-
if server_header == self.AGENT_HEADER:
161+
if 200 <= response.status_code < 300:
164162
logger.debug("Instana host agent found on %s:%d", host, port)
165163
result = True
166164
else:
167-
logger.debug("...something is listening on %s:%d but it's not the Instana Host Agent: %s",
168-
host, port, server_header)
165+
logger.debug("The attempt to connect to the Instana host "\
166+
"agent on %s:%d has failed with an unexpected " \
167+
"status code. Expected HTTP 200 but received: %d",
168+
host, port, response.status_code)
169169
except Exception:
170170
logger.debug("Instana Host Agent not found on %s:%d", host, port)
171171
return result

tests/platforms/test_host.py

+38
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,41 @@ def test_announce_fails_with_missing_uuid(self, mock_requests_session_put):
236236
self.assertEqual(len(log.output), 1)
237237
self.assertEqual(len(log.records), 1)
238238
self.assertIn('response payload has no agentUuid', log.output[0])
239+
240+
241+
@patch.object(requests.Session, "get")
242+
def test_agent_connection_attempt(self, mock_requests_session_get):
243+
mock_response = MagicMock()
244+
mock_response.status_code = 200
245+
mock_requests_session_get.return_value = mock_response
246+
247+
self.create_agent_and_setup_tracer()
248+
host = self.agent.options.agent_host
249+
port = self.agent.options.agent_port
250+
msg = f"Instana host agent found on {host}:{port}"
251+
252+
with self.assertLogs(logger, level='DEBUG') as log:
253+
result = self.agent.is_agent_listening(host, port)
254+
255+
self.assertTrue(result)
256+
self.assertIn(msg, log.output[0])
257+
258+
259+
@patch.object(requests.Session, "get")
260+
def test_agent_connection_attempt_fails_with_404(self, mock_requests_session_get):
261+
mock_response = MagicMock()
262+
mock_response.status_code = 404
263+
mock_requests_session_get.return_value = mock_response
264+
265+
self.create_agent_and_setup_tracer()
266+
host = self.agent.options.agent_host
267+
port = self.agent.options.agent_port
268+
msg = "The attempt to connect to the Instana host agent on " \
269+
f"{host}:{port} has failed with an unexpected status code. " \
270+
f"Expected HTTP 200 but received: {mock_response.status_code}"
271+
272+
with self.assertLogs(logger, level='DEBUG') as log:
273+
result = self.agent.is_agent_listening(host, port)
274+
275+
self.assertFalse(result)
276+
self.assertIn(msg, log.output[0])

0 commit comments

Comments
 (0)