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

Dont crop correlation id in logs by default #14

Merged
merged 3 commits into from
Nov 30, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ jobs:
id: poetry-cache
with:
path: ~/.local
key: key-0
key: ${{ matrix.python-version }}-1
- uses: snok/install-poetry@v1
with:
virtualenvs-create: false
version: 1.1.11
version: 1.1.12
- uses: actions/cache@v2
id: cache-venv
with:
path: .venv
key: ${{ hashFiles('**/poetry.lock') }}-${{ matrix.python-version }}-0
key: ${{ hashFiles('**/poetry.lock') }}-${{ matrix.python-version }}-1
- run: |
python -m venv .venv
source .venv/bin/activate
Expand Down
9 changes: 6 additions & 3 deletions asgi_correlation_id/log_filters.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from logging import Filter, LogRecord
from typing import Type
from typing import Optional, Type

from asgi_correlation_id.context import celery_current_id, celery_parent_id, correlation_id

# Middleware


def correlation_id_filter(uuid_length: int = 32) -> Type[Filter]:
def correlation_id_filter(uuid_length: Optional[int] = None) -> Type[Filter]:
class CorrelationId(Filter):
def filter(self, record: LogRecord) -> bool:
"""
Expand All @@ -18,7 +18,10 @@ def filter(self, record: LogRecord) -> bool:
metadata.
"""
cid = correlation_id.get()
record.correlation_id = cid[:uuid_length] if cid else cid # type: ignore[attr-defined]
if uuid_length is not None and cid:
record.correlation_id = cid[:uuid_length] # type: ignore[attr-defined]
else:
record.correlation_id = cid # type: ignore[attr-defined]
return True

return CorrelationId
Expand Down
21 changes: 21 additions & 0 deletions tests/test_log_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from unittest.mock import Mock
from uuid import uuid4

from asgi_correlation_id import correlation_id_filter
from asgi_correlation_id.context import correlation_id


def test_correlation_id_filter():
mock_record = Mock()

cid = uuid4().hex
correlation_id.set(cid)

# Call with no uuid length
correlation_id_filter(None)().filter(mock_record)
assert mock_record.correlation_id == cid

# Call with uuid length
for length in [0, 14, 30, 100]:
correlation_id_filter(uuid_length=length)().filter(mock_record)
assert mock_record.correlation_id == cid[:length]
2 changes: 1 addition & 1 deletion tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async def test_websocket_request(caplog):


@app.get('/access-control-expose-headers')
async def access_control_view() -> dict:
async def access_control_view() -> Response:
return Response(status_code=204, headers={'Access-Control-Expose-Headers': 'test1, test2'})


Expand Down