@@ -123,19 +123,12 @@ def can_send(self):
123
123
124
124
return False
125
125
126
- def set_from (self , json_string ):
126
+ def set_from (self , res_data ):
127
127
"""
128
128
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
130
130
@return: None
131
131
"""
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
-
139
132
if "secrets" in res_data :
140
133
self .options .secrets_matcher = res_data ['secrets' ]['matcher' ]
141
134
self .options .secrets_list = res_data ['secrets' ]['list' ]
@@ -181,19 +174,43 @@ def announce(self, discovery):
181
174
"""
182
175
With the passed in Discovery class, attempt to announce to the host agent.
183
176
"""
184
- response = None
185
177
try :
186
178
url = self .__discovery_url ()
187
179
response = self .client .put (url ,
188
180
data = to_json (discovery ),
189
181
headers = {"Content-Type" : "application/json" },
190
182
timeout = 0.8 )
191
-
192
- if 200 <= response .status_code <= 204 :
193
- self .last_seen = datetime .now ()
194
183
except Exception as exc :
195
184
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
197
214
198
215
def log_message_to_host_agent (self , message ):
199
216
"""
0 commit comments