From 60162498c8c91271aeedad182f22476a745216bc Mon Sep 17 00:00:00 2001 From: Kevin Michel Date: Wed, 26 Feb 2020 09:43:18 +0100 Subject: [PATCH 1/8] import django reverse from django.urls The previous location, django.core.urlresolvers, was deprecated in 1.10 and removed in 2.0 --- tests/ext/django/test_middleware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ext/django/test_middleware.py b/tests/ext/django/test_middleware.py index 3c89c51e..926b6f81 100644 --- a/tests/ext/django/test_middleware.py +++ b/tests/ext/django/test_middleware.py @@ -1,6 +1,6 @@ import django from aws_xray_sdk import global_sdk_config -from django.core.urlresolvers import reverse +from django.urls import reverse from django.test import TestCase from aws_xray_sdk.core import xray_recorder, lambda_launcher From 9a115879bb9f450b42a3bf533449838fe3146471 Mon Sep 17 00:00:00 2001 From: Kevin Michel Date: Wed, 26 Feb 2020 09:44:44 +0100 Subject: [PATCH 2/8] Remove django SessionAuthenticationMiddleware This middleware was redundant in Django 1.10 and removed in Django 2.0 --- tests/ext/django/app/settings.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/ext/django/app/settings.py b/tests/ext/django/app/settings.py index 6a78b6d6..3d33737d 100644 --- a/tests/ext/django/app/settings.py +++ b/tests/ext/django/app/settings.py @@ -44,7 +44,6 @@ 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.security.SecurityMiddleware', From 3143be9d6971543687f5135241efb5e154024841 Mon Sep 17 00:00:00 2001 From: Kevin Michel Date: Wed, 26 Feb 2020 09:49:41 +0100 Subject: [PATCH 3/8] Fix #168: Subsegment name serialization causing MissingName error Django sometimes uses SafeString instead of str for template names. Jsonpickle serializes SafeString as `{ "py/newargs": ["django/forms/widgets/attrs.html"]}` instead of `"django/forms/widgets/attrs.html" which is the reason for the invalid subsegment name. --- aws_xray_sdk/ext/django/templates.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/aws_xray_sdk/ext/django/templates.py b/aws_xray_sdk/ext/django/templates.py index 1eaa7736..a77f766b 100644 --- a/aws_xray_sdk/ext/django/templates.py +++ b/aws_xray_sdk/ext/django/templates.py @@ -1,6 +1,7 @@ import logging from django.template import Template +from django.utils.safestring import SafeString from aws_xray_sdk.core import xray_recorder @@ -22,6 +23,10 @@ def xray_render(self, context): template_name = self.name or getattr(context, 'template_name', None) if template_name: name = str(template_name) + # SafeString are not properly serialized by jsonpickle, + # turn them back to str by adding a non-safe str. + if isinstance(name, SafeString): + name += '' subsegment = xray_recorder.current_subsegment() if subsegment: subsegment.name = name From 7b89df33b56c771d1c60d671f46f2dc114915d22 Mon Sep 17 00:00:00 2001 From: Kevin Michel Date: Thu, 5 Mar 2020 14:10:16 +0100 Subject: [PATCH 4/8] Add django tests for template names as SafeString --- tests/ext/django/app/templates/block.html | 1 + tests/ext/django/app/templates/block_user.html | 10 ++++++++++ tests/ext/django/app/views.py | 5 +++++ tests/ext/django/test_middleware.py | 11 +++++++++++ 4 files changed, 27 insertions(+) create mode 100644 tests/ext/django/app/templates/block.html create mode 100644 tests/ext/django/app/templates/block_user.html diff --git a/tests/ext/django/app/templates/block.html b/tests/ext/django/app/templates/block.html new file mode 100644 index 00000000..cb179d16 --- /dev/null +++ b/tests/ext/django/app/templates/block.html @@ -0,0 +1 @@ +

Hello World

diff --git a/tests/ext/django/app/templates/block_user.html b/tests/ext/django/app/templates/block_user.html new file mode 100644 index 00000000..bfa2c370 --- /dev/null +++ b/tests/ext/django/app/templates/block_user.html @@ -0,0 +1,10 @@ + + + + +

Django Test App

