Repository navigation
MPT-26429 Include extension id in mrok access logs #101
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import contextvars | ||
| import logging | ||
|
|
||
| request_target: contextvars.ContextVar[str | None] = contextvars.ContextVar( | ||
| "request_target", default=None | ||
| ) | ||
|
|
||
|
|
||
| def setup_log_record_factory() -> None: | ||
| default_factory = logging.getLogRecordFactory() | ||
|
|
||
| def record_factory(*args, **kwargs): | ||
| record = default_factory(*args, **kwargs) | ||
| record.request_target = request_target.get() or "-" | ||
| return record | ||
|
|
||
| logging.setLogRecordFactory(record_factory) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import logging | ||
|
|
||
| import pytest | ||
| from pytest_mock import MockerFixture | ||
|
|
||
| from mrok.frontend.logging import request_target, setup_log_record_factory | ||
| from mrok.logging import ACCESS_LOG_FORMAT, get_logging_config | ||
| from tests.types import SettingsFactory | ||
|
|
||
|
|
||
| def _install_factory(mocker: MockerFixture): | ||
| m_set_factory = mocker.patch("logging.setLogRecordFactory") | ||
| setup_log_record_factory() | ||
| return m_set_factory.call_args.args[0] | ||
|
|
||
|
|
||
| def test_log_record_factory_sets_request_target(mocker: MockerFixture): | ||
| factory = _install_factory(mocker) | ||
|
|
||
| token = request_target.set("ext-1234-5678") | ||
| try: | ||
| record = factory("uvicorn.access", logging.INFO, __file__, 1, "msg", (), None) | ||
| finally: | ||
| request_target.reset(token) | ||
|
|
||
| assert isinstance(record, logging.LogRecord) | ||
| assert record.request_target == "ext-1234-5678" | ||
|
|
||
|
|
||
| def test_log_record_factory_without_request_target(mocker: MockerFixture): | ||
| factory = _install_factory(mocker) | ||
|
|
||
| record = factory("uvicorn.access", logging.INFO, __file__, 1, "msg", (), None) | ||
|
|
||
| assert record.request_target == "-" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("rich", "expected_handler"), | ||
| [(False, "access"), (True, "rich")], | ||
| ) | ||
| def test_get_logging_config_access_handler( | ||
| settings_factory: SettingsFactory, rich: bool, expected_handler: str | ||
| ): | ||
| settings = settings_factory(logging={"debug": True, "rich": rich}) | ||
|
|
||
| config = get_logging_config(settings) | ||
|
|
||
| assert config["loggers"]["gunicorn.access"]["handlers"] == [expected_handler] | ||
| assert config["loggers"]["uvicorn.access"]["handlers"] == [expected_handler] | ||
| assert config["formatters"]["access"] == { | ||
| "format": ACCESS_LOG_FORMAT, | ||
| "style": "{", | ||
| "defaults": {"request_target": "-"}, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.