Skip to content

Commit 8a3574c

Browse files
authored
Merge pull request #26 from SAP/per-thread-cid
Enable per thread correlation ID in job logging
2 parents 4662334 + 6ce33c3 commit 8a3574c

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

README.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ Setting and getting correlation ID
159159
""""""""""""""""""""""""""""""""""
160160

161161
When using cf_logging in a web application you don't need to set the correlation ID, because the logging library will fetch it from the HTTP headers and set it.
162-
For non web applications you could set the correlation ID manually, so that the log entries can be filtered later on based on the correlation_id.
162+
For non web applications you could set the correlation ID manually, so that the log entries can be filtered later on based on the ``correlation_id`` log property.
163+
In this case the correlation ID is kept in a thread local variable and each thread should set its own correlation ID.
163164

164165
Setting and getting the correlation_id can be done via:
165166

@@ -168,11 +169,7 @@ Setting and getting the correlation_id can be done via:
168169
cf_logging.FRAMEWORK.context.get_correlation_id()
169170
cf_logging.FRAMEWORK.context.set_correlation_id(value)
170171
171-
Whenever a correlation id is set after initializing cf_logging without a specific framework (ex: cf_logging.init()) - this same correlation ID would be used for each log record.
172-
173-
If you need a *thread safe* correlation ID, you can reuse the ``cf_logging.job_loffing.framework.JobFramework`` and provide your own context implementation that is *thread safe*.
174-
175-
If you need to get the correlation_id in a web application, take into account the framework you are using.
172+
If you need to get the correlation ID in a web application, take into account the framework you are using.
176173
In async frameworks like Sanic and Falcon the context is stored into the request object and you need to provide the request to the call:
177174

178175
.. code:: python

sap/cf_logging/job_logging/context.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
""" Job logging context - used by the logging package to keep log data """
2+
import threading
23
from sap.cf_logging.core.context import Context
34

45

5-
class JobContext(Context):
6+
class JobContext(Context, threading.local):
67
""" Stores logging context in dict """
78

89
def __init__(self):
10+
super(JobContext, self).__init__()
911
self._mem_store = {}
1012

1113
def set(self, key, value, request):

tests/test_job_logging.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
""" Module to test the cf_logging library """
2+
import uuid
23
import logging
4+
import time
5+
import threading
36
from json import JSONDecoder
47
import pytest
58
from json_validator.validator import JsonValidator
@@ -42,3 +45,30 @@ def test_set_correlation_id():
4245
assert error == {}
4346
assert log_json['correlation_id'] == correlation_id
4447
assert cf_logging.FRAMEWORK.context.get_correlation_id() == correlation_id
48+
49+
def test_thread_safety():
50+
""" test context keeps separate correlation ID per thread """
51+
class _SampleThread(threading.Thread):
52+
def __init__(self):
53+
super(_SampleThread, self).__init__()
54+
self.correlation_id = str(uuid.uuid1())
55+
self.read_correlation_id = ''
56+
57+
def run(self):
58+
cf_logging.FRAMEWORK.context.set_correlation_id(self.correlation_id)
59+
time.sleep(0.1)
60+
self.read_correlation_id = cf_logging.FRAMEWORK.context.get_correlation_id()
61+
62+
cf_logging.init(level=logging.DEBUG)
63+
64+
thread_one = _SampleThread()
65+
thread_two = _SampleThread()
66+
67+
thread_one.start()
68+
thread_two.start()
69+
70+
thread_one.join()
71+
thread_two.join()
72+
73+
assert thread_one.correlation_id == thread_one.read_correlation_id
74+
assert thread_two.correlation_id == thread_two.read_correlation_id

0 commit comments

Comments
 (0)