def _urlopen(self, url, data=None, method=None, contenttype='application/json'): from rules.es_graphs import ESError headers = {'content-type': contenttype} if isinstance(data, str): data = data.encode('utf-8') req = urllib.request.Request(url, data, headers) if method is not None: req.get_method = lambda: method try: out = urllib.request.urlopen(req, timeout=self.TIMEOUT) except (urllib.error.URLError, urllib.error.HTTPError, socket.timeout) as e: msg = url + '\n' if isinstance(e, socket.timeout): msg += 'Request timeout' elif isinstance(e, urllib.error.HTTPError): msg += '%s %s\n%s\n\n%s\n%s' % (e.code, e.reason, e, data, e.read()) else: msg += repr(e) es_logger.exception(msg) if settings.DEBUG: raise ESError(msg, e) else: if isinstance(e, socket.timeout): msg = 'request timeout' elif isinstance(e, urllib.error.HTTPError): msg = 'error %s %s' % (e.code, e.reason) else: msg = repr(e) raise ESError('Elasticsearch %s' % msg, e) else: if settings.DEBUG: if method is None: if data: method = 'POST' else: method = 'GET' if data: data = '-- ' + data.decode('utf-8').replace('\n', '\n-- ') else: data = '-- No data' es_logger.info('%s %s\n%s' % (method, url, data)) out = out.read() out = json.loads(out.decode('utf-8')) return out
def __call__(self, *args, **kwargs): if settings.DEBUG: msg = '' body = kwargs.get('body') if 'index' in kwargs: msg = kwargs['index'] if body: msg += ':\n' if body: msg += pformat(body) if msg: es_logger.info(msg) try: return self._es(*args, **kwargs) except ElasticsearchException as e: from rules.es_graphs import ESError if isinstance(e, ConnectionTimeout): msg = 'ES connection timeout' elif isinstance(e, ConnectionError): msg = 'ES connection error: %s' % e.info elif isinstance(e, TransportError): msg = 'ES transport error: %s %s' % (e.status_code, e.error) if settings.DEBUG and getattr(e, 'info', None): msg += '\n%s' % e.info else: es_logger.error('%s %s %s' % (e.status_code, e.error, e.info)) else: msg = 'ES error' es_logger.error(format_exc() + '\n' + msg) raise ESError('ES failure: %s' % msg, e)