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.

124 lines
4.4 KiB

  1. # Copyright 2016-2017 Versada <https://versada.eu/>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. import collections
  4. import logging
  5. import warnings
  6. import odoo.loglevels
  7. from sentry_sdk.integrations.logging import LoggingIntegration
  8. from sentry_sdk import HttpTransport
  9. from sentry_sdk.consts import DEFAULT_OPTIONS
  10. def split_multiple(string, delimiter=',', strip_chars=None):
  11. """Splits :param:`string` and strips :param:`strip_chars` from values."""
  12. if not string:
  13. return []
  14. return [v.strip(strip_chars) for v in string.split(delimiter)]
  15. SentryOption = collections.namedtuple(
  16. 'SentryOption', ['key', 'default', 'converter'])
  17. # Mapping of Odoo logging level -> Python stdlib logging library log level.
  18. LOG_LEVEL_MAP = dict([
  19. (getattr(odoo.loglevels, 'LOG_%s' % x), getattr(logging, x))
  20. for x in ('CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'NOTSET')
  21. ])
  22. DEFAULT_LOG_LEVEL = 'warn'
  23. ODOO_USER_EXCEPTIONS = [
  24. 'odoo.exceptions.AccessDenied',
  25. 'odoo.exceptions.AccessError',
  26. 'odoo.exceptions.DeferredException',
  27. 'odoo.exceptions.MissingError',
  28. 'odoo.exceptions.RedirectWarning',
  29. 'odoo.exceptions.UserError',
  30. 'odoo.exceptions.ValidationError',
  31. 'odoo.exceptions.Warning',
  32. 'odoo.exceptions.except_orm',
  33. ]
  34. DEFAULT_IGNORED_EXCEPTIONS = ','.join(ODOO_USER_EXCEPTIONS)
  35. EXCLUDE_LOGGERS = (
  36. 'werkzeug',
  37. )
  38. DEFAULT_EXCLUDE_LOGGERS = ','.join(EXCLUDE_LOGGERS)
  39. DEFAULT_ENVIRONMENT = 'develop'
  40. DEFAULT_TRANSPORT = 'threaded'
  41. def select_transport(name=DEFAULT_TRANSPORT):
  42. warnings.warn(
  43. "`sentry_transport` has been deprecated. "
  44. "Its not neccesary send it, will use `HttpTranport` by default.",
  45. DeprecationWarning,
  46. )
  47. return {
  48. 'threaded': HttpTransport,
  49. }.get(name, HttpTransport)
  50. def get_sentry_logging(level=DEFAULT_LOG_LEVEL):
  51. if level not in LOG_LEVEL_MAP:
  52. level = DEFAULT_LOG_LEVEL
  53. return LoggingIntegration(
  54. level=LOG_LEVEL_MAP[level],
  55. event_level=logging.WARNING
  56. )
  57. def get_sentry_options():
  58. return [
  59. SentryOption('dsn', '', str.strip),
  60. SentryOption('transport',
  61. DEFAULT_OPTIONS['transport'], select_transport),
  62. SentryOption('logging_level',
  63. DEFAULT_LOG_LEVEL, get_sentry_logging),
  64. SentryOption(
  65. 'with_locals', DEFAULT_OPTIONS['with_locals'], None),
  66. SentryOption(
  67. 'max_breadcrumbs', DEFAULT_OPTIONS['max_breadcrumbs'], None),
  68. SentryOption('release', DEFAULT_OPTIONS['release'], None),
  69. SentryOption(
  70. 'environment', DEFAULT_OPTIONS['environment'], None),
  71. SentryOption(
  72. 'server_name', DEFAULT_OPTIONS['server_name'], None),
  73. SentryOption('shutdown_timeout',
  74. DEFAULT_OPTIONS['shutdown_timeout'], None),
  75. SentryOption('integrations',
  76. DEFAULT_OPTIONS['integrations'], None),
  77. SentryOption('in_app_include',
  78. DEFAULT_OPTIONS['in_app_include'], split_multiple),
  79. SentryOption('in_app_exclude',
  80. DEFAULT_OPTIONS['in_app_exclude'], split_multiple),
  81. SentryOption('default_integrations',
  82. DEFAULT_OPTIONS['default_integrations'], None),
  83. SentryOption('dist', DEFAULT_OPTIONS['dist'], None),
  84. SentryOption('sample_rate',
  85. DEFAULT_OPTIONS['sample_rate'], None),
  86. SentryOption('send_default_pii',
  87. DEFAULT_OPTIONS['send_default_pii'], None),
  88. SentryOption('http_proxy',
  89. DEFAULT_OPTIONS['http_proxy'], None),
  90. SentryOption('https_proxy',
  91. DEFAULT_OPTIONS['https_proxy'], None),
  92. SentryOption('ignore_exceptions',
  93. DEFAULT_IGNORED_EXCEPTIONS, split_multiple),
  94. SentryOption('request_bodies',
  95. DEFAULT_OPTIONS['request_bodies'], None),
  96. SentryOption('attach_stacktrace',
  97. DEFAULT_OPTIONS['attach_stacktrace'], None),
  98. SentryOption('ca_certs', DEFAULT_OPTIONS['ca_certs'], None),
  99. SentryOption('propagate_traces',
  100. DEFAULT_OPTIONS['propagate_traces'], None),
  101. SentryOption('traces_sample_rate',
  102. DEFAULT_OPTIONS['traces_sample_rate'], None),
  103. SentryOption('auto_enabling_integrations',
  104. DEFAULT_OPTIONS['auto_enabling_integrations'], None),
  105. ]