Skip to content

Commit

Permalink
fix: prevent tracing internal requests.
Browse files Browse the repository at this point in the history
This commit sets the HTTP headers with the tracing level as 0 to prevent our lib's internal requests from being traced when the INSTANA_ALLOW_EXIT_AS_ROOT environment variable is set.

Signed-off-by: Paulo Vital <paulo.vital@ibm.com>
  • Loading branch information
pvital committed Aug 6, 2024
1 parent eb5f7bc commit 1e4ef52
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/instana/agent/aws_eks_fargate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions src/instana/agent/aws_fargate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions src/instana/agent/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion src/instana/agent/google_cloud_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
23 changes: 14 additions & 9 deletions src/instana/agent/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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))
Expand Down
32 changes: 32 additions & 0 deletions tests/platforms/test_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 1e4ef52

Please sign in to comment.