Skip to content

Commit 8fa1f28

Browse files
authored
Better container pid detection on Linux (#93)
* Better container pid detection on Linux * Better regexp & safety * wicked betta regexp
1 parent dce7f4c commit 8fa1f28

File tree

1 file changed

+57
-20
lines changed

1 file changed

+57
-20
lines changed

instana/fsm.py

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22

33
import os
4+
import re
45
import socket
56
import subprocess
67
import sys
@@ -74,25 +75,7 @@ def start_metric_reporting(self, e):
7475
self.agent.sensor.meter.run()
7576

7677
def lookup_agent_host(self, e):
77-
host = a.AGENT_DEFAULT_HOST
78-
port = a.AGENT_DEFAULT_PORT
79-
80-
if "INSTANA_AGENT_HOST" in os.environ:
81-
host = os.environ["INSTANA_AGENT_HOST"]
82-
if "INSTANA_AGENT_PORT" in os.environ:
83-
port = int(os.environ["INSTANA_AGENT_PORT"])
84-
85-
elif "INSTANA_AGENT_IP" in os.environ:
86-
# Deprecated: INSTANA_AGENT_IP environment variable
87-
# To be removed in a future version
88-
host = os.environ["INSTANA_AGENT_IP"]
89-
if "INSTANA_AGENT_PORT" in os.environ:
90-
port = int(os.environ["INSTANA_AGENT_PORT"])
91-
92-
elif self.agent.sensor.options.agent_host != "":
93-
host = self.agent.sensor.options.agent_host
94-
if self.agent.sensor.options.agent_port != 0:
95-
port = self.agent.sensor.options.agent_port
78+
host, port = self.__get_agent_host_port()
9679

9780
h = self.check_host(host, port)
9881
if h == a.AGENT_HEADER:
@@ -165,7 +148,7 @@ def announce_sensor(self, e):
165148
cmdline = sys.argv
166149
log.debug(err)
167150

168-
d = Discovery(pid=pid,
151+
d = Discovery(pid=self.__get_real_pid(),
169152
name=cmdline[0],
170153
args=cmdline[1:])
171154

@@ -206,3 +189,57 @@ def test_agent(self, e):
206189
self.schedule_retry(self.test_agent, e, "agent test")
207190
else:
208191
self.fsm.test()
192+
193+
def __get_real_pid(self):
194+
"""
195+
Attempts to determine the true process ID by querying the
196+
/proc/<pid>/sched file. This works on systems with a proc filesystem.
197+
Otherwise default to os default.
198+
"""
199+
pid = None
200+
201+
if os.path.exists("/proc/"):
202+
sched_file = "/proc/%d/sched" % os.getpid()
203+
204+
if os.path.isfile(sched_file):
205+
try:
206+
file = open(sched_file)
207+
line = file.readline()
208+
g = re.search(r'\((\d+),', line)
209+
if len(g.groups()) == 1:
210+
pid = int(g.groups()[0])
211+
except Exception:
212+
log.debug("parsing sched file failed", exc_info=True)
213+
pass
214+
215+
if pid is None:
216+
pid = os.getpid()
217+
218+
return pid
219+
220+
def __get_agent_host_port(self):
221+
"""
222+
Iterates the the various ways the host and port of the Instana host
223+
agent may be configured: default, env vars, sensor options...
224+
"""
225+
host = a.AGENT_DEFAULT_HOST
226+
port = a.AGENT_DEFAULT_PORT
227+
228+
if "INSTANA_AGENT_HOST" in os.environ:
229+
host = os.environ["INSTANA_AGENT_HOST"]
230+
if "INSTANA_AGENT_PORT" in os.environ:
231+
port = int(os.environ["INSTANA_AGENT_PORT"])
232+
233+
elif "INSTANA_AGENT_IP" in os.environ:
234+
# Deprecated: INSTANA_AGENT_IP environment variable
235+
# To be removed in a future version
236+
host = os.environ["INSTANA_AGENT_IP"]
237+
if "INSTANA_AGENT_PORT" in os.environ:
238+
port = int(os.environ["INSTANA_AGENT_PORT"])
239+
240+
elif self.agent.sensor.options.agent_host != "":
241+
host = self.agent.sensor.options.agent_host
242+
if self.agent.sensor.options.agent_port != 0:
243+
port = self.agent.sensor.options.agent_port
244+
245+
return host, port

0 commit comments

Comments
 (0)