def get_config(self, current_version=None, keys=None): """ Gets configuration from a remote APM Server :param current_version: version of the current configuration :param keys: a JSON-serializable dict to identify this instance, e.g. { "service": { "name": "foo", "environment": "bar" } } :return: a three-tuple of new version, config dictionary and validity in seconds. Any element of the tuple can be None. """ url = self._config_url data = json_encoder.dumps(keys).encode("utf-8") headers = self._headers.copy() headers.update(self.auth_headers) max_age = 300 if current_version: headers["If-None-Match"] = current_version try: response = self.http.urlopen("POST", url, body=data, headers=headers, timeout=self._timeout, preload_content=False) except (urllib3.exceptions.RequestError, urllib3.exceptions.HTTPError) as e: logger.debug("HTTP error while fetching remote config: %s", compat.text_type(e)) return current_version, None, max_age body = response.read() if "Cache-Control" in response.headers: try: max_age = int( next( re.finditer( r"max-age=(\d+)", response.headers["Cache-Control"])).groups()[0]) except StopIteration: logger.debug("Could not parse Cache-Control header: %s", response.headers["Cache-Control"]) if response.status == 304: # config is unchanged, return logger.debug("Configuration unchanged") return current_version, None, max_age elif response.status >= 400: return None, None, max_age if not body: logger.debug( "APM Server answered with empty body and status code %s", response.status) return current_version, None, max_age return response.headers.get("Etag"), json_encoder.loads( body.decode("utf-8")), max_age
def fetch_server_info(self): headers = self._headers.copy() if self._headers else {} headers.update(self.auth_headers) headers["accept"] = "text/plain" try: response = self.http.urlopen("GET", self._server_info_url, headers=headers, timeout=self._timeout) body = response.data data = json_encoder.loads(body.decode("utf8")) version = data["version"] logger.info("Fetched APM Server version %s", version) self.client.server_version = version_string_to_tuple(version) except (urllib3.exceptions.RequestError, urllib3.exceptions.HTTPError) as e: logger.warning("HTTP error while fetching server information: %s", str(e)) except json.JSONDecodeError as e: logger.warning("JSON decoding error while fetching server information: %s", str(e)) except (KeyError, TypeError): logger.warning("No version key found in server response: %s", response.data)
def get_config(self, current_version=None, keys=None): url = self._config_url data = json_encoder.dumps(keys).encode("utf-8") headers = self._headers.copy() max_age = 300 if current_version: headers["If-None-Match"] = current_version try: response = self.http.urlopen("POST", url, body=data, headers=headers, timeout=self._timeout, preload_content=False) except (urllib3.exceptions.RequestError, urllib3.exceptions.HTTPError) as e: logger.debug("HTTP error while fetching remote config: %s", compat.text_type(e)) return current_version, None, max_age body = response.read() if "Cache-Control" in response.headers: try: max_age = int( next( re.finditer( r"max-age=(\d+)", response.headers["Cache-Control"])).groups()[0]) except StopIteration: logger.debug("Could not parse Cache-Control header: %s", response["Cache-Control"]) if response.status == 304: # config is unchanged, return logger.debug("Configuration unchanged") return current_version, None, max_age elif response.status >= 400: return None, None, max_age return response.headers.get("Etag"), json_encoder.loads( body.decode("utf-8")), max_age
def decode(self, data): """ Unserializes a string, ``data``. """ return json.loads(zlib.decompress(data).decode('utf8'))