diff --git a/src/instana/agent/aws_eks_fargate.py b/src/instana/agent/aws_eks_fargate.py index d404c3f95..b36160157 100644 --- a/src/instana/agent/aws_eks_fargate.py +++ b/src/instana/agent/aws_eks_fargate.py @@ -66,6 +66,7 @@ def report_data_payload(self, payload): self.report_headers["Content-Type"] = "application/json" self.report_headers["X-Instana-Host"] = self.podname self.report_headers["X-Instana-Key"] = self.options.agent_key + self.report_headers["X-Instana-L"] = "0" response = self.client.post(self.__data_bundle_url(), data=to_json(payload), diff --git a/src/instana/agent/aws_fargate.py b/src/instana/agent/aws_fargate.py index c38024c42..23cb2db32 100644 --- a/src/instana/agent/aws_fargate.py +++ b/src/instana/agent/aws_fargate.py @@ -63,6 +63,7 @@ def report_data_payload(self, payload): self.report_headers["Content-Type"] = "application/json" self.report_headers["X-Instana-Host"] = self.collector.get_fq_arn() self.report_headers["X-Instana-Key"] = self.options.agent_key + self.report_headers["X-Instana-L"] = "0" response = self.client.post(self.__data_bundle_url(), data=to_json(payload), diff --git a/src/instana/agent/aws_lambda.py b/src/instana/agent/aws_lambda.py index 66145e158..4425e880d 100644 --- a/src/instana/agent/aws_lambda.py +++ b/src/instana/agent/aws_lambda.py @@ -63,6 +63,7 @@ def report_data_payload(self, payload): self.report_headers["Content-Type"] = "application/json" self.report_headers["X-Instana-Host"] = self.collector.get_fq_arn() self.report_headers["X-Instana-Key"] = self.options.agent_key + self.report_headers["X-Instana-L"] = "0" response = self.client.post(self.__data_bundle_url(), data=to_json(payload), diff --git a/src/instana/agent/google_cloud_run.py b/src/instana/agent/google_cloud_run.py index 59d6d9a09..5cfc2e76a 100644 --- a/src/instana/agent/google_cloud_run.py +++ b/src/instana/agent/google_cloud_run.py @@ -64,7 +64,8 @@ def report_data_payload(self, payload): "Content-Type": "application/json", "X-Instana-Host": "gcp:cloud-run:revision:{revision}".format( revision=self.collector.revision), - "X-Instana-Key": self.options.agent_key + "X-Instana-Key": self.options.agent_key, + "X-Instana-L": "0", } response = self.client.post(self.__data_bundle_url(), diff --git a/src/instana/agent/host.py b/src/instana/agent/host.py index 89daff8ee..eb84012a9 100644 --- a/src/instana/agent/host.py +++ b/src/instana/agent/host.py @@ -40,6 +40,10 @@ class HostAgent(BaseAgent): """ AGENT_DISCOVERY_PATH = "com.instana.plugin.python.discovery" AGENT_DATA_PATH = "com.instana.plugin.python.%d" + AGENT_REQUEST_HEADERS = { + "Content-Type": "application/json", + "X-INSTANA-L": "0", + } def __init__(self): super(HostAgent, self).__init__() @@ -154,7 +158,7 @@ def is_agent_listening(self, host, port): result = False try: url = "http://%s:%s/" % (host, port) - response = self.client.get(url, timeout=5) + response = self.client.get(url, timeout=5, headers={"X-INSTANA-L": "0"}) if 200 <= response.status_code < 300: logger.debug("Instana host agent found on %s:%d", host, port) @@ -176,7 +180,7 @@ def announce(self, discovery): url = self.__discovery_url() response = self.client.put(url, data=to_json(discovery), - headers={"Content-Type": "application/json"}, + headers=self.AGENT_REQUEST_HEADERS, timeout=0.8) except Exception as exc: logger.debug("announce: connection error (%s)", type(exc)) @@ -224,10 +228,11 @@ def log_message_to_host_agent(self, message): payload["m"] = message url = self.__agent_logger_url() + headers = self.AGENT_REQUEST_HEADERS + headers.update({"X-Log-Level": "INFO"}) response = self.client.post(url, data=to_json(payload), - headers={"Content-Type": "application/json", - "X-Log-Level": "INFO"}, + headers=headers, timeout=0.8) if 200 <= response.status_code <= 204: @@ -241,7 +246,7 @@ def is_agent_ready(self): """ ready = False try: - response = self.client.head(self.__data_url(), timeout=0.8) + response = self.client.head(self.__data_url(), timeout=0.8, headers={"X-INSTANA-L": "0"}) if response.status_code == 200: ready = True @@ -261,7 +266,7 @@ def report_data_payload(self, payload): logger.debug("Reporting %d spans", span_count) response = self.client.post(self.__traces_url(), data=to_json(payload['spans']), - headers={"Content-Type": "application/json"}, + headers=self.AGENT_REQUEST_HEADERS, timeout=0.8) if response is not None and 200 <= response.status_code <= 204: @@ -273,7 +278,7 @@ def report_data_payload(self, payload): logger.debug("Reporting %d profiles", profile_count) response = self.client.post(self.__profiles_url(), data=to_json(payload['profiles']), - headers={"Content-Type": "application/json"}, + headers=self.AGENT_REQUEST_HEADERS, timeout=0.8) if response is not None and 200 <= response.status_code <= 204: @@ -283,7 +288,7 @@ def report_data_payload(self, payload): metric_bundle = payload["metrics"]["plugins"][0]["data"] response = self.client.post(self.__data_url(), data=to_json(metric_bundle), - headers={"Content-Type": "application/json"}, + headers=self.AGENT_REQUEST_HEADERS, timeout=0.8) if response is not None and 200 <= response.status_code <= 204: @@ -377,7 +382,7 @@ def __task_response(self, message_id, data): response = self.client.post(self.__response_url(message_id), data=payload, - headers={"Content-Type": "application/json"}, + headers=self.AGENT_REQUEST_HEADERS, timeout=0.8) except Exception as exc: logger.debug("__task_response: Instana host agent connection error (%s)", type(exc)) diff --git a/tests/platforms/test_host.py b/tests/platforms/test_host.py index 2ca09804f..33d6410ef 100644 --- a/tests/platforms/test_host.py +++ b/tests/platforms/test_host.py @@ -234,6 +234,38 @@ def test_announce_fails_with_missing_uuid(self, mock_requests_session_put): self.assertEqual(len(log.records), 1) self.assertIn('response payload has no agentUuid', log.output[0]) + @patch.object(requests.Session, "put") + def test_announce_is_suppressed(self, mock_requests_session_put): + test_pid = 4242 + test_process_name = 'test_process' + test_process_args = ['-v', '-d'] + test_agent_uuid = '83bf1e09-ab16-4203-abf5-34ee0977023a' + + mock_response = MagicMock() + mock_response.status_code = 200 + mock_response.content = ( + '{' + f' "pid": {test_pid}, ' + f' "agentUuid": "{test_agent_uuid}"' + '}') + + # This mocks the call to self.agent.client.put + mock_requests_session_put.return_value = mock_response + + self.create_agent_and_setup_tracer() + d = Discovery( + pid=test_pid, + name=test_process_name, + args=test_process_args + ) + payload = self.agent.announce(d) + + self.assertIn('pid', payload) + self.assertEqual(test_pid, payload['pid']) + + spans = self.span_recorder.queued_spans() + self.assertEqual(len(spans), 0) + @patch.object(requests.Session, "get") def test_agent_connection_attempt(self, mock_requests_session_get):