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

fix bug with OPTIONS requests and body capturing #174

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 0 deletions elasticapm/conf/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'

KEYWORD_MAX_LENGTH = 1024

HTTP_WITH_BODY = {'POST', 'PUT', 'PATCH', 'DELETE'}
5 changes: 3 additions & 2 deletions elasticapm/contrib/django/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from django.http import HttpRequest

from elasticapm.base import Client
from elasticapm.conf import constants
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
Expand Down Expand Up @@ -106,11 +107,11 @@ def get_data_from_request(self, request, capture_body=False):
'cookies': dict(request.COOKIES),
}

if request.method not in ('GET', 'HEAD'):
if request.method in constants.HTTP_WITH_BODY:
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'):
elif content_type and 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)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def info(self, *args, **kwargs):
self.log('info', *args, **kwargs)



CONFIG_EXAMPLE = """

You can set it in your settings file:
Expand Down
5 changes: 3 additions & 2 deletions elasticapm/contrib/flask/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from werkzeug.exceptions import ClientDisconnected

from elasticapm.conf import constants
from elasticapm.utils import compat, get_url_dict
from elasticapm.utils.wsgi import get_environ, get_headers

Expand All @@ -17,11 +18,11 @@ def get_data_from_request(request, capture_body=False):
},
'cookies': request.cookies,
}
if request.method not in ('GET', 'HEAD'):
if request.method in constants.HTTP_WITH_BODY:
body = None
if request.content_type == 'application/x-www-form-urlencoded':
body = compat.multidict_to_dict(request.form)
elif request.content_type.startswith('multipart/form-data'):
elif request.content_type and request.content_type.startswith('multipart/form-data'):
body = compat.multidict_to_dict(request.form)
if request.files:
body['_files'] = {
Expand Down
12 changes: 12 additions & 0 deletions tests/contrib/django/django_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1271,3 +1271,15 @@ def test_capture_files(client, django_elasticapm_client):
}
else:
assert error['errors'][0]['context']['request']['body'] == '[REDACTED]'


@pytest.mark.parametrize('django_elasticapm_client', [
{'capture_body': 'transactions'},
], indirect=True)
def test_options_request(client, django_elasticapm_client):
with override_settings(**middleware_setting(django.VERSION, [
'elasticapm.contrib.django.middleware.TracingMiddleware'
])):
client.options('/')
transactions = django_elasticapm_client.instrumentation_store.get_all()
assert transactions[0]['context']['request']['method'] == 'OPTIONS'
9 changes: 9 additions & 0 deletions tests/contrib/flask/flask_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,12 @@ def test_post_files(flask_apm_client):
}
else:
assert event['context']['request']['body'] == '[REDACTED]'


@pytest.mark.parametrize('elasticapm_client', [
{'capture_body': 'transactions'},
], indirect=True)
def test_options_request(flask_apm_client):
resp = flask_apm_client.app.test_client().options('/')
transactions = flask_apm_client.client.instrumentation_store.get_all()
assert transactions[0]['context']['request']['method'] == 'OPTIONS'