From 2bfd747cc3fb902f3758673b0ef450427a29a710 Mon Sep 17 00:00:00 2001 From: Quentin Madec Date: Tue, 17 Mar 2015 14:45:48 -0400 Subject: [PATCH] [flare] use new endpoints Create a function in config to get new endpoints, and use it also in the forwarder --- config.py | 21 +++++++++++++++++++++ ddagent.py | 30 +++++------------------------- utils/flare.py | 24 +++++++++++++++--------- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/config.py b/config.py index 91a5e1a4e5..a0e6725290 100644 --- a/config.py +++ b/config.py @@ -17,6 +17,7 @@ from socket import gaierror from optparse import OptionParser, Values from cStringIO import StringIO +from urlparse import urlparse # project @@ -57,6 +58,11 @@ 'nagios_perf_cfg' ] +LEGACY_DATADOG_URLS = [ + "app.datadoghq.com", + "app.datad0g.com", +] + class PathNotFound(Exception): pass @@ -92,6 +98,21 @@ def get_parsed_args(): def get_version(): return AGENT_VERSION + +# Return url endpoint, here because needs access to version number +def get_url_endpoint(default_url, endpoint_type='app'): + parsed_url = urlparse(default_url) + if parsed_url.netloc not in LEGACY_DATADOG_URLS: + return default_url + + subdomain = parsed_url.netloc.split(".")[0] + + # Replace https://app.datadoghq.com in https://5-2-0-app.agent.datadoghq.com + return default_url.replace(subdomain, + "{0}-{1}.agent".format( + get_version().replace(".", "-"), + endpoint_type)) + def skip_leading_wsp(f): "Works on a file, returns a file-like object" return StringIO("\n".join(map(string.strip, f.readlines()))) diff --git a/ddagent.py b/ddagent.py index 10d8f84bc3..c4d3d137db 100755 --- a/ddagent.py +++ b/ddagent.py @@ -27,7 +27,6 @@ from hashlib import md5 from datetime import datetime, timedelta from socket import gaierror, error as socket_error -from urlparse import urlparse # Tornado import tornado.httpserver @@ -39,7 +38,7 @@ # agent import from util import Watchdog, get_uuid, get_hostname, json, get_tornado_ioloop from emitter import http_emitter -from config import get_config, get_version +from config import get_config, get_url_endpoint, get_version from checks.check_status import ForwarderStatus from transaction import Transaction, TransactionManager import modules @@ -72,11 +71,6 @@ THROTTLING_DELAY = timedelta(microseconds=1000000/2) # 2 msg/second -LEGACY_DATADOG_URLS = [ - "app.datadoghq.com", - "app.datad0g.com", -] - class EmitterThread(threading.Thread): def __init__(self, *args, **kwargs): @@ -192,23 +186,9 @@ def __init__(self, data, headers): def __sizeof__(self): return sys.getsizeof(self._data) - @classmethod - def get_url_endpoint(cls, endpoint): - default_url = cls._application._agentConfig[endpoint] - parsed_url = urlparse(default_url) - if parsed_url.netloc not in LEGACY_DATADOG_URLS: - return default_url - - subdomain = parsed_url.netloc.split(".")[0] - - # Replace https://app.datadoghq.com in https://5-2-0-app.agent.datadoghq.com - return default_url.replace(subdomain, - "{0}-{1}.agent".format( - get_version().replace(".", "-"), - subdomain)) def get_url(self, endpoint): - endpoint_base_url = self.get_url_endpoint(endpoint) + endpoint_base_url = get_url_endpoint(self._application._agentConfig[endpoint]) api_key = self._application._agentConfig.get('api_key') if api_key: return endpoint_base_url + '/intake?api_key=%s' % api_key @@ -252,14 +232,14 @@ def flush(self): if self._application._agentConfig.get('proxy_forbid_method_switch'): # See http://stackoverflow.com/questions/8156073/curl-violate-rfc-2616-10-3-2-and-switch-from-post-to-get tornado_client_params['prepare_curl_callback'] = lambda curl: curl.setopt(pycurl.POSTREDIR, pycurl.REDIR_POST_ALL) - + if (not self._application.use_simple_http_client or force_use_curl) and pycurl is not None: ssl_certificate = self._application._agentConfig.get('ssl_certificate', None) tornado_client_params['ca_certs'] = ssl_certificate req = tornado.httpclient.HTTPRequest(**tornado_client_params) use_curl = force_use_curl or self._application._agentConfig.get("use_curl_http_client") and not self._application.use_simple_http_client - + if use_curl: if pycurl is None: log.error("dd-agent is configured to use the Curl HTTP Client, but pycurl is not available on this system.") @@ -284,7 +264,7 @@ def on_response(self, response): class APIMetricTransaction(MetricTransaction): def get_url(self, endpoint): - endpoint_base_url = self.get_url_endpoint(endpoint) + endpoint_base_url = get_url_endpoint(self._application._agentConfig[endpoint]) config = self._application._agentConfig api_key = config['api_key'] url = endpoint_base_url + '/api/v1/series/?api_key=' + api_key diff --git a/utils/flare.py b/utils/flare.py index 6b1795d7c9..a8dab2e302 100644 --- a/utils/flare.py +++ b/utils/flare.py @@ -19,6 +19,7 @@ get_config_path, get_logging_config, get_os, + get_url_endpoint, ) from util import ( get_hostname, @@ -28,7 +29,6 @@ # 3p import requests - # Globals log = logging.getLogger('flare') @@ -76,7 +76,10 @@ def __init__(self, cmdline=False, case_id=None): self._save_logs_path() config = get_config() self._api_key = config.get('api_key') - self._url = "{0}{1}".format(config.get('dd_url'), self.DATADOG_SUPPORT_URL) + self._url = "{0}{1}".format( + get_url_endpoint(config.get('dd_url'), endpoint_type='flare'), + self.DATADOG_SUPPORT_URL + ) self._hostname = get_hostname(config) self._prefix = "datadog-{0}".format(self._hostname) @@ -296,18 +299,21 @@ def _ask_for_confirmation(self): # Ask for email if needed def _ask_for_email(self): if self._case_id: - return None + return '' return raw_input('Please enter your email: ').lower() # Print output (success/error) of the request def _analyse_result(self, resp): - if resp.status_code == 200: + if resp.status_code in range(200, 203): log.info("Your logs were successfully uploaded. For future reference,"\ - " your internal case id is {0}".format(json.loads(resp.text)['case_id'])) - elif resp.status_code == 400: - raise Exception('Your request is incorrect: {0}'.format(resp.text)) - elif resp.status_code == 500: - raise Exception('An error has occurred while uploading: {0}'.format(resp).text) + " your internal case id is {0}".format( + json.loads(resp.text)['case_id'])) + elif resp.status_code in range(400, 405): + raise Exception('Your request is incorrect: {0}'.format( + json.loads(resp.text)['error'])) + elif resp.status_code in range(500, 506): + raise Exception('An error has occurred while uploading: {0}'.format( + json.loads(resp.text)['error'])) else: raise Exception('An unknown error has occured: {0}\n'\ 'Please contact support by email'.format(resp.text))