|
@ -1,7 +1,6 @@ |
|
|
# This class is responsible for handling all asynchronous Logz.io's |
|
|
# This class is responsible for handling all asynchronous Logz.io's |
|
|
# communication |
|
|
# communication |
|
|
import sys |
|
|
import sys |
|
|
|
|
|
|
|
|
import json |
|
|
import json |
|
|
|
|
|
|
|
|
from time import sleep |
|
|
from time import sleep |
|
@ -21,8 +20,8 @@ MAX_BULK_SIZE_IN_BYTES = 1 * 1024 * 1024 # 1 MB |
|
|
|
|
|
|
|
|
def backup_logs(logs): |
|
|
def backup_logs(logs): |
|
|
timestamp = datetime.now().strftime("%d%m%Y-%H%M%S") |
|
|
timestamp = datetime.now().strftime("%d%m%Y-%H%M%S") |
|
|
print("Backing up your logs to logzio-failures-{0}.txt".format(timestamp)) |
|
|
|
|
|
with open("logzio-failures-{0}.txt".format(timestamp), "a") as f: |
|
|
|
|
|
|
|
|
print("Backing up your logs to logzio-failures-{}.txt".format(timestamp)) |
|
|
|
|
|
with open("logzio-failures-{}.txt".format(timestamp), "a") as f: |
|
|
f.writelines('\n'.join(logs)) |
|
|
f.writelines('\n'.join(logs)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -33,7 +32,7 @@ class LogzioSender: |
|
|
logs_drain_timeout=5, |
|
|
logs_drain_timeout=5, |
|
|
debug=False): |
|
|
debug=False): |
|
|
self.token = token |
|
|
self.token = token |
|
|
self.url = "{0}/?token={1}".format(url, token) |
|
|
|
|
|
|
|
|
self.url = "{}/?token={}".format(url, token) |
|
|
self.logs_drain_timeout = logs_drain_timeout |
|
|
self.logs_drain_timeout = logs_drain_timeout |
|
|
self.debug = debug |
|
|
self.debug = debug |
|
|
|
|
|
|
|
@ -75,20 +74,22 @@ class LogzioSender: |
|
|
try: |
|
|
try: |
|
|
self._flush_queue() |
|
|
self._flush_queue() |
|
|
|
|
|
|
|
|
|
|
|
# TODO: Which exception? |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
self._debug( |
|
|
self._debug( |
|
|
"Unexpected exception while draining queue to Logz.io, " |
|
|
"Unexpected exception while draining queue to Logz.io, " |
|
|
"swallowing. Exception: " + str(e)) |
|
|
|
|
|
|
|
|
"swallowing. Exception: {}".format(e)) |
|
|
|
|
|
|
|
|
if not last_try: |
|
|
if not last_try: |
|
|
sleep(self.logs_drain_timeout) |
|
|
sleep(self.logs_drain_timeout) |
|
|
|
|
|
|
|
|
def _flush_queue(self): |
|
|
def _flush_queue(self): |
|
|
|
|
|
# TODO: Break this down. This function is crazy. |
|
|
# Sending logs until queue is empty |
|
|
# Sending logs until queue is empty |
|
|
while not self.queue.empty(): |
|
|
while not self.queue.empty(): |
|
|
logs_list = self._get_messages_up_to_max_allowed_size() |
|
|
logs_list = self._get_messages_up_to_max_allowed_size() |
|
|
self._debug("Starting to drain " + |
|
|
|
|
|
str(len(logs_list)) + " logs to Logz.io") |
|
|
|
|
|
|
|
|
self._debug( |
|
|
|
|
|
"Starting to drain {} logs to Logz.io".format(len(logs_list))) |
|
|
|
|
|
|
|
|
# Not configurable from the outside |
|
|
# Not configurable from the outside |
|
|
sleep_between_retries = 2 |
|
|
sleep_between_retries = 2 |
|
@ -106,7 +107,7 @@ class LogzioSender: |
|
|
if response.status_code == 400: |
|
|
if response.status_code == 400: |
|
|
print("Got 400 code from Logz.io. This means that " |
|
|
print("Got 400 code from Logz.io. This means that " |
|
|
"some of your logs are too big, or badly " |
|
|
"some of your logs are too big, or badly " |
|
|
"formatted. response: {0}".format( |
|
|
|
|
|
|
|
|
"formatted. response: {}".format( |
|
|
response.text)) |
|
|
response.text)) |
|
|
should_backup_to_disk = False |
|
|
should_backup_to_disk = False |
|
|
break |
|
|
break |
|
@ -127,11 +128,13 @@ class LogzioSender: |
|
|
response.text)) |
|
|
response.text)) |
|
|
should_retry = True |
|
|
should_retry = True |
|
|
else: |
|
|
else: |
|
|
self._debug("Successfully sent bulk of " + |
|
|
|
|
|
str(len(logs_list)) + " logs to Logz.io!") |
|
|
|
|
|
|
|
|
self._debug( |
|
|
|
|
|
"Successfully sent bulk of {} logs to " |
|
|
|
|
|
"Logz.io!".format(len(logs_list))) |
|
|
should_backup_to_disk = False |
|
|
should_backup_to_disk = False |
|
|
break |
|
|
break |
|
|
|
|
|
|
|
|
|
|
|
# TODO: Which exception? |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
print("Got exception while sending logs to Logz.io, " |
|
|
print("Got exception while sending logs to Logz.io, " |
|
|
"Try ({}/{}). Message: {}".format( |
|
|
"Try ({}/{}). Message: {}".format( |
|
@ -144,9 +147,9 @@ class LogzioSender: |
|
|
|
|
|
|
|
|
if should_backup_to_disk: |
|
|
if should_backup_to_disk: |
|
|
# Write to file |
|
|
# Write to file |
|
|
print("Could not send logs to Logz.io after " + |
|
|
|
|
|
str(number_of_retries) + |
|
|
|
|
|
" tries, backing up to local file system.") |
|
|
|
|
|
|
|
|
print("Could not send logs to Logz.io after {} tries, " |
|
|
|
|
|
"backing up to local file system".format( |
|
|
|
|
|
number_of_retries)) |
|
|
backup_logs(logs_list) |
|
|
backup_logs(logs_list) |
|
|
|
|
|
|
|
|
def _get_messages_up_to_max_allowed_size(self): |
|
|
def _get_messages_up_to_max_allowed_size(self): |
|
|