Browse Source

Add extra tests for trace context & extra fields

master
ralongit 1 year ago
parent
commit
e44d5359c9
5 changed files with 74 additions and 6 deletions
  1. BIN
      .DS_Store
  2. +2
    -1
      README.md
  3. +1
    -1
      setup.py
  4. +22
    -3
      tests/test_add_context.py
  5. +49
    -1
      tests/test_extra_fields.py

BIN
.DS_Store View File


+ 2
- 1
README.md View File

@ -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`


+ 1
- 1
setup.py View File

@ -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",


+ 22
- 3
tests/test_add_context.py View File

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

+ 49
- 1
tests/test_extra_fields.py View File

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


Loading…
Cancel
Save