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

Add support for log filter default value #70

Merged
merged 1 commit into from
Feb 24, 2023
Merged
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ LOGGING = {
+ 'correlation_id': {
+ '()': 'asgi_correlation_id.CorrelationIdFilter',
+ 'uuid_length': 32,
+ 'default_value': '-',
+ },
+ },
'formatters': {
Expand Down Expand Up @@ -341,6 +342,7 @@ def configure_logging() -> None:
'correlation_id': {
'()': 'asgi_correlation_id.CorrelationIdFilter',
'uuid_length': 8 if not settings.ENVIRONMENT == 'local' else 32,
'default_value': '-',
},
},
'formatters': {
Expand Down Expand Up @@ -564,10 +566,12 @@ LOGGING = {
'correlation_id': {
+ '()': 'asgi_correlation_id.CorrelationIdFilter',
+ 'uuid_length': 32,
+ 'default_value': '-',
+ },
+ 'celery_tracing': {
+ '()': 'asgi_correlation_id.CeleryTracingIdsFilter',
+ 'uuid_length': 32,
+ 'default_value': '-',
+ },
},
'formatters': {
Expand Down Expand Up @@ -646,7 +650,7 @@ your logs could look something like this:
correlation-id current-id
| parent-id |
| | |
INFO [3b162382e1] [ None ] [93ddf3639c] project.tasks - Debug task 1
INFO [3b162382e1] [ - ] [93ddf3639c] project.tasks - Debug task 1
INFO [3b162382e1] [93ddf3639c] [24046ab022] project.tasks - Debug task 2
INFO [3b162382e1] [93ddf3639c] [cb5595a417] project.tasks - Debug task 2
INFO [3b162382e1] [24046ab022] [08f5428a66] project.tasks - Debug task 3
Expand Down
12 changes: 7 additions & 5 deletions asgi_correlation_id/log_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ def _trim_string(string: Optional[str], string_length: Optional[int]) -> Optiona
class CorrelationIdFilter(Filter):
"""Logging filter to attached correlation IDs to log records"""

def __init__(self, name: str = '', uuid_length: Optional[int] = None):
def __init__(self, name: str = '', uuid_length: Optional[int] = None, default_value: Optional[str] = None):
super().__init__(name=name)
self.uuid_length = uuid_length
self.default_value = default_value

def filter(self, record: 'LogRecord') -> bool:
"""
Expand All @@ -30,7 +31,7 @@ def filter(self, record: 'LogRecord') -> bool:
for, if the correlation ID is added to the message, or included as
metadata.
"""
cid = correlation_id.get()
cid = correlation_id.get(self.default_value)
record.correlation_id = _trim_string(cid, self.uuid_length)
return True

Expand All @@ -39,9 +40,10 @@ def filter(self, record: 'LogRecord') -> bool:


class CeleryTracingIdsFilter(Filter):
def __init__(self, name: str = '', uuid_length: Optional[int] = None):
def __init__(self, name: str = '', uuid_length: Optional[int] = None, default_value: Optional[str] = None):
super().__init__(name=name)
self.uuid_length = uuid_length
self.default_value = default_value

def filter(self, record: 'LogRecord') -> bool:
"""
Expand All @@ -52,8 +54,8 @@ def filter(self, record: 'LogRecord') -> bool:
the current process. If the worker process was spawned by a beat process
or from an endpoint, the parent ID will be None.
"""
pid = celery_parent_id.get()
pid = celery_parent_id.get(self.default_value)
record.celery_parent_id = _trim_string(pid, self.uuid_length)
cid = celery_current_id.get()
cid = celery_current_id.get(self.default_value)
record.celery_current_id = _trim_string(cid, self.uuid_length)
return True
46 changes: 46 additions & 0 deletions tests/test_log_filter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextvars
from logging import INFO, LogRecord
from uuid import uuid4

Expand All @@ -6,6 +7,12 @@
from asgi_correlation_id import CeleryTracingIdsFilter, CorrelationIdFilter
from asgi_correlation_id.context import celery_current_id, celery_parent_id, correlation_id

# Initialize context variables to obtain reset tokens which we can later use
# when testing application of filter default values.
correlation_id_token: contextvars.Token = correlation_id.set(None)
celery_parent_id_token: contextvars.Token = celery_parent_id.set(None)
celery_current_id_token: contextvars.Token = celery_current_id.set(None)


@pytest.fixture()
def cid():
Expand All @@ -26,6 +33,11 @@ def test_filter_has_uuid_length_attributes():
assert filter_.uuid_length == 8


def test_filter_has_default_value_attributes():
filter_ = CorrelationIdFilter(default_value='-')
assert filter_.default_value == '-'


def test_filter_adds_correlation_id(cid: str, log_record: LogRecord):
filter_ = CorrelationIdFilter()

Expand All @@ -43,11 +55,29 @@ def test_filter_truncates_correlation_id(cid: str, log_record: LogRecord):
assert cid.startswith(log_record.correlation_id) # And needs to be the first 8 characters of the id


def test_filter_uses_default_value(cid: str, log_record: LogRecord):
"""
We expect the filter to set the log record attribute to the default value
if the context variable is not set.
"""
filter_ = CorrelationIdFilter(default_value='-')
correlation_id.reset(correlation_id_token)

assert not hasattr(log_record, 'correlation_id')
filter_.filter(log_record)
assert log_record.correlation_id == '-'


def test_celery_filter_has_uuid_length_attributes():
filter_ = CeleryTracingIdsFilter(uuid_length=8)
assert filter_.uuid_length == 8


def test_celery_filter_has_default_value_attributes():
filter_ = CeleryTracingIdsFilter(default_value='-')
assert filter_.default_value == '-'


def test_celery_filter_adds_parent_id(cid: str, log_record: LogRecord):
filter_ = CeleryTracingIdsFilter()
celery_parent_id.set('a')
Expand All @@ -66,6 +96,22 @@ def test_celery_filter_adds_current_id(cid: str, log_record: LogRecord):
assert log_record.celery_current_id == 'b'


def test_celery_filter_uses_default_value(cid: str, log_record: LogRecord):
"""
We expect the filter to set the log record attributes to the default value
if the context variables are not not set.
"""
filter_ = CeleryTracingIdsFilter(default_value='-')
celery_parent_id.reset(celery_parent_id_token)
celery_current_id.reset(celery_current_id_token)

assert not hasattr(log_record, 'celery_parent_id')
assert not hasattr(log_record, 'celery_current_id')
filter_.filter(log_record)
assert log_record.celery_parent_id == '-'
assert log_record.celery_current_id == '-'


@pytest.mark.parametrize(
('uuid_length', 'expected'),
[
Expand Down