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.

82 lines
2.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. 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(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. log_message = "this log should have a trace context"
  61. self.logger.info(log_message)
  62. time.sleep(self.logs_drain_timeout * 2)
  63. logs_list = self.logzio_listener.logs_list
  64. for current_log in logs_list:
  65. if log_message in current_log:
  66. log_dict = json.loads(current_log)
  67. try:
  68. self.assertTrue('otelSpanID' in log_dict)
  69. self.assertTrue('otelTraceID' in log_dict)
  70. self.assertTrue('otelServiceName' in log_dict)
  71. except AssertionError as err:
  72. print(err)