From 34dd571a324fab472ec2de0926b0b2658cfc1ce1 Mon Sep 17 00:00:00 2001 From: Ravil Latypov Date: Mon, 1 Feb 2021 10:01:21 +0300 Subject: [PATCH 1/3] add custom logging logging --- aiopika_macrobase/__init__.py | 1 + aiopika_macrobase/endpoint.py | 2 ++ aiopika_macrobase/logging.py | 51 +++++++++++++++++++++++++++++++++ aiopika_macrobase/rpc/client.py | 2 ++ setup.py | 2 +- 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 aiopika_macrobase/logging.py diff --git a/aiopika_macrobase/__init__.py b/aiopika_macrobase/__init__.py index ec0c3b3..f4601a7 100644 --- a/aiopika_macrobase/__init__.py +++ b/aiopika_macrobase/__init__.py @@ -3,3 +3,4 @@ from .method import Method from .endpoint import AiopikaEndpoint from .result import AiopikaResult, AiopikaResultAction +from .logging import configure_logger, set_request_id, get_logger diff --git a/aiopika_macrobase/endpoint.py b/aiopika_macrobase/endpoint.py index df72b23..111b999 100644 --- a/aiopika_macrobase/endpoint.py +++ b/aiopika_macrobase/endpoint.py @@ -7,6 +7,7 @@ from aio_pika import IncomingMessage from structlog import get_logger +from .logging import set_request_id log = get_logger('macrobase.aiopika.endpoint') @@ -30,6 +31,7 @@ async def handle(self, driver, message: IncomingMessage, data, *args, **kwargs) AiopikaResult: Aiopika result action or None (if return None then driver ack message). """ try: + set_request_id(message.headers.get('x-cross-request-id')) result = await self.method(driver, message, data, *args, **kwargs) except Exception as e: capture_exception(e) diff --git a/aiopika_macrobase/logging.py b/aiopika_macrobase/logging.py new file mode 100644 index 0000000..042db07 --- /dev/null +++ b/aiopika_macrobase/logging.py @@ -0,0 +1,51 @@ +from contextvars import ContextVar +from logging import Logger +from uuid import uuid4 + +from structlog import get_logger as get_struct_logger, configure +from structlog._config import _BUILTIN_DEFAULT_PROCESSORS, _CONFIG + +_cross_request_id = ContextVar('cross_request_id', default=None) + +__all__ = ['get_request_id', 'set_request_id', 'configure_logger', 'get_logger'] + + +def get_request_id() -> str: + """ + get cross request id. generate and return if not set + """ + req_id = _cross_request_id.get() + + if not req_id: + req_id = str(uuid4()) + _cross_request_id.set(req_id) + + return req_id + + +def set_request_id(request_id: str = None): + """ + set cross request id + """ + if not request_id: + request_id = str(uuid4()) + + _cross_request_id.set(request_id) + + +def _cross_request_processor(logger, log_method, event_dict: dict) -> dict: + event_dict['crossRequestId'] = get_request_id() + return event_dict + + +def configure_logger(): + """ + configure structlog, add crossRequestId + """ + if not _CONFIG.is_configured: + configure(processors=[_cross_request_processor, *_BUILTIN_DEFAULT_PROCESSORS]) + + +def get_logger(*args, **kwargs) -> Logger: + configure_logger() + return get_struct_logger(*args, **kwargs) diff --git a/aiopika_macrobase/rpc/client.py b/aiopika_macrobase/rpc/client.py index c5b3e61..c1d1490 100644 --- a/aiopika_macrobase/rpc/client.py +++ b/aiopika_macrobase/rpc/client.py @@ -16,6 +16,7 @@ from aio_pika.message import DeliveredMessage from logging import getLogger +from ..logging import get_request_id log = getLogger('AiopikaClient') @@ -240,6 +241,7 @@ async def _get_message(self, body, content_type: str, type: str, correlation_id: headers={ 'lang': 'py', 'method': identifier, + 'x-cross-request-id': get_request_id(), # 'id': correlation_id, # 'shadow': shadow, # 'countdown': countdown, diff --git a/setup.py b/setup.py index 028dbb4..57232c7 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ 'aio-pika==5.5.3', 'uvloop==0.14.0', 'python-rapidjson==0.8.0', - 'structlog==19.2.0', + 'structlog>=20.2.0,<21.0.0', 'sentry-sdk>=0.14.3', 'aiocontextvars==0.2.2' ] From d23ef89c43561713bb5ddd5256a02625153878e3 Mon Sep 17 00:00:00 2001 From: Ravil Latypov Date: Mon, 1 Feb 2021 10:40:24 +0300 Subject: [PATCH 2/3] use configured logger on macrobase-driver --- aiopika_macrobase/__init__.py | 1 - aiopika_macrobase/endpoint.py | 2 +- aiopika_macrobase/logging.py | 51 --------------------------------- aiopika_macrobase/rpc/client.py | 2 +- 4 files changed, 2 insertions(+), 54 deletions(-) delete mode 100644 aiopika_macrobase/logging.py diff --git a/aiopika_macrobase/__init__.py b/aiopika_macrobase/__init__.py index f4601a7..ec0c3b3 100644 --- a/aiopika_macrobase/__init__.py +++ b/aiopika_macrobase/__init__.py @@ -3,4 +3,3 @@ from .method import Method from .endpoint import AiopikaEndpoint from .result import AiopikaResult, AiopikaResultAction -from .logging import configure_logger, set_request_id, get_logger diff --git a/aiopika_macrobase/endpoint.py b/aiopika_macrobase/endpoint.py index 111b999..2cde2d8 100644 --- a/aiopika_macrobase/endpoint.py +++ b/aiopika_macrobase/endpoint.py @@ -7,7 +7,7 @@ from aio_pika import IncomingMessage from structlog import get_logger -from .logging import set_request_id +from macrobase_driver.logging import set_request_id log = get_logger('macrobase.aiopika.endpoint') diff --git a/aiopika_macrobase/logging.py b/aiopika_macrobase/logging.py deleted file mode 100644 index 042db07..0000000 --- a/aiopika_macrobase/logging.py +++ /dev/null @@ -1,51 +0,0 @@ -from contextvars import ContextVar -from logging import Logger -from uuid import uuid4 - -from structlog import get_logger as get_struct_logger, configure -from structlog._config import _BUILTIN_DEFAULT_PROCESSORS, _CONFIG - -_cross_request_id = ContextVar('cross_request_id', default=None) - -__all__ = ['get_request_id', 'set_request_id', 'configure_logger', 'get_logger'] - - -def get_request_id() -> str: - """ - get cross request id. generate and return if not set - """ - req_id = _cross_request_id.get() - - if not req_id: - req_id = str(uuid4()) - _cross_request_id.set(req_id) - - return req_id - - -def set_request_id(request_id: str = None): - """ - set cross request id - """ - if not request_id: - request_id = str(uuid4()) - - _cross_request_id.set(request_id) - - -def _cross_request_processor(logger, log_method, event_dict: dict) -> dict: - event_dict['crossRequestId'] = get_request_id() - return event_dict - - -def configure_logger(): - """ - configure structlog, add crossRequestId - """ - if not _CONFIG.is_configured: - configure(processors=[_cross_request_processor, *_BUILTIN_DEFAULT_PROCESSORS]) - - -def get_logger(*args, **kwargs) -> Logger: - configure_logger() - return get_struct_logger(*args, **kwargs) diff --git a/aiopika_macrobase/rpc/client.py b/aiopika_macrobase/rpc/client.py index c1d1490..06adb1d 100644 --- a/aiopika_macrobase/rpc/client.py +++ b/aiopika_macrobase/rpc/client.py @@ -16,7 +16,7 @@ from aio_pika.message import DeliveredMessage from logging import getLogger -from ..logging import get_request_id +from macrobase_driver.logging import get_request_id log = getLogger('AiopikaClient') From fe954d9b5303210ccf1d2f3aa9012b0623c864df Mon Sep 17 00:00:00 2001 From: Ravil Latypov Date: Mon, 1 Feb 2021 11:10:26 +0300 Subject: [PATCH 3/3] update requirements --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 57232c7..6c37db0 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ 'aio-pika==5.5.3', 'uvloop==0.14.0', 'python-rapidjson==0.8.0', - 'structlog>=20.2.0,<21.0.0', + 'structlog>=19.2.0,<21.0.0', 'sentry-sdk>=0.14.3', 'aiocontextvars==0.2.2' ]