This repository was archived by the owner on Oct 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 656
/
Copy pathsanic.py
224 lines (174 loc) · 6.92 KB
/
sanic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""
raven.contrib.sanic
~~~~~~~~~~~~~~~~~~~
:copyright: (c) 2010-2018 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
from __future__ import absolute_import
import logging
import blinker
from raven.conf import setup_logging
from raven.base import Client
from raven.handlers.logging import SentryHandler
from raven.utils.compat import urlparse
from raven.utils.encoding import to_unicode
from raven.utils.conf import convert_options
raven_signals = blinker.Namespace()
logging_configured = raven_signals.signal('logging_configured')
def make_client(client_cls, app, dsn=None):
return client_cls(
**convert_options(
app.config,
defaults={
'dsn': dsn,
'include_paths': (
set(app.config.get('SENTRY_INCLUDE_PATHS', []))
| set([app.name])
),
'extra': {
'app': app,
},
},
)
)
class Sentry(object):
"""
Sanic application for Sentry.
Look up configuration from ``os.environ['SENTRY_DSN']``::
>>> sentry = Sentry(app)
Pass an arbitrary DSN::
>>> sentry = Sentry(app, dsn='http://public:[email protected]/1')
Pass an explicit client::
>>> sentry = Sentry(app, client=client)
Automatically configure logging::
>>> sentry = Sentry(app, logging=True, level=logging.ERROR)
Capture an exception::
>>> try:
>>> 1 / 0
>>> except ZeroDivisionError:
>>> sentry.captureException()
Capture a message::
>>> sentry.captureMessage('hello, world!')
"""
def __init__(self, app, client=None, client_cls=Client, dsn=None,
logging=False, logging_exclusions=None, level=logging.NOTSET):
if client and not isinstance(client, Client):
raise TypeError('client should be an instance of Client')
self.client = client
self.client_cls = client_cls
self.dsn = dsn
self.logging = logging
self.logging_exclusions = logging_exclusions
self.level = level
self.init_app(app)
def handle_exception(self, request, exception):
if not self.client:
return
try:
self.client.http_context(self.get_http_info(request))
except Exception as e:
self.client.logger.exception(to_unicode(e))
# Since Sanic is restricted to Python 3, let's be explicit with what
# we pass for exception info, rather than relying on sys.exc_info().
exception_info = (type(exception), exception, exception.__traceback__)
self.captureException(exc_info=exception_info)
def get_form_data(self, request):
return request.form
def get_http_info(self, request):
"""
Determine how to retrieve actual data by using request.mimetype.
"""
if self.is_json_type(request):
retriever = self.get_json_data
else:
retriever = self.get_form_data
return self.get_http_info_with_retriever(request, retriever)
def get_json_data(self, request):
return request.json
def get_http_info_with_retriever(self, request, retriever):
"""
Exact method for getting http_info but with form data work around.
"""
urlparts = urlparse.urlsplit(request.url)
try:
data = retriever(request)
except Exception:
data = {}
return {
'url': '{0}://{1}{2}'.format(
urlparts.scheme, urlparts.netloc, urlparts.path),
'query_string': urlparts.query,
'method': request.method,
'data': data,
'cookies': request.cookies,
'headers': request.headers,
'env': {
'REMOTE_ADDR': request.remote_addr,
}
}
def is_json_type(self, request):
content_type = request.headers.get('content-type')
return content_type == 'application/json'
def init_app(self, app, dsn=None, logging=None, level=None,
logging_exclusions=None):
if dsn is not None:
self.dsn = dsn
if level is not None:
self.level = level
if logging is not None:
self.logging = logging
if logging_exclusions is None:
self.logging_exclusions = (
'root', 'sanic.access', 'sanic.error')
else:
self.logging_exclusions = logging_exclusions
if not self.client:
self.client = make_client(self.client_cls, app, self.dsn)
if self.logging:
kwargs = {}
if self.logging_exclusions is not None:
kwargs['exclude'] = self.logging_exclusions
handler = SentryHandler(self.client, level=self.level)
setup_logging(handler, **kwargs)
logging_configured.send(
self, sentry_handler=SentryHandler, **kwargs)
if not hasattr(app, 'extensions'):
app.extensions = {}
app.extensions['sentry'] = self
app.error_handler.add(Exception, self.handle_exception)
app.register_middleware(self.before_request, attach_to='request')
app.register_middleware(self.after_request, attach_to='response')
def before_request(self, request):
self.last_event_id = None
try:
self.client.http_context(self.get_http_info(request))
except Exception as e:
self.client.logger.exception(to_unicode(e))
def after_request(self, request, response):
if self.last_event_id:
response.headers['X-Sentry-ID'] = self.last_event_id
self.client.context.clear()
def captureException(self, *args, **kwargs):
assert self.client, 'captureException called before application configured'
result = self.client.captureException(*args, **kwargs)
self.set_last_event_id_from_result(result)
return result
def captureMessage(self, *args, **kwargs):
assert self.client, 'captureMessage called before application configured'
result = self.client.captureMessage(*args, **kwargs)
self.set_last_event_id_from_result(result)
return result
def set_last_event_id_from_result(self, result):
if result:
self.last_event_id = self.client.get_ident(result)
else:
self.last_event_id = None
def user_context(self, *args, **kwargs):
assert self.client, 'user_context called before application configured'
return self.client.user_context(*args, **kwargs)
def tags_context(self, *args, **kwargs):
assert self.client, 'tags_context called before application configured'
return self.client.tags_context(*args, **kwargs)
def extra_context(self, *args, **kwargs):
assert self.client, 'extra_context called before application configured'
return self.client.extra_context(*args, **kwargs)