Skip to content

Commit e0b8c5f

Browse files
committed
fix bug with OPTIONS requests and body capturing
fixes #172
1 parent 17c3461 commit e0b8c5f

File tree

6 files changed

+29
-5
lines changed

6 files changed

+29
-5
lines changed

elasticapm/conf/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
88

99
KEYWORD_MAX_LENGTH = 1024
10+
11+
HTTP_WITH_BODY = {'POST', 'PUT', 'PATCH', 'DELETE'}

elasticapm/contrib/django/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from django.http import HttpRequest
2121

2222
from elasticapm.base import Client
23+
from elasticapm.conf import constants
2324
from elasticapm.contrib.django.utils import iterate_with_template_sources
2425
from elasticapm.utils import compat, encoding, get_url_dict
2526
from elasticapm.utils.module_import import import_string
@@ -106,11 +107,11 @@ def get_data_from_request(self, request, capture_body=False):
106107
'cookies': dict(request.COOKIES),
107108
}
108109

109-
if request.method not in ('GET', 'HEAD'):
110+
if request.method in constants.HTTP_WITH_BODY:
110111
content_type = request.META.get('CONTENT_TYPE')
111112
if content_type == 'application/x-www-form-urlencoded':
112113
data = compat.multidict_to_dict(request.POST)
113-
elif content_type.startswith('multipart/form-data'):
114+
elif content_type and content_type.startswith('multipart/form-data'):
114115
data = compat.multidict_to_dict(request.POST)
115116
if request.FILES:
116117
data['_files'] = {field: file.name for field, file in compat.iteritems(request.FILES)}

elasticapm/contrib/django/management/commands/elasticapm.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ def info(self, *args, **kwargs):
5656
self.log('info', *args, **kwargs)
5757

5858

59-
6059
CONFIG_EXAMPLE = """
6160
6261
You can set it in your settings file:

elasticapm/contrib/flask/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from werkzeug.exceptions import ClientDisconnected
22

3+
from elasticapm.conf import constants
34
from elasticapm.utils import compat, get_url_dict
45
from elasticapm.utils.wsgi import get_environ, get_headers
56

@@ -17,11 +18,11 @@ def get_data_from_request(request, capture_body=False):
1718
},
1819
'cookies': request.cookies,
1920
}
20-
if request.method not in ('GET', 'HEAD'):
21+
if request.method in constants.HTTP_WITH_BODY:
2122
body = None
2223
if request.content_type == 'application/x-www-form-urlencoded':
2324
body = compat.multidict_to_dict(request.form)
24-
elif request.content_type.startswith('multipart/form-data'):
25+
elif request.content_type and request.content_type.startswith('multipart/form-data'):
2526
body = compat.multidict_to_dict(request.form)
2627
if request.files:
2728
body['_files'] = {

tests/contrib/django/django_tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,3 +1271,15 @@ def test_capture_files(client, django_elasticapm_client):
12711271
}
12721272
else:
12731273
assert error['errors'][0]['context']['request']['body'] == '[REDACTED]'
1274+
1275+
1276+
@pytest.mark.parametrize('django_elasticapm_client', [
1277+
{'capture_body': 'transactions'},
1278+
], indirect=True)
1279+
def test_options_request(client, django_elasticapm_client):
1280+
with override_settings(**middleware_setting(django.VERSION, [
1281+
'elasticapm.contrib.django.middleware.TracingMiddleware'
1282+
])):
1283+
client.options('/')
1284+
transactions = django_elasticapm_client.instrumentation_store.get_all()
1285+
assert transactions[0]['context']['request']['method'] == 'OPTIONS'

tests/contrib/flask/flask_tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,12 @@ def test_post_files(flask_apm_client):
232232
}
233233
else:
234234
assert event['context']['request']['body'] == '[REDACTED]'
235+
236+
237+
@pytest.mark.parametrize('elasticapm_client', [
238+
{'capture_body': 'transactions'},
239+
], indirect=True)
240+
def test_options_request(flask_apm_client):
241+
resp = flask_apm_client.app.test_client().options('/')
242+
transactions = flask_apm_client.client.instrumentation_store.get_all()
243+
assert transactions[0]['context']['request']['method'] == 'OPTIONS'

0 commit comments

Comments
 (0)