Skip to content
Merged
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
13 changes: 11 additions & 2 deletions src/django_graylog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,29 @@


GELF_FIELD_REGEX = re.compile(r"^[\w\.\-]+$")
GELF_RESERVED_FIELDS = set(
[
"id",
"version",
"host",
"short_message",
"full_message",
"timestamp",
"level",
"facility",
"line",
"file",
"logs",
]
)

Check failure on line 73 in src/django_graylog/__init__.py

View workflow job for this annotation

GitHub Actions / checks

ruff (C405)

src/django_graylog/__init__.py:59:24: C405 Unnecessary list literal (rewrite as a set literal) help: Rewrite as a set literal

SENSITIVE_HEADERS = set(
[
"authorization",
"cookie",
"proxy-authorization",
]
)

Check failure on line 81 in src/django_graylog/__init__.py

View workflow job for this annotation

GitHub Actions / checks

ruff (C405)

src/django_graylog/__init__.py:75:21: C405 Unnecessary list literal (rewrite as a set literal) help: Rewrite as a set literal


def get_ip(request):
Expand Down Expand Up @@ -118,15 +118,15 @@
def __setitem__(self, name, value):
if name.startswith("_"):
raise KeyError(
"Invalid key ({}). Keys will automatically be prefixed with an "
"underscore.".format(name)

Check failure on line 122 in src/django_graylog/__init__.py

View workflow job for this annotation

GitHub Actions / checks

ruff (UP032)

src/django_graylog/__init__.py:121:17: UP032 Use f-string instead of `format` call help: Convert to f-string
)
if not GELF_FIELD_REGEX.match(name):
raise KeyError(
"Invalid key ({}). Keys must match [\\w\\.\\-]+.".format(name)

Check failure on line 126 in src/django_graylog/__init__.py

View workflow job for this annotation

GitHub Actions / checks

ruff (UP032)

src/django_graylog/__init__.py:126:17: UP032 Use f-string instead of `format` call help: Convert to f-string
)
if name in GELF_RESERVED_FIELDS:
raise KeyError("Invalid key ({}). This key name is reserved.".format(name))

Check failure on line 129 in src/django_graylog/__init__.py

View workflow job for this annotation

GitHub Actions / checks

ruff (UP032)

src/django_graylog/__init__.py:129:28: UP032 Use f-string instead of `format` call help: Convert to f-string
self.extra[name] = value

def log(self, level, message, *args, **kwargs):
Expand Down Expand Up @@ -218,12 +218,21 @@
self.address = (parts.hostname, parts.port)
self.timeout = float(getattr(settings, "GRAYLOG_TIMEOUT", 0.25))
self.delim = bytes(getattr(settings, "GRAYLOG_TCP_DELIMITER", b"\x00"))
self.retries = getattr(settings, "GRAYLOG_TCP_CONN_RETRIES", 1)

def create_conn(self):
for attempt in range(1, self.retries + 1):
try:
# TODO: have an option to keep a socket open and reconnect as needed?
return socket.create_connection(self.address, timeout=self.timeout)
except Exception:
if attempt == self.retries:
raise

def send(self, record):
# Graylog over TCP does not support compression.
payload = json.dumps(record).encode("utf-8")
# TODO: have an option to keep a socket open and reconnect as needed?
with socket.create_connection(self.address, timeout=self.timeout) as sock:
with self.create_conn() as sock:
sock.sendall(payload + self.delim)


Expand Down Expand Up @@ -256,13 +265,13 @@


class GraylogMiddleware:
scheme_transports = {
"http": RequestsTransport,
"https": RequestsTransport,
"udp": UDPTransport,
"tcp": TCPTransport,
"test": TestTransport,
}

Check failure on line 274 in src/django_graylog/__init__.py

View workflow job for this annotation

GitHub Actions / checks

ruff (RUF012)

src/django_graylog/__init__.py:268:25: RUF012 Mutable default value for class attribute help: Consider initializing in `__init__` or annotating with `typing.ClassVar`

def __init__(self, get_response):
self.endpoint = getattr(settings, "GRAYLOG_ENDPOINT", "")
Expand All @@ -284,7 +293,7 @@

def __call__(self, request):
start = time.time()
setattr(request, "graylog", GraylogProxy(self.facility))

Check failure on line 296 in src/django_graylog/__init__.py

View workflow job for this annotation

GitHub Actions / checks

ruff (B010)

src/django_graylog/__init__.py:296:9: B010 Do not call `setattr` with a constant attribute value. It is not any safer than normal property access. help: Replace `setattr` with assignment
token = current_request.set(request)
response = self.get_response(request)
elapsed = time.time() - start
Expand Down Expand Up @@ -315,7 +324,7 @@
# trace, and don't include the _exception_message field.
lines.pop()
fields["full_message"] = textwrap.dedent("".join(lines)).rstrip()
setattr(request, "_graylog_exception", fields)

Check failure on line 327 in src/django_graylog/__init__.py

View workflow job for this annotation

GitHub Actions / checks

ruff (B010)

src/django_graylog/__init__.py:327:9: B010 Do not call `setattr` with a constant attribute value. It is not any safer than normal property access. help: Replace `setattr` with assignment

def filter(self, record, request, response):
for field_name, regexes in self.filters.items():
Expand Down Expand Up @@ -364,10 +373,10 @@
def request_headers(self, request):
headers = {}
include_headers = getattr(settings, "GRAYLOG_HEADERS", [])
exclude_headers = set(
name.lower()
for name in getattr(settings, "GRAYLOG_EXCLUDE_HEADERS", SENSITIVE_HEADERS)
)

Check failure on line 379 in src/django_graylog/__init__.py

View workflow job for this annotation

GitHub Actions / checks

ruff (C401)

src/django_graylog/__init__.py:376:27: C401 Unnecessary generator (rewrite as a set comprehension) help: Rewrite as a set comprehension
if include_headers is True:
# User-Agent and Referer are not included by default, since they will either
# be in separate fields if requested via settings, or can be explicitly
Expand Down Expand Up @@ -418,8 +427,8 @@
try:
if request.user.is_authenticated:
record["_username"] = request.user.get_username()
except Exception:
pass

Check failure on line 431 in src/django_graylog/__init__.py

View workflow job for this annotation

GitHub Actions / checks

ruff (S110)

src/django_graylog/__init__.py:430:13: S110 `try`-`except`-`pass` detected, consider logging the exception
if getattr(settings, "GRAYLOG_HEADERS", False):
record.update(self.request_headers(request))
if getattr(settings, "GRAYLOG_USER_AGENT", False) and has_ua_parser:
Expand Down
Loading