Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
xente authored Feb 3, 2025
2 parents 9faf3c8 + 07a399d commit 15fb86a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ A logging handler that sends log messages to **(Grafana) Loki** in JSON format.
* url (str): The URL of the Loki server.
* labels (dict): A dictionary of labels to attach to each log message.
* label_keys (dict, optional): A dictionary of keys to extract from each log message and use as labels. Defaults to None.
* auth (tuple, optional): Basic authentication credentials for the Loki request. Defaults to None.
* additional_headers (dict, optional): Additional headers for the Loki request. Defaults to None.
* timeout (int, optional): Timeout interval in seconds to wait before flushing the buffer. Defaults to 10 seconds.
* compressed (bool, optional): Whether to compress the log messages before sending them to Loki. Defaults to True.
Expand Down
5 changes: 4 additions & 1 deletion loki_logger_handler/loki_logger_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(
url,
labels,
label_keys=None,
auth=None,
additional_headers=None,
message_in_json_format=True,
timeout=10,
Expand All @@ -43,6 +44,8 @@ def __init__(
url (str): The URL of the Loki server.
labels (dict): Default labels for the logs.
label_keys (dict, optional): Specific log record keys to extract as labels. Defaults to None.
auth (tuple, optional): Basic authentication credentials for the Loki request. Defaults to None.
insecure_ssl_verify (bool): Whether to verify ssl certificate. Defaults to True
additional_headers (dict, optional): Additional headers for the Loki request. Defaults to None.
message_in_json_format (bool): Whether to format log values as JSON.
timeout (int, optional): Timeout interval for flushing logs. Defaults to 10 seconds.
Expand Down Expand Up @@ -70,7 +73,7 @@ def __init__(
self.debug_logger.addHandler(console_handler)

self.request = LokiRequest(
url=url, compressed=compressed, additional_headers=additional_headers or {},
url=url, compressed=compressed, auth=auth, additional_headers=additional_headers or {},
insecure_ssl_verify=insecure_ssl_verify
)

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 @@ -10,22 +10,26 @@ class LokiRequest:
Attributes:
url (str): The URL of the Loki server.
compressed (bool): Whether to compress the logs using gzip.
auth (tuple): Basic authentication credentials to include in the request.
headers (dict): Additional headers to include in the request.
session (requests.Session): The session used for making HTTP requests.
insecure_ssl_verify (bool): Whether to verify ssl certificate. Defaults to True
"""

def __init__(self, url, compressed=False, additional_headers=None, insecure_ssl_verify=True):
def __init__(self, url, compressed=False, auth=None, additional_headers=None, insecure_ssl_verify=True):
"""
Initialize the LokiRequest object with the server URL, compression option, and additional headers.
Args:
url (str): The URL of the Loki server.
compressed (bool, optional): Whether to compress the logs using gzip. Defaults to False.
auth (tuple, optional): Basic authentication credentials to include in the request. Defaults to None.
additional_headers (dict, optional): Additional headers to include in the request.
Defaults to an empty dictionary.
"""
self.url = url
self.compressed = compressed
self.auth = auth
self.headers = additional_headers if additional_headers is not None else {}
self.headers["Content-Type"] = "application/json"
self.session = requests.Session()
Expand All @@ -47,7 +51,7 @@ def send(self, data):
self.headers["Content-Encoding"] = "gzip"
data = gzip.compress(data.encode("utf-8"))

response = self.session.post(self.url, data=data, headers=self.headers, verify=self.insecure_ssl_verify)
response = self.session.post(self.url, data=data, auth=self.auth, headers=self.headers, verify=self.insecure_ssl_verify)
response.raise_for_status()

except requests.RequestException as e:
Expand Down

0 comments on commit 15fb86a

Please sign in to comment.