forked from elastic/apm-agent-python
/
client.py
284 lines (239 loc) · 10.4 KB
/
client.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""
elasticapm.contrib.django.client
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:copyright: (c) 2011-2017 Elasticsearch
Large portions are
:copyright: (c) 2010 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
from __future__ import absolute_import
import logging
import django
from django.conf import settings as django_settings
from django.core.exceptions import DisallowedHost
from django.db import DatabaseError
from django.http import HttpRequest
from elasticapm.base import Client
from elasticapm.contrib.django.utils import iterate_with_template_sources
from elasticapm.utils import compat, encoding, get_url_dict
from elasticapm.utils.module_import import import_string
from elasticapm.utils.wsgi import get_environ, get_headers
__all__ = ('DjangoClient',)
default_client_class = 'elasticapm.contrib.django.DjangoClient'
_client = (None, None)
def get_client(client=None):
"""
Get an ElasticAPM client.
:param client:
:return:
:rtype: elasticapm.base.Client
"""
global _client
tmp_client = client is not None
if not tmp_client:
config = getattr(django_settings, 'ELASTIC_APM', {})
client = config.get('CLIENT', default_client_class)
if _client[0] != client:
client_class = import_string(client)
instance = client_class()
if not tmp_client:
_client = (client, instance)
return instance
return _client[1]
class DjangoClient(Client):
logger = logging.getLogger('elasticapm.errors.client.django')
def __init__(self, config=None, **defaults):
if config is None:
config = getattr(django_settings, 'ELASTIC_APM', {})
if 'framework_name' not in defaults:
defaults['framework_name'] = 'django'
defaults['framework_version'] = django.get_version()
super(DjangoClient, self).__init__(config, **defaults)
def get_user_info(self, request):
user_info = {}
if not hasattr(request, 'user'):
return user_info
try:
user = request.user
if hasattr(user, 'is_authenticated'):
if callable(user.is_authenticated):
user_info['is_authenticated'] = user.is_authenticated()
else:
user_info['is_authenticated'] = bool(user.is_authenticated)
if hasattr(user, 'id'):
user_info['id'] = encoding.keyword_field(user.id)
if hasattr(user, 'get_username'):
user_info['username'] = encoding.keyword_field(user.get_username())
elif hasattr(user, 'username'):
user_info['username'] = encoding.keyword_field(user.username)
if hasattr(user, 'email'):
user_info['email'] = user.email
except DatabaseError:
# If the connection is closed or similar, we'll just skip this
return {}
return user_info
def get_data_from_request(self, request, capture_body=False):
result = {
'env': dict(get_environ(request.META)),
'headers': dict(get_headers(request.META)),
'method': request.method,
'socket': {
'remote_address': request.META.get('REMOTE_ADDR'),
'encrypted': request.is_secure()
},
'cookies': dict(request.COOKIES),
}
if request.method not in ('GET', 'HEAD'):
content_type = request.META.get('CONTENT_TYPE')
if content_type == 'application/x-www-form-urlencoded':
data = compat.multidict_to_dict(request.POST)
elif content_type.startswith('multipart/form-data'):
data = compat.multidict_to_dict(request.POST)
if request.FILES:
data['_files'] = {field: file.name for field, file in compat.iteritems(request.FILES)}
else:
try:
data = request.body
except Exception:
data = '<unavailable>'
result['body'] = data if (capture_body or not data) else '[REDACTED]'
if hasattr(request, 'get_raw_uri'):
# added in Django 1.9
url = request.get_raw_uri()
else:
try:
# Requires host to be in ALLOWED_HOSTS, might throw a
# DisallowedHost exception
url = request.build_absolute_uri()
except DisallowedHost:
# We can't figure out the real URL, so we have to set it to
# DisallowedHost
result['url'] = {'full': 'DisallowedHost'}
url = None
if url:
result['url'] = get_url_dict(url)
return result
def get_data_from_response(self, response):
result = {'status_code': response.status_code}
# Django does not expose a public API to iterate over the headers of a response.
# Unfortunately, we have to access the private _headers dictionary here, which is
# a mapping of the form lower-case-header: (Original-Header, value)
if getattr(response, '_headers', {}):
result['headers'] = {key: value[1] for key, value in response._headers.items()}
return result
def capture(self, event_type, request=None, **kwargs):
if 'context' not in kwargs:
kwargs['context'] = context = {}
else:
context = kwargs['context']
is_http_request = isinstance(request, HttpRequest)
if is_http_request:
context['request'] = self.get_data_from_request(request,
capture_body=self.config.capture_body in ('all', 'errors'))
context['user'] = self.get_user_info(request)
result = super(DjangoClient, self).capture(event_type, **kwargs)
if is_http_request:
# attach the elasticapm object to the request
request._elasticapm = {
'service_name': self.config.service_name,
'id': result,
}
return result
def _get_stack_info_for_trace(self, frames,
library_frame_context_lines=None,
in_app_frame_context_lines=None,
with_locals=True,
locals_processor_func=None):
"""If the stacktrace originates within the elasticapm module, it will skip
frames until some other module comes up."""
return list(iterate_with_template_sources(
frames,
with_locals=with_locals,
library_frame_context_lines=library_frame_context_lines,
in_app_frame_context_lines=in_app_frame_context_lines,
include_paths_re=self.include_paths_re,
exclude_paths_re=self.exclude_paths_re,
locals_processor_func=locals_processor_func,
))
def send(self, url, **kwargs):
"""
Serializes and signs ``data`` and passes the payload off to ``send_remote``
If ``server`` was passed into the constructor, this will serialize the data and pipe it to
the server using ``send_remote()``.
"""
if self.config.server_url:
return super(DjangoClient, self).send(url, **kwargs)
else:
self.error_logger.error('No server configured, and elasticapm not installed. Cannot send message')
return None
class ProxyClient(object):
"""
A proxy which represents the current client at all times.
"""
# introspection support:
__members__ = property(lambda x: x.__dir__())
# Need to pretend to be the wrapped class, for the sake of objects that care
# about this (especially in equality tests)
__class__ = property(lambda x: get_client().__class__)
__dict__ = property(lambda o: get_client().__dict__)
__repr__ = lambda: repr(get_client())
__getattr__ = lambda x, o: getattr(get_client(), o)
__setattr__ = lambda x, o, v: setattr(get_client(), o, v)
__delattr__ = lambda x, o: delattr(get_client(), o)
__lt__ = lambda x, o: get_client() < o
__le__ = lambda x, o: get_client() <= o
__eq__ = lambda x, o: get_client() == o
__ne__ = lambda x, o: get_client() != o
__gt__ = lambda x, o: get_client() > o
__ge__ = lambda x, o: get_client() >= o
if compat.PY2:
__cmp__ = lambda x, o: cmp(get_client(), o) # noqa F821
__hash__ = lambda x: hash(get_client())
# attributes are currently not callable
# __call__ = lambda x, *a, **kw: get_client()(*a, **kw)
__nonzero__ = lambda x: bool(get_client())
__len__ = lambda x: len(get_client())
__getitem__ = lambda x, i: get_client()[i]
__iter__ = lambda x: iter(get_client())
__contains__ = lambda x, i: i in get_client()
__getslice__ = lambda x, i, j: get_client()[i:j]
__add__ = lambda x, o: get_client() + o
__sub__ = lambda x, o: get_client() - o
__mul__ = lambda x, o: get_client() * o
__floordiv__ = lambda x, o: get_client() // o
__mod__ = lambda x, o: get_client() % o
__divmod__ = lambda x, o: get_client().__divmod__(o)
__pow__ = lambda x, o: get_client() ** o
__lshift__ = lambda x, o: get_client() << o
__rshift__ = lambda x, o: get_client() >> o
__and__ = lambda x, o: get_client() & o
__xor__ = lambda x, o: get_client() ^ o
__or__ = lambda x, o: get_client() | o
__div__ = lambda x, o: get_client().__div__(o)
__truediv__ = lambda x, o: get_client().__truediv__(o)
__neg__ = lambda x: -(get_client())
__pos__ = lambda x: +(get_client())
__abs__ = lambda x: abs(get_client())
__invert__ = lambda x: ~(get_client())
__complex__ = lambda x: complex(get_client())
__int__ = lambda x: int(get_client())
if compat.PY2:
__long__ = lambda x: long(get_client()) # noqa F821
__float__ = lambda x: float(get_client())
__str__ = lambda x: str(get_client())
__unicode__ = lambda x: compat.text_type(get_client())
__oct__ = lambda x: oct(get_client())
__hex__ = lambda x: hex(get_client())
__index__ = lambda x: get_client().__index__()
__coerce__ = lambda x, o: x.__coerce__(x, o)
__enter__ = lambda x: x.__enter__()
__exit__ = lambda x, *a, **kw: x.__exit__(*a, **kw)
client = ProxyClient()
def _get_installed_apps_paths():
"""
Generate a list of modules in settings.INSTALLED_APPS.
"""
out = set()
for app in django_settings.INSTALLED_APPS:
out.add(app)
return out