Replies: 3 comments 20 replies
-
So for the first suggestion, you are proposing this change, correct? class CorrelationIdFilter(Filter):
"""Logging filter to attached correlation IDs to log records"""
- def __init__(self, name: str = '', uuid_length: Optional[int] = None):
+ def __init__(self, name: str = '', uuid_length: Optional[int] = None, default_value: Optional[str] = None):
super().__init__(name=name)
self.uuid_length = uuid_length
+ self.default_value = default_value
def filter(self, record: 'LogRecord') -> bool:
- cid = correlation_id.get()
+ cid = correlation_id.get() or default_value
record.correlation_id = _trim_string(cid, self.uuid_length)
return True That seems reasonable 👍 For the second, perhaps logging the logger, without change, after we call |
Beta Was this translation helpful? Give feedback.
-
First request: 👍 I agree 😊 Second request: I don't agree, not because it's not a nice idea, but because of security concerns. Sonarqube, bandit etc. will fail if this is implemented, and we've seen the catastrophic impact usage of headers, without parsing can have with log4j. I specifically removed it in Django-GUID due to this: snok/django-guid#69 If you really want this, I suggest creating a custom validator where you log the input, I don't think it should be the default of the library. |
Beta Was this translation helpful? Give feedback.
-
I've created PRs for both discussed changes, please take a look. |
Beta Was this translation helpful? Give feedback.
-
Hi @sondrelg I wonder if you would be willing to accept another two minor improvements.
The default value for the correlation ID is
None
which is perfectly fine. However,None
also appears in logs when there is no correlation ID set. People might want to alter this behaviour and log something more neutral asNone
looks too Pythonic. E.g. some symbol like-
(which is what for example nginx logs when variable is not defined). This could be achieved by introducing a new parameter to the log filter constructor which would be then supplied as the default value to the call tocorrelation_id.get()
. The parameter could be named for exampledefault_value
(makes sense in the context of a log filter) ornone_replacement
.The second thing relates to the case when incoming correlation ID does not pass validations and is thus replaced with a generated value. It logs a warning which reads
but
xxxxx
contains the original header value, not the newly generated one. Given the wording of the warning, it's a bit confusing as one would expect that the log would contain the newly generated value. I'd either suggest to replace the behaviour and log the newly generated value, or change the warning toThe later approach might be a better solution as it would allow you to correlate both IDs, the original (which might come from and be logged by a downstream service) and the new one (which might be propagated to and logged by an upstream service).
What do you think?
Beta Was this translation helpful? Give feedback.
All reactions