__init__.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import os
  17. import logging
  18. from mediagoblin.tools import pluginapi
  19. _log = logging.getLogger(__name__)
  20. def get_client():
  21. from raven import Client
  22. config = pluginapi.get_config('mediagoblin.plugins.raven')
  23. sentry_dsn = config.get('sentry_dsn')
  24. client = None
  25. if sentry_dsn:
  26. _log.info('Setting up raven from plugin config: {0}'.format(
  27. sentry_dsn))
  28. client = Client(sentry_dsn)
  29. elif os.environ.get('SENTRY_DSN'):
  30. _log.info('Setting up raven from SENTRY_DSN environment variable: {0}'\
  31. .format(os.environ.get('SENTRY_DSN')))
  32. client = Client() # Implicitly looks for SENTRY_DSN
  33. if not client:
  34. _log.error('Could not set up client, missing sentry DSN')
  35. return None
  36. return client
  37. def setup_celery():
  38. from raven.contrib.celery import register_signal
  39. client = get_client()
  40. register_signal(client)
  41. def setup_logging():
  42. config = pluginapi.get_config('mediagoblin.plugins.raven')
  43. conf_setup_logging = False
  44. if config.get('setup_logging'):
  45. conf_setup_logging = bool(int(config.get('setup_logging')))
  46. if not conf_setup_logging:
  47. return
  48. from raven.handlers.logging import SentryHandler
  49. from raven.conf import setup_logging
  50. client = get_client()
  51. _log.info('Setting up raven logging handler')
  52. setup_logging(SentryHandler(client))
  53. def wrap_wsgi(app):
  54. from raven.middleware import Sentry
  55. client = get_client()
  56. _log.info('Attaching raven middleware...')
  57. return Sentry(app, client)
  58. hooks = {
  59. 'setup': setup_logging,
  60. 'wrap_wsgi': wrap_wsgi,
  61. 'celery_logging_setup': setup_logging,
  62. 'celery_setup': setup_celery,
  63. }