From 43d2441fb35b0096e7c74891fe61d7758f0a974e Mon Sep 17 00:00:00 2001 From: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com> Date: Wed, 3 Apr 2024 15:28:13 +0200 Subject: [PATCH] Remove the automatic creation of correlation ids (#16) --- README.rst | 4 ++-- reconplogger.py | 8 ++++---- reconplogger_tests.py | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.rst b/README.rst index 7796a7c..90bbcca 100644 --- a/README.rst +++ b/README.rst @@ -143,8 +143,8 @@ In a system build with microservices we need a way to correlate logs coming from For example when a user of our system do a call to the MicroserviceA this could need to retrieve some information from the MicroserviceB, if there is an error and we want to check the logs of the MicroserviceB related to the user call we don't have a way to correlate them, to solve this we use the correlation id! -Its a uuid4 that its passed in the headers of the rest calls and will be forwarded automatically when we do calls with the library *requests*, -if the correlation id its not present in the request headers it will be generated, all of this is taken care in the background by this library. +It is a unique string that is passed in the headers of the REST calls and will be forwarded automatically when we do calls with the library *requests*. All of this is taken care in the background by this library. +If the correlation id its not present in the request headers, it will not be generated. It is up to developers to explicitly create a correlation id. The usage would be as follows: diff --git a/reconplogger.py b/reconplogger.py index 07811f6..87a4728 100644 --- a/reconplogger.py +++ b/reconplogger.py @@ -7,7 +7,6 @@ from importlib.util import find_spec from logging import CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET from typing import Optional, Union -import uuid import time @@ -23,7 +22,7 @@ def _request_patch(slf, *args, **kwargs): headers = kwargs.pop("headers", {}) - if has_request_context(): + if has_request_context() and g.correlation_id: headers["Correlation-ID"] = g.correlation_id return slf.request_orig(*args, **kwargs, headers=headers) @@ -336,7 +335,7 @@ def flask_app_logger_setup( # Add flask before and after request functions to augment the logs def _flask_logging_before_request(): g.correlation_id = request.headers.get( - "Correlation-ID", str(uuid.uuid4()) + "Correlation-ID", None ) # pylint: disable=assigning-non-slot g.start_time = time.time() # pylint: disable=assigning-non-slot @@ -345,7 +344,8 @@ def _flask_logging_before_request(): ) def _flask_logging_after_request(response): - response.headers.set("Correlation-ID", g.correlation_id) + if g.correlation_id: + response.headers.set("Correlation-ID", g.correlation_id) if request.path not in flask_request_completed_skip_endpoints: if is_json_logger: message = "Request completed" diff --git a/reconplogger_tests.py b/reconplogger_tests.py index 84bb8d8..654fdc4 100755 --- a/reconplogger_tests.py +++ b/reconplogger_tests.py @@ -241,7 +241,7 @@ def hello_world(): correlation_id = request.args.get("id") reconplogger.set_correlation_id(correlation_id) app.logger.info(flask_msg) # pylint: disable=no-member - return "correlation_id=" + correlation_id + return "correlation_id=" + str(correlation_id) client = app.test_client() with LogCapture( @@ -271,14 +271,14 @@ def hello_world(): (app.logger.name, "INFO", flask_msg, correlation_id), (app.logger.name, "INFO", "Request completed", correlation_id), ) - # Check correlation id creation + # Check missing correlation id with LogCapture( names=app.logger.name, attributes=("name", "levelname", "getMessage", "correlation_id"), ) as logs: client.get("/") correlation_id = logs.actual()[0][3] - uuid.UUID(correlation_id) + self.assertIsNone(correlation_id) logs.check( (app.logger.name, "INFO", flask_msg, correlation_id), (app.logger.name, "INFO", "Request completed", correlation_id),