Skip to content

Commit

Permalink
Merge pull request #2089 from fishtown-analytics/feature/batched-trac…
Browse files Browse the repository at this point in the history
…king

batch anonymous usage statistics calls
  • Loading branch information
beckjake authored Feb 4, 2020
2 parents 5b65591 + 1881c0b commit ccab27a
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions core/dbt/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@

class TimeoutEmitter(Emitter):
def __init__(self):
super().__init__(COLLECTOR_URL, protocol=COLLECTOR_PROTOCOL,
buffer_size=1, on_failure=self.handle_failure)
super().__init__(
COLLECTOR_URL, protocol=COLLECTOR_PROTOCOL,
buffer_size=30, on_failure=self.handle_failure,
method='post',
# don't set this.
byte_limit=None,
)

@staticmethod
def handle_failure(num_ok, unsent):
Expand All @@ -41,21 +46,43 @@ def handle_failure(num_ok, unsent):
logger.warning('Error sending message, disabling tracking')
do_not_track()

def http_get(self, payload):
sp_logger.info("Sending GET request to {}...".format(self.endpoint))
sp_logger.debug("Payload: {}".format(payload))
r = requests.get(self.endpoint, params=payload, timeout=5.0)
def _log_request(self, request, payload):
sp_logger.info(f"Sending {request} request to {self.endpoint}...")
sp_logger.debug(f"Payload: {payload}")

msg = "GET request finished with status code: " + str(r.status_code)
if self.is_good_status_code(r.status_code):
def _log_result(self, request, status_code):
msg = f"{request} request finished with status code: {status_code}"
if self.is_good_status_code(status_code):
sp_logger.info(msg)
else:
sp_logger.warning(msg)

def http_post(self, payload):
self._log_request('POST', payload)

r = requests.post(
self.endpoint,
data=payload,
headers={'content-type': 'application/json; charset=utf-8'},
timeout=5.0
)

self._log_result('GET', r.status_code)
return r

def http_get(self, payload):
self._log_request('GET', payload)

r = requests.get(self.endpoint, params=payload, timeout=5.0)

self._log_result('GET', r.status_code)
return r


emitter = TimeoutEmitter()
tracker = Tracker(emitter, namespace="cf", app_id="dbt")
tracker = Tracker(
emitter, namespace="cf", app_id="dbt",
)


class User:
Expand Down

0 comments on commit ccab27a

Please sign in to comment.