+ +{% include "block.html" %} + + + diff --git a/tests/ext/django/app/views.py b/tests/ext/django/app/views.py index 6bb5edf9..1b3b6f62 100644 --- a/tests/ext/django/app/views.py +++ b/tests/ext/django/app/views.py @@ -9,6 +9,10 @@ class IndexView(TemplateView): template_name = 'index.html' +class TemplateBlockView(TemplateView): + template_name = 'block_user.html' + + def ok(request): return HttpResponse(status=200) @@ -32,4 +36,5 @@ def call_db(request): url(r'^500fault/$', fault, name='500fault'), url(r'^call_db/$', call_db, name='call_db'), url(r'^template/$', IndexView.as_view(), name='template'), + url(r'^template_block/$', TemplateBlockView.as_view(), name='template_block'), ] diff --git a/tests/ext/django/test_middleware.py b/tests/ext/django/test_middleware.py index 926b6f81..8388b386 100644 --- a/tests/ext/django/test_middleware.py +++ b/tests/ext/django/test_middleware.py @@ -90,6 +90,17 @@ def test_template(self): assert not subsegment.in_progress assert subsegment.namespace == 'local' + def test_template_block(self): + url = reverse('template_block') + self.client.get(url) + segment = xray_recorder.emitter.pop() + assert len(segment.subsegments) == 1 + + subsegment = segment.subsegments[0] + assert subsegment.name == 'block_user.html' + assert not subsegment.in_progress + assert subsegment.namespace == 'local' + def test_trace_header_data_perservation(self): url = reverse('200ok') self.client.get(url, HTTP_X_AMZN_TRACE_ID='k1=v1') From d78bac1982a89611590f1f1f0185f02835ac3fe0 Mon Sep 17 00:00:00 2001 From: Kevin Michel Date: Thu, 5 Mar 2020 14:11:20 +0100 Subject: [PATCH 5/8] Enable tests on Django 2.2 (LTS) and Django 3.0 (latest) --- tox.ini | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tox.ini b/tox.ini index d829182d..017f9a64 100644 --- a/tox.ini +++ b/tox.ini @@ -1,7 +1,9 @@ [tox] envlist = - py{27,34,35,36,37,38} + py{27,34,35,36,37,38}-django1 py{35,36,37,38}-aiohttp2 + py{35,36,37,38}-django2 + py{36,37,38}-django3 coverage-report skip_missing_interpreters = True @@ -19,7 +21,9 @@ deps = Flask-SQLAlchemy future # the sdk doesn't support earlier version of django - django >= 1.10, <2.0 + django1: django >= 1.10, <2.0 + django2: django >= 2.2, <2.3 + django3: django >= 3.0, <4.0 django-fake-model pynamodb >= 3.3.1 psycopg2 From 89253412f5e9f0d261c0faf7995e43fdbfeb4331 Mon Sep 17 00:00:00 2001 From: Kevin Michel Date: Thu, 5 Mar 2020 16:36:42 +0100 Subject: [PATCH 6/8] Use Django 3.0 when django factor isn't specified in tox environment --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 017f9a64..f0cb7362 100644 --- a/tox.ini +++ b/tox.ini @@ -23,7 +23,7 @@ deps = # the sdk doesn't support earlier version of django django1: django >= 1.10, <2.0 django2: django >= 2.2, <2.3 - django3: django >= 3.0, <4.0 + !django1-!django2: django >= 3.0, <4.0 django-fake-model pynamodb >= 3.3.1 psycopg2 From 18b3fc3ef6b00511b69e036821dcc69778f5d726 Mon Sep 17 00:00:00 2001 From: Kevin Michel Date: Thu, 5 Mar 2020 16:48:29 +0100 Subject: [PATCH 7/8] Use the latest compatible Django for each python unless specified otherwise --- tox.ini | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tox.ini b/tox.ini index f0cb7362..15627420 100644 --- a/tox.ini +++ b/tox.ini @@ -21,9 +21,9 @@ deps = Flask-SQLAlchemy future # the sdk doesn't support earlier version of django - django1: django >= 1.10, <2.0 - django2: django >= 2.2, <2.3 - !django1-!django2: django >= 3.0, <4.0 + py{27,34}-!django2-!django3,django1: django >= 1.10, <2.0 + py{35}-!django1-!django3,django2: django >= 2.2, <2.3 + py{36,37,38}-!django1-!django2,django3: django >= 3.0, <4.0 django-fake-model pynamodb >= 3.3.1 psycopg2 From 16a00d7040f7c1f36ab086461496130b90c75f8e Mon Sep 17 00:00:00 2001 From: Kevin Michel Date: Fri, 6 Mar 2020 11:06:55 +0100 Subject: [PATCH 8/8] Simplify Django version selection in tox --- tox.ini | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tox.ini b/tox.ini index 15627420..6039e008 100644 --- a/tox.ini +++ b/tox.ini @@ -1,9 +1,7 @@ [tox] envlist = - py{27,34,35,36,37,38}-django1 + py{27,34,35,36,37,38} py{35,36,37,38}-aiohttp2 - py{35,36,37,38}-django2 - py{36,37,38}-django3 coverage-report skip_missing_interpreters = True @@ -21,9 +19,7 @@ deps = Flask-SQLAlchemy future # the sdk doesn't support earlier version of django - py{27,34}-!django2-!django3,django1: django >= 1.10, <2.0 - py{35}-!django1-!django3,django2: django >= 2.2, <2.3 - py{36,37,38}-!django1-!django2,django3: django >= 3.0, <4.0 + django >= 1.10 django-fake-model pynamodb >= 3.3.1 psycopg2