@@ -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 """
0 commit comments