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 2 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
26 changes: 18 additions & 8 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,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
10 changes: 7 additions & 3 deletions 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 All @@ -41,15 +42,16 @@ def send(self, data):
Args:
data (str): The log data to be sent.

Raises:
fighterii marked this conversation as resolved.
Show resolved Hide resolved
requests.RequestException: If the request fails.
Returns:
error (requests.RequestException | None): Returns the error occurs
during sending or None if everything was successfull.
"""
response = None
try:
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 +60,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 +69,7 @@ def send(self, data):
response.status_code, response.text, response.request.url
)
)
raise e

finally:
if response:
Expand Down