Logging handler to send logs to your OpenSearch cluster with bulk SSL. Forked from https://github.com/logzio/logzio-python-handler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.7 KiB

  1. import fnmatch
  2. import logging.config
  3. import os
  4. import time
  5. import json
  6. from unittest import TestCase
  7. from .mockLogzioListener import listener
  8. def _find(pattern, path):
  9. result = []
  10. for root, dirs, files in os.walk(path):
  11. for name in files:
  12. if fnmatch.fnmatch(name, pattern):
  13. result.append(os.path.join(root, name))
  14. break # Not descending recursively
  15. return result
  16. class TestAddContext(TestCase):
  17. def setUp(self):
  18. self.logzio_listener = listener.MockLogzioListener()
  19. self.logzio_listener.clear_logs_buffer()
  20. self.logzio_listener.clear_server_error()
  21. self.logs_drain_timeout = 1
  22. self.retries_no = 4
  23. self.retry_timeout = 2
  24. self.add_context = True
  25. self.logging_configuration = {
  26. "version": 1,
  27. "formatters": {
  28. "logzio": {
  29. "format": '{"key": "value"}',
  30. "validate": False
  31. }
  32. },
  33. "handlers": {
  34. "LogzioHandler": {
  35. "class": "logzio.handler.LogzioHandler",
  36. "formatter": "logzio",
  37. "level": "DEBUG",
  38. "token": "token",
  39. 'logzio_type': "type",
  40. 'logs_drain_timeout': self.logs_drain_timeout,
  41. 'url': "http://" + self.logzio_listener.get_host() + ":" + str(self.logzio_listener.get_port()),
  42. 'debug': True,
  43. 'retries_no': self.retries_no,
  44. 'retry_timeout': self.retry_timeout,
  45. 'add_context': self.add_context
  46. }
  47. },
  48. "loggers": {
  49. "test": {
  50. "handlers": ["LogzioHandler"],
  51. "level": "DEBUG"
  52. }
  53. }
  54. }
  55. logging.config.dictConfig(self.logging_configuration)
  56. self.logger = logging.getLogger('test')
  57. for curr_file in _find("logzio-failures-*.txt", "."):
  58. os.remove(curr_file)
  59. def test_add_context(self):
  60. # Logging configuration of add_context default to True
  61. log_message = "this log should have a trace context"
  62. self.logger.info(log_message)
  63. time.sleep(self.logs_drain_timeout * 2)
  64. logs_list = self.logzio_listener.logs_list
  65. for current_log in logs_list:
  66. if log_message in current_log:
  67. log_dict = json.loads(current_log)
  68. try:
  69. self.assertTrue('otelSpanID' in log_dict)
  70. self.assertTrue('otelTraceID' in log_dict)
  71. self.assertTrue('otelServiceName' in log_dict)
  72. except AssertionError as err:
  73. print(err)
  74. def test_ignore_context(self):
  75. # Set add_context to False and reconfigure the logger as it defaults to True
  76. self.logging_configuration["handlers"]["LogzioHandler"]["add_context"] = False
  77. logging.config.dictConfig(self.logging_configuration)
  78. self.logger = logging.getLogger('test')
  79. log_message = "this log should not have a trace context"
  80. self.logger.info(log_message)
  81. time.sleep(self.logs_drain_timeout * 2)
  82. logs_list = self.logzio_listener.logs_list
  83. for current_log in logs_list:
  84. if log_message in current_log:
  85. log_dict = json.loads(current_log)
  86. try:
  87. self.assertFalse('otelSpanID' in log_dict)
  88. self.assertFalse('otelTraceID' in log_dict)
  89. self.assertFalse('otelServiceName' in log_dict)
  90. except AssertionError as err:
  91. print(err)