Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save if a error occurs during sending of the request #22

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions loki_logger_handler/loki_logger_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
except ImportError:
import Queue as queue # Python 2.7

import atexit
import logging
import threading
import time
import logging
import atexit

import requests

from loki_logger_handler.formatters.logger_formatter import LoggerFormatter
from loki_logger_handler.streams import Streams
from loki_logger_handler.loki_request import LokiRequest
from loki_logger_handler.stream import Stream
from loki_logger_handler.streams import Streams


class LokiLoggerHandler(logging.Handler):
Expand All @@ -40,7 +43,7 @@ def __init__(
message_in_json_format=True,
timeout=10,
compressed=True,
default_formatter=LoggerFormatter()
default_formatter=LoggerFormatter(),
fighterii marked this conversation as resolved.
Show resolved Hide resolved
):
"""
Initialize the LokiLoggerHandler object.
Expand All @@ -62,16 +65,18 @@ def __init__(
self.label_keys = label_keys if label_keys is not None else {}
self.timeout = timeout
self.formatter = default_formatter
self.request = LokiRequest(url=url, compressed=compressed, additional_headers=additional_headers or {})
self.request = LokiRequest(
url=url, compressed=compressed, additional_headers=additional_headers or {}
)
self.buffer = queue.Queue()
self.flush_thread = threading.Thread(target=self._flush)

# Set daemon for Python 2 and 3 compatibility
self.flush_thread.daemon = True
self.flush_thread.start()

self.message_in_json_format = message_in_json_format

self.send_error = None

def emit(self, record):
Expand Down Expand Up @@ -119,11 +124,10 @@ def _send(self):

if temp_streams:
streams = Streams(list(temp_streams.values()))
request_error = self.request.send(streams.serialize())
if request_error:
self.send_error = request_error
else:
self.send_error = None
try:
self.request.send(streams.serialize())
except requests.RequestException as e:
self.send_error = e

def write(self, message):
"""
Expand Down
5 changes: 2 additions & 3 deletions loki_logger_handler/loki_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def send(self, data):
during sending or None if everything was successfull.
"""
response = None
error = None
try:
if self.compressed:
self.headers["Content-Encoding"] = "gzip"
Expand All @@ -61,7 +60,7 @@ def send(self, data):

except requests.RequestException as e:
sys.stderr.write("Error while sending logs: {}\n".format(e))
error = e

if response is not None:
sys.stderr.write(
"Response status code: {}, "
Expand All @@ -70,8 +69,8 @@ def send(self, data):
response.status_code, response.text, response.request.url
)
)
raise e

finally:
if response:
response.close()
return error