Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sentry integration #1846

Merged
merged 8 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/app/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def insert_settings(request):
'github_handle': request.user.username if user_is_authenticated else False,
'email': request.user.email if user_is_authenticated else False,
'name': request.user.get_full_name() if user_is_authenticated else False,
'rollbar_client_token': settings.ROLLBAR_CLIENT_TOKEN,
'sentry_address': settings.SENTRY_ADDRESS,
'env': settings.ENV,
'email_key': email_key,
'profile_id': profile.id if profile else '',
Expand Down
96 changes: 50 additions & 46 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.

"""
import os
import socket

from django.http import Http404
from django.utils.translation import gettext_noop

import environ
import rollbar
import raven
from easy_thumbnails.conf import Settings as easy_thumbnails_defaults

root = environ.Path(__file__) - 2 # Set the base directory to two levels.
Expand Down Expand Up @@ -70,6 +71,7 @@
'autotranslate',
'django_extensions',
'easy_thumbnails',
'raven.contrib.django.raven_compat',
'app',
'avatar',
'retail',
Expand All @@ -96,6 +98,7 @@
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'raven.contrib.django.raven_compat.middleware.SentryResponseErrorIdMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
Expand Down Expand Up @@ -189,26 +192,46 @@
if ENV not in ['local', 'test']:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_is_false': {
'()': 'django.utils.log.RequireDebugFalse'
'disable_existing_loggers': True,
'root': {
'level': 'WARNING',
'handlers': ['sentry'],
},
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s '
'%(process)d %(thread)d %(message)s'
},
},
'handlers': {
'rotatingfilehandler': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/var/log/django/debug.log',
'maxBytes': 1024 * 1024 * 10, # 10 MB
'backupCount': 100, # max 100 logs
'sentry': {
'level': 'ERROR', # To capture more than ERROR, change to WARNING, INFO, etc.
'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
'tags': {
'custom-tag': 'x'
},
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
}
},
'loggers': {
'django': {
'handlers': ['rotatingfilehandler', ],
'propagate': True,
'filters': ['require_debug_is_false'],
'django.db.backends': {
'level': 'ERROR',
'handlers': ['console'],
'propagate': False,
},
'raven': {
'level': 'DEBUG',
'handlers': ['console'],
'propagate': False,
},
'sentry.errors': {
'level': 'DEBUG',
'handlers': ['console'],
'propagate': False,
},
},
}
Expand Down Expand Up @@ -370,38 +393,19 @@
}
HOTJAR_CONFIG = {'hjid': env.int('HOTJAR_ID', default=0), 'hjsv': env.int('HOTJAR_SV', default=0), }

# Rollbar - https://rollbar.com/docs/notifier/pyrollbar/#django
ROLLBAR_CLIENT_TOKEN = env('ROLLBAR_CLIENT_TOKEN', default='') # post_client_item
ROLLBAR_SERVER_TOKEN = env('ROLLBAR_SERVER_TOKEN', default='') # post_server_item
if ROLLBAR_SERVER_TOKEN and ENV not in ['local', 'test']:
# Handle rollbar initialization.
ROLLBAR = {
'access_token': ROLLBAR_SERVER_TOKEN,
'environment': ENV,
'root': BASE_DIR,
'patch_debugview': False, # Disable debug view patching.
'branch': 'master',
'exception_level_filters': [(Http404, 'ignored')],
'capture_ip': 'anonymize',
'capture_username': True,
'scrub_fields': [
'pw', 'passwd', 'password', 'secret', 'confirm_password', 'confirmPassword', 'password_confirmation',
'passwordConfirmation', 'access_token', 'accessToken', 'auth', 'authentication', 'github_access_token',
'github_client_secret', 'secret_key', 'twitter_access_token', 'twitter_access_secret',
'twitter_consumer_secret', 'mixpanel_token', 'slack_verification_token', 'redirect_state', 'slack_token',
'priv_key',
],
# Sentry
SENTRY_USER = env('SENTRY_USER', default='')
SENTRY_PASSWORD = env('SENTRY_PASSWORD', default='')
SENTRY_ADDRESS = env('SENTRY_ADDRESS', default='')
SENTRY_PROJECT = env('SENTRY_PROJECT', default='')
SENTRY_RELEASE = raven.fetch_git_sha(os.path.abspath(os.pardir)) if SENTRY_USER else ''
if SENTRY_ADDRESS and SENTRY_PROJECT:
RAVEN_CONFIG = {
'dsn': f'https://{SENTRY_USER}:{SENTRY_PASSWORD}@{SENTRY_ADDRESS}/{SENTRY_PROJECT}',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcode {SENTRY_ADDRESS} -> sentry.io ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We aren't using the hosted solution. This gives whoever is running the app the choice of URI. We're self-hosting, since it's a fairly small footprint and we can retain data as long as necessary.

# If you are using git, you can also automatically configure the
# release based on the git info.
'release': SENTRY_RELEASE,
}
MIDDLEWARE.append('rollbar.contrib.django.middleware.RollbarNotifierMiddleware')
REST_FRAMEWORK['EXCEPTION_HANDLER'] = 'rollbar.contrib.django_rest_framework.post_exception_handler'
# LOGGING['handlers']['rollbar'] = {
# 'filters': ['require_debug_false'],
# 'access_token': ROLLBAR_SERVER_TOKEN,
# 'environment': ENV,
# 'class': 'rollbar.logger.RollbarHandler',
# }
# LOGGING['loggers']['django']['handlers'].append('rollbar')
rollbar.init(**ROLLBAR)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥


# List of github usernames to not count as comments on an issue
IGNORE_COMMENTS_FROM = ['gitcoinbot', ]
Expand Down
50 changes: 0 additions & 50 deletions app/app/templates/shared/rollbar.html

This file was deleted.

22 changes: 22 additions & 0 deletions app/app/templates/shared/sentry.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% comment %}
Copyright (C) 2018 Gitcoin Core

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
{% endcomment %}
{% if sentry_address %}
{% load raven %}
<script>
Raven.config("{% sentry_public_dsn 'https' %}").install()
</script>
{% endif %}
3 changes: 1 addition & 2 deletions app/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import geoip2.database
import requests
import rollbar
from dashboard.models import Profile
from geoip2.errors import AddressNotFoundError
from git.utils import _AUTH, HEADERS, get_user
Expand Down Expand Up @@ -119,7 +118,7 @@ def sync_profile(handle, user=None, hide_profile=True):
is_error = 'name' not in data.keys()
if is_error:
print("- error main")
rollbar.report_message('Failed to fetch github username', 'warning', extra_data=data)
logger.warning('Failed to fetch github username', exc_info=True, extra={'handle': handle})
return None

defaults = {'last_sync_date': timezone.now(), 'data': data, 'hide_profile': hide_profile, }
Expand Down
33 changes: 19 additions & 14 deletions app/app/wsgi.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
'''
Copyright (C) 2017 Gitcoin Core
# -*- coding: utf-8
"""Define the application WSGI.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Copyright (C) 2018 Gitcoin Core

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

'''
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

"""
import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")

application = get_wsgi_application()
if os.environ.get('ENV') in ['prod', 'stage']:
from raven.contrib.django.raven_compat.middleware.wsgi import Sentry
application = Sentry(get_wsgi_application())
else:
application = get_wsgi_application()
Loading