Skip to content

Commit c70c06a

Browse files
committed
Replaced raven with sentry-sdk
1 parent 2e37a72 commit c70c06a

File tree

9 files changed

+115
-37
lines changed

9 files changed

+115
-37
lines changed

src/fluentdemo/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
import raven.exceptions
2-
3-
try:
4-
version_sha = raven.fetch_git_sha('..')
5-
except raven.exceptions.InvalidGitRepository:
6-
version_sha = None # for unit testing
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default_app_config = __name__ + ".apps.BaseAppConfig"

src/fluentdemo/apps/sentry/apps.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from django.apps import AppConfig
2+
3+
from .sentry import init_sentry_sdk
4+
5+
6+
class BaseAppConfig(AppConfig):
7+
name = 'fluentdemo.apps.sentry'
8+
9+
def ready(self):
10+
"""
11+
Monkey patch after apps are loaded.
12+
"""
13+
# Moved SDK initialization to ready() moment,
14+
# so SENTRY_DSN can be redefined by other setting files.
15+
init_sentry_sdk()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import logging
2+
3+
import sentry_sdk
4+
from django.conf import settings
5+
from sentry_sdk.integrations.django import DjangoIntegration
6+
from sentry_sdk.integrations.logging import LoggingIntegration
7+
from sentry_sdk.integrations.redis import RedisIntegration
8+
9+
10+
def init_sentry_sdk():
11+
"""Initialize the sentry SDK"""
12+
if settings.SENTRY_DSN:
13+
sentry_sdk.init(
14+
settings.SENTRY_DSN,
15+
environment=settings.SENTRY_ENVIRONMENT,
16+
release=settings.GIT_VERSION,
17+
integrations=[
18+
LoggingIntegration(event_level=logging.WARNING),
19+
DjangoIntegration(transaction_style="function_name"),
20+
RedisIntegration(),
21+
]
22+
)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Copied from old sentry-raven integration.
3+
"""
4+
import os.path
5+
6+
7+
class InvalidGitRepository(Exception):
8+
pass
9+
10+
11+
def fetch_git_sha(path, head=None):
12+
"""
13+
>>> fetch_git_sha(os.path.dirname(__file__))
14+
"""
15+
if not head:
16+
head_path = os.path.join(path, '.git', 'HEAD')
17+
if not os.path.exists(head_path):
18+
raise InvalidGitRepository(
19+
'Cannot identify HEAD for git repository at %s' % (path,))
20+
21+
with open(head_path, 'r') as fp:
22+
head = str(fp.read()).strip()
23+
24+
if head.startswith('ref: '):
25+
head = head[5:]
26+
revision_file = os.path.join(
27+
path, '.git', *head.split('/')
28+
)
29+
else:
30+
return head
31+
else:
32+
revision_file = os.path.join(path, '.git', 'refs', 'heads', head)
33+
34+
if not os.path.exists(revision_file):
35+
if not os.path.exists(os.path.join(path, '.git')):
36+
raise InvalidGitRepository(
37+
'%s does not seem to be the root of a git repository' % (path,))
38+
39+
# Check for our .git/packed-refs' file since a `git gc` may have run
40+
# https://git-scm.com/book/en/v2/Git-Internals-Maintenance-and-Data-Recovery
41+
packed_file = os.path.join(path, '.git', 'packed-refs')
42+
if os.path.exists(packed_file):
43+
with open(packed_file) as fh:
44+
for line in fh:
45+
line = line.rstrip()
46+
if line and line[:1] not in ('#', '^'):
47+
try:
48+
revision, ref = line.split(' ', 1)
49+
except ValueError:
50+
continue
51+
if ref == head:
52+
return str(revision)
53+
54+
raise InvalidGitRepository(
55+
'Unable to find ref to head "%s" in repository' % (head,))
56+
57+
with open(revision_file) as fh:
58+
return str(fh.read()).strip()

src/fluentdemo/settings/defaults.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"""
44
import logging
55
import os
6+
import sys
67

78
import environ
8-
import raven.exceptions
99
from django.utils.translation import ugettext_lazy as _
10+
from fluentdemo.apps.sentry.versioning import InvalidGitRepository, fetch_git_sha
1011

1112
env = environ.Env()
1213

@@ -88,6 +89,7 @@
8889
# Site parts
8990
'frontend',
9091
'fluentdemo.apps.blog',
92+
'fluentdemo.apps.sentry',
9193
'fluentdemo.apps.wysiwyg_config',
9294

9395
# CMS parts
@@ -176,8 +178,6 @@
176178
)
177179

