Skip to content

Commit

Permalink
Remove the automatic creation of correlation ids (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
mauvilsa authored Apr 3, 2024
1 parent 1de5aa9 commit 43d2441
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions reconplogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)

Expand Down Expand Up @@ -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

Expand All @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions reconplogger_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 43d2441

Please sign in to comment.