From e44d5359c99b8a726e4eddde9f4b9f110efb7f7b Mon Sep 17 00:00:00 2001 From: ralongit Date: Thu, 6 Jul 2023 09:50:24 +0300 Subject: [PATCH] Add extra tests for trace context & extra fields --- .DS_Store | Bin 0 -> 6148 bytes README.md | 3 ++- setup.py | 2 +- tests/test_add_context.py | 25 ++++++++++++++++--- tests/test_extra_fields.py | 50 ++++++++++++++++++++++++++++++++++++- 5 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..54251d1aaa8d59bb807bf6faaa80b41ee629eb3b GIT binary patch literal 6148 zcmeHKOH0E*5Z-NT9~2=6g&r5Y7K~3QUP7#Yz=$4HYC=j4#%yU)1C~P0`iJ}_{vKy` zH-})rn~0r(-EVdtyO|HN55^dGm*D|p7Gtan4UwZ#BWSL5HB2xfS93&626;LUVj0v- z^cPL|?QIq_$!6@8Rlolun8#6?+4d)IG+SHk4oJUq?>)-g%lvGay8hx8M^{qDL8<$} zbu`T<*3P+1GCxYD3zZOsQwX`ciIPy}u3RQzs&YN;=z&bE!D@AUbUG9#_Gmp6tI?r7 z6ku4dd)?i={j3`ieqaFi2LTPyF<59+TL*M_eMWx^5e0O7OCSn^j=@4BctE&L1=OkB zJTbUV2fHwFj=@5sPG?-L4D*tAkyraK;^t)Dr{5K$U@}9$I+*U%)R@`^c}R z(1;iy2L2fXyfya5E)-?X)^FwESu3DDKtsW}0u>O@E0+Km;65@?PVE<{L!4u<(1^33 SU8MukML-ck9Wn3=4157-=S)lh literal 0 HcmV?d00001 diff --git a/README.md b/README.md index a24e64c..c08c995 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,7 @@ logger.info('Warning', extra={'extra_key':'extra_value'}) #### Dynamic Extra Fields If you prefer, you can add extra fields to your logs dynamically, and not pre-defining them in the configuration. This way, you can allow different logs to have different extra fields. + See the following code example: ```python @@ -291,7 +292,7 @@ LOGGING = { Please note that if you are using `python 3.8`, it is preferred to use the `logging.config.dictConfig` method, as mentioned in [python's documentation](https://docs.python.org/3/library/logging.config.html#configuration-file-format). ## Release Notes -- 4.0.3 +- 4.1.0 - Add ability to dynamically attach extra fields to the logs. - Import opentelemetry logging dependency only if trace context is enabled and dependency is installed manually. - Updated `opentelemetry-instrumentation-logging==0.39b0` diff --git a/setup.py b/setup.py index 63b3731..5157705 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import setup, find_packages setup( name="logzio-python-handler", - version='4.0.3', + version='4.1.0', description="Logging handler to send logs to your Logz.io account with bulk SSL", keywords="logging handler logz.io bulk https", author="roiravhon", diff --git a/tests/test_add_context.py b/tests/test_add_context.py index 8ffffdb..d176857 100644 --- a/tests/test_add_context.py +++ b/tests/test_add_context.py @@ -28,7 +28,7 @@ class TestAddContext(TestCase): self.retries_no = 4 self.retry_timeout = 2 self.add_context = True - logging_configuration = { + self.logging_configuration = { "version": 1, "formatters": { "logzio": { @@ -59,14 +59,14 @@ class TestAddContext(TestCase): } } - logging.config.dictConfig(logging_configuration) + logging.config.dictConfig(self.logging_configuration) self.logger = logging.getLogger('test') for curr_file in _find("logzio-failures-*.txt", "."): os.remove(curr_file) def test_add_context(self): - + # Logging configuration of add_context default to True log_message = "this log should have a trace context" self.logger.info(log_message) time.sleep(self.logs_drain_timeout * 2) @@ -80,3 +80,22 @@ class TestAddContext(TestCase): self.assertTrue('otelServiceName' in log_dict) except AssertionError as err: print(err) + + def test_ignore_context(self): + # Set add_context to False and reconfigure the logger as it defaults to True + self.logging_configuration["handlers"]["LogzioHandler"]["add_context"] = False + logging.config.dictConfig(self.logging_configuration) + self.logger = logging.getLogger('test') + log_message = "this log should not have a trace context" + self.logger.info(log_message) + time.sleep(self.logs_drain_timeout * 2) + logs_list = self.logzio_listener.logs_list + for current_log in logs_list: + if log_message in current_log: + log_dict = json.loads(current_log) + try: + self.assertFalse('otelSpanID' in log_dict) + self.assertFalse('otelTraceID' in log_dict) + self.assertFalse('otelServiceName' in log_dict) + except AssertionError as err: + print(err) diff --git a/tests/test_extra_fields.py b/tests/test_extra_fields.py index 4a332a7..33e9c5b 100644 --- a/tests/test_extra_fields.py +++ b/tests/test_extra_fields.py @@ -77,7 +77,55 @@ class TestExtraFieldsFilter(TestCase): if log_message in current_log: log_dict = json.loads(current_log) try: - self.assertEqual(extra_fields, {**extra_fields,**log_dict}) + self.assertEqual(extra_fields, {**extra_fields, **log_dict}) + except AssertionError as err: + print(err) + + def test_remove_extra_fields(self): + extra_fields = {"foo": "bar"} + + self.logger.addFilter(ExtraFieldsLogFilter(extra=extra_fields)) + log_message = "this log should have a additional fields" + self.logger.info(log_message) + + self.logger.removeFilter(ExtraFieldsLogFilter(extra=extra_fields)) + unfiltered_log_message = "this log shouldn't have a additional fields" + self.logger.info(unfiltered_log_message) + + time.sleep(self.logs_drain_timeout * 2) + logs_list = self.logzio_listener.logs_list + for current_log in logs_list: + if unfiltered_log_message in current_log: + log_dict = json.loads(current_log) + try: + self.assertNotEqual(extra_fields, {**extra_fields, **log_dict}) + except AssertionError as err: + print(err) + + def test_add_multiple_extra_fields(self): + extra_fields = {"foo": "bar"} + self.logger.addFilter(ExtraFieldsLogFilter(extra=extra_fields)) + log_message = "this log should have additional fields" + self.logger.info(log_message) + + extra_fields = {"counter":1} + self.logger.addFilter(ExtraFieldsLogFilter(extra=extra_fields)) + filtered_log_message = "this log should have multiple additional fields" + self.logger.info(filtered_log_message) + + time.sleep(self.logs_drain_timeout * 2) + logs_list = self.logzio_listener.logs_list + for current_log in logs_list: + if log_message in current_log: + log_dict = json.loads(current_log) + try: + self.assertEqual(extra_fields, {**extra_fields, **log_dict}) + except AssertionError as err: + print(err) + elif filtered_log_message in current_log: + log_dict = json.loads(current_log) + try: + self.assertEqual(extra_fields, {**extra_fields, **log_dict}) except AssertionError as err: print(err)