178180
MIDDLEWARE = (
179-
'raven.contrib.django.middleware.SentryMiddleware', # make 'request' available on all logs.
180-
'raven.contrib.django.middleware.Sentry404CatchMiddleware', # on 404, report to sentry.
181181
'django.middleware.security.SecurityMiddleware',
182182
'whitenoise.middleware.WhiteNoiseMiddleware',
183183
'django.middleware.common.CommonMiddleware',
@@ -234,15 +234,14 @@
234234

235235
locals().update(env.email_url(default='smtp://'))
236236

237-
RAVEN_CONFIG = {
238-
'dsn': env.str('SENTRY_DSN', default=''),
239-
}
240-
241237
try:
242-
GIT_VERSION = raven.fetch_git_sha('..')
243-
RAVEN_CONFIG['release'] = GIT_VERSION
244-
except raven.exceptions.InvalidGitRepository:
245-
pass
238+
GIT_VERSION = fetch_git_sha(ROOT_DIR)
239+
except InvalidGitRepository as e:
240+
sys.stderr.write(str(e) + "\n")
241+
GIT_VERSION = None
242+
243+
SENTRY_DSN = env.str('SENTRY_DSN', default='')
244+
SENTRY_ENVIRONMENT = env.str('SENTRY_ENVIRONMENT', default='dev')
246245

247246
LOGGING = {
248247
'version': 1,
@@ -266,10 +265,6 @@
266265
'filters': ['require_debug_false'],
267266
'class': 'django.utils.log.AdminEmailHandler'
268267
},
269-
'sentry': {
270-
'level': 'WARNING',
271-
'class': 'raven.contrib.django.handlers.SentryHandler',
272-
},
273268
'console': {
274269
'level': 'DEBUG',
275270
'class': 'logging.StreamHandler',
@@ -286,11 +281,6 @@
286281
'level': 'ERROR',
287282
'propagate': True,
288283
},
289-
'raven': {
290-
'level': 'DEBUG',
291-
'handlers': ['console'],
292-
'propagate': False,
293-
},
294284
}
295285
}
296286

src/fluentdemo/settings/production.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@
1313
FLUENT_CONTENTS_CACHE_OUTPUT = env.bool('FLUENT_CONTENTS_CACHE_OUTPUT', not DEBUG)
1414
FLUENT_CONTENTS_CACHE_PLACEHOLDER_OUTPUT = env.bool('FLUENT_CONTENTS_CACHE_PLACEHOLDER_OUTPUT', not DEBUG)
1515

16-
# Raven is only enabled in production to avoid warnings in development
17-
INSTALLED_APPS += (
18-
'raven.contrib.django.raven_compat',
19-
#'gunicorn',
20-
)
21-
2216
# Keep templates in memory
2317
TEMPLATES[0]['OPTIONS']['loaders'] = (
2418
('django.template.loaders.cached.Loader', TEMPLATES[0]['OPTIONS']['loaders']),

src/requirements/base.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Django == 1.11.29
55
Pillow == 7.0.0
66
psycopg2 == 2.8.4
77
python-memcached == 1.59
8-
raven == 6.10.0
8+
sentry-sdk == 1.3.1
99
uWSGI == 2.0.18
1010
WsgiUnproxy == 1.0
1111

src/requirements/base.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ beautifulsoup4==4.8.2
1515
brotlipy==0.7.0
1616
# via -r requirements/base.in
1717
certifi==2019.11.28
18-
# via requests
18+
# via
19+
# requests
20+
# sentry-sdk
1921
cffi==1.14.0
2022
# via brotlipy
2123
chardet==3.0.4
@@ -308,8 +310,6 @@ pytz==2019.3
308310
# -r requirements/base.in
309311
# babel
310312
# django
311-
raven==6.10.0
312-
# via -r requirements/base.in
313313
redis==3.4.1
314314
# via
315315
# -r requirements/base.in
@@ -320,6 +320,8 @@ requests==2.23.0
320320
# via
321321
# django-healthchecks
322322
# python-akismet
323+
sentry-sdk==1.3.1
324+
# via -r requirements/base.in
323325
six==1.14.0
324326
# via
325327
# django-appconf
@@ -353,7 +355,9 @@ unicodecsv==0.14.1
353355
unidecode==1.1.1
354356
# via django-forms-builder
355357
urllib3==1.25.8
356-
# via requests
358+
# via
359+
# requests
360+
# sentry-sdk
357361
uwsgi==2.0.18
358362
# via -r requirements/base.in
359363
wcwidth==0.1.8

0 commit comments

Comments
 (0)