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 all commits
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
24 changes: 17 additions & 7 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 Down Expand Up @@ -62,16 +65,20 @@ 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):
"""
Emit a log record.
Expand Down Expand Up @@ -117,7 +124,10 @@ def _send(self):

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

def write(self, message):
"""
Expand Down
5 changes: 4 additions & 1 deletion loki_logger_handler/loki_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class LokiRequest:
headers (dict): Additional headers to include in the request.
session (requests.Session): The session used for making HTTP requests.
"""

def __init__(self, url, compressed=False, additional_headers=None):
"""
Initialize the LokiRequest object with the server URL, compression option, and additional headers.
Expand Down Expand Up @@ -49,7 +50,7 @@ def send(self, data):
if self.compressed:
self.headers["Content-Encoding"] = "gzip"
buf = IO()
with gzip.GzipFile(fileobj=buf, mode='wb') as f:
with gzip.GzipFile(fileobj=buf, mode="wb") as f:
f.write(data.encode("utf-8"))
data = buf.getvalue()

Expand All @@ -58,6 +59,7 @@ def send(self, data):

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

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

finally:
if response:
Expand Down