Skip to content

Commit e54aabf

Browse files
committed
fix(fsm/agent): Handle faulty announce responses
Signed-off-by: Ferenc Géczi <[email protected]>
1 parent 6743aa3 commit e54aabf

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

instana/agent/host.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,12 @@ def can_send(self):
123123

124124
return False
125125

126-
def set_from(self, json_string):
126+
def set_from(self, res_data):
127127
"""
128128
Sets the source identifiers given to use by the Instana Host agent.
129-
@param json_string: source identifiers
129+
@param res_data: source identifiers provided as announce response
130130
@return: None
131131
"""
132-
if isinstance(json_string, bytes):
133-
raw_json = json_string.decode("UTF-8")
134-
else:
135-
raw_json = json_string
136-
137-
res_data = json.loads(raw_json)
138-
139132
if "secrets" in res_data:
140133
self.options.secrets_matcher = res_data['secrets']['matcher']
141134
self.options.secrets_list = res_data['secrets']['list']
@@ -181,19 +174,43 @@ def announce(self, discovery):
181174
"""
182175
With the passed in Discovery class, attempt to announce to the host agent.
183176
"""
184-
response = None
185177
try:
186178
url = self.__discovery_url()
187179
response = self.client.put(url,
188180
data=to_json(discovery),
189181
headers={"Content-Type": "application/json"},
190182
timeout=0.8)
191-
192-
if 200 <= response.status_code <= 204:
193-
self.last_seen = datetime.now()
194183
except Exception as exc:
195184
logger.debug("announce: connection error (%s)", type(exc))
196-
return response
185+
return None
186+
187+
if 200 <= response.status_code <= 204:
188+
self.last_seen = datetime.now()
189+
190+
if response.status_code != 200:
191+
logger.debug("announce: response status code (%s) is NOT 200", response.status_code)
192+
return None
193+
194+
if isinstance(response.content, bytes):
195+
raw_json = response.content.decode("UTF-8")
196+
else:
197+
raw_json = response.content
198+
199+
try:
200+
payload = json.loads(raw_json)
201+
except json.JSONDecodeError as e:
202+
logger.debug("announce: response is not JSON: (%s)", raw_json)
203+
return None
204+
205+
if not payload.get('pid'):
206+
logger.debug("announce: response payload has no pid: (%s)", payload)
207+
return None
208+
209+
if not payload.get('agentUuid'):
210+
logger.debug("announce: response payload has no agentUuid: (%s)", payload)
211+
return None
212+
213+
return payload
197214

198215
def log_message_to_host_agent(self, message):
199216
"""

instana/fsm.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,18 @@ def announce_sensor(self, e):
157157
except:
158158
logger.debug("Error generating file descriptor: ", exc_info=True)
159159

160-
response = self.agent.announce(d)
161-
162-
if response and (response.status_code == 200) and (len(response.content) > 2):
163-
self.agent.set_from(response.content)
164-
self.fsm.pending()
165-
logger.debug("Announced pid: %s (true pid: %s). Waiting for Agent Ready...",
166-
str(pid), str(self.agent.announce_data.pid))
167-
return True
168-
169-
logger.debug("Cannot announce sensor. Scheduling retry.")
170-
self.schedule_retry(self.announce_sensor, e, self.THREAD_NAME + ": announce")
171-
return False
160+
payload = self.agent.announce(d)
161+
162+
if not payload:
163+
logger.debug("Cannot announce sensor. Scheduling retry.")
164+
self.schedule_retry(self.announce_sensor, e, self.THREAD_NAME + ": announce")
165+
return False
166+
167+
self.agent.set_from(payload)
168+
self.fsm.pending()
169+
logger.debug("Announced pid: %s (true pid: %s). Waiting for Agent Ready...",
170+
str(pid), str(self.agent.announce_data.pid))
171+
return True
172172

173173
def schedule_retry(self, fun, e, name):
174174
self.timer = threading.Timer(self.RETRY_PERIOD, fun, [e])
@@ -214,4 +214,4 @@ def __get_real_pid(self):
214214
if pid is None:
215215
pid = os.getpid()
216216

217-
return pid
217+
return pid

0 commit comments

Comments
 (0)