Skip to content

Commit

Permalink
Improve performace
Browse files Browse the repository at this point in the history
  • Loading branch information
xente committed Dec 29, 2024
1 parent 64b6554 commit 4e153fc
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"forwardPorts": [
3000
],
"postCreateCommand": "pip3 install --user -r /workspaces/loki-logger-handler/requirements.txt",
"postCreateCommand": "pip3 install --user -r /workspaces/loki-logger-handler/requirements.txt && pip3 install pytest",
"customizations": {
"vscode": {
"extensions": [
Expand Down
10 changes: 7 additions & 3 deletions loki_logger_handler/formatters/logger_formatter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import traceback

import time

class LoggerFormatter:
"""
Expand Down Expand Up @@ -41,6 +41,7 @@ def format(self, record):
Returns:
dict: A dictionary representation of the log record.
"""

formatted = {
"message": record.getMessage(),
"timestamp": record.created,
Expand All @@ -53,8 +54,11 @@ def format(self, record):
}

# Capture any custom fields added to the log record
record_keys = set(record.__dict__.keys())
custom_fields = record_keys - self.LOG_RECORD_FIELDS
custom_fields = {
key: value for key, value in record.__dict__.items()
if key not in self.LOG_RECORD_FIELDS
}

loki_metadata = {}

for key in custom_fields:
Expand Down
16 changes: 4 additions & 12 deletions loki_logger_handler/loki_request.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import requests
import gzip
import sys
import requests

try:
from io import BytesIO as IO # For Python 3
except ImportError:
from StringIO import StringIO as IO # For Python 2


class LokiRequest:
Expand Down Expand Up @@ -49,14 +44,11 @@ def send(self, data):
try:
if self.compressed:
self.headers["Content-Encoding"] = "gzip"
buf = IO()
with gzip.GzipFile(fileobj=buf, mode="wb") as f:
f.write(data.encode("utf-8"))
data = buf.getvalue()

data = gzip.compress(data.encode("utf-8"))

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

except requests.RequestException as e:

if response is not None:
Expand Down
4 changes: 2 additions & 2 deletions loki_logger_handler/stream.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
import time
import json as json

# Compatibility for Python 2 and 3
try:
Expand Down Expand Up @@ -69,7 +69,7 @@ def append_value(self, value, metadata=None):
except (TypeError, ValueError):
# Fallback to the current time in nanoseconds if the timestamp is missing or invalid
timestamp = str(time_ns())

formatted_value = json.dumps(value, ensure_ascii=False) if self.message_in_json_format else value
if metadata or self.loki_metadata:
# Ensure both metadata and self.loki_metadata are dictionaries (default to empty dict if None)
Expand Down
10 changes: 5 additions & 5 deletions loki_logger_handler/streams.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json
import json as json


class _LokiRequestEncoder(json.JSONEncoder):
Expand All @@ -7,12 +7,12 @@ class _LokiRequestEncoder(json.JSONEncoder):
This internal class is used to handle the serialization
of Streams objects into the JSON format expected by Loki.
"""
def default(self, obj):
if isinstance(obj, Streams):
def default(self, o):
if isinstance(o, Streams):
# Convert the Streams object to a dictionary format suitable for Loki
return {"streams": [stream.__dict__ for stream in obj.streams]}
return {"streams": [stream.__dict__ for stream in o.streams]}
# Use the default serialization method for other objects
return super(_LokiRequestEncoder, self).default(obj)
return super(_LokiRequestEncoder, self).default(o)


class Streams(object): # Explicitly inherit from object for Python 2 compatibility
Expand Down

0 comments on commit 4e153fc

Please sign in to comment.