Skip to content

Commit 5ca0ea0

Browse files
authored
Assure Response headers are returned under all conditions (#120)
* Assure Response headers are returned under all conditions * A few more assertions
1 parent 7fed139 commit 5ca0ea0

File tree

3 files changed

+107
-28
lines changed

3 files changed

+107
-28
lines changed

instana/instrumentation/django/middleware.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ def process_exception(self, request, exception):
7575
request.iscope.span.set_tag("error", True)
7676
ec = request.iscope.span.tags.get('ec', 0)
7777
request.iscope.span.set_tag("ec", ec+1)
78-
request.iscope.close()
79-
request.iscope = None
8078

8179

8280
def load_middleware_wrapper(wrapped, instance, args, kwargs):

tests/test_django.py

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ def tearDown(self):
2626
def test_basic_request(self):
2727
with tracer.start_active_span('test'):
2828
response = self.http.request('GET', self.live_server_url + '/')
29-
# response = self.client.get('/')
3029

31-
assert_equals(response.status, 200)
30+
assert response
31+
assert_equals(200, response.status)
32+
assert('X-Instana-T' in response.headers)
33+
assert(int(response.headers['X-Instana-T'], 16))
34+
assert('X-Instana-S' in response.headers)
35+
assert(int(response.headers['X-Instana-S'], 16))
36+
assert('X-Instana-L' in response.headers)
37+
assert_equals('1', response.headers['X-Instana-L'])
3238

3339
spans = self.recorder.queued_spans()
3440
assert_equals(3, len(spans))
@@ -53,16 +59,21 @@ def test_basic_request(self):
5359
assert_equals('/', django_span.data.http.url)
5460
assert_equals('GET', django_span.data.http.method)
5561
assert_equals(200, django_span.data.http.status)
56-
assert(django_span.stack)
62+
assert django_span.stack
5763
assert_equals(2, len(django_span.stack))
5864

59-
6065
def test_request_with_error(self):
6166
with tracer.start_active_span('test'):
6267
response = self.http.request('GET', self.live_server_url + '/cause_error')
63-
# response = self.client.get('/')
6468

65-
assert_equals(response.status, 500)
69+
assert response
70+
assert_equals(500, response.status)
71+
assert('X-Instana-T' in response.headers)
72+
assert(int(response.headers['X-Instana-T'], 16))
73+
assert('X-Instana-S' in response.headers)
74+
assert(int(response.headers['X-Instana-S'], 16))
75+
assert('X-Instana-L' in response.headers)
76+
assert_equals('1', response.headers['X-Instana-L'])
6677

6778
spans = self.recorder.queued_spans()
6879
assert_equals(3, len(spans))
@@ -95,7 +106,14 @@ def test_complex_request(self):
95106
with tracer.start_active_span('test'):
96107
response = self.http.request('GET', self.live_server_url + '/complex')
97108

98-
assert_equals(response.status, 200)
109+
assert response
110+
assert_equals(200, response.status)
111+
assert('X-Instana-T' in response.headers)
112+
assert(int(response.headers['X-Instana-T'], 16))
113+
assert('X-Instana-S' in response.headers)
114+
assert(int(response.headers['X-Instana-S'], 16))
115+
assert('X-Instana-L' in response.headers)
116+
assert_equals('1', response.headers['X-Instana-L'])
99117

100118
spans = self.recorder.queued_spans()
101119
assert_equals(5, len(spans))
@@ -135,15 +153,22 @@ def test_custom_header_capture(self):
135153
# Hack together a manual custom headers list
136154
agent.extra_headers = [u'X-Capture-This', u'X-Capture-That']
137155

138-
request_headers = {}
156+
request_headers = dict()
139157
request_headers['X-Capture-This'] = 'this'
140158
request_headers['X-Capture-That'] = 'that'
141159

142160
with tracer.start_active_span('test'):
143161
response = self.http.request('GET', self.live_server_url + '/', headers=request_headers)
144162
# response = self.client.get('/')
145163

146-
assert_equals(response.status, 200)
164+
assert response
165+
assert_equals(200, response.status)
166+
assert('X-Instana-T' in response.headers)
167+
assert(int(response.headers['X-Instana-T'], 16))
168+
assert('X-Instana-S' in response.headers)
169+
assert(int(response.headers['X-Instana-S'], 16))
170+
assert('X-Instana-L' in response.headers)
171+
assert_equals('1', response.headers['X-Instana-L'])
147172

148173
spans = self.recorder.queued_spans()
149174
assert_equals(3, len(spans))
@@ -177,13 +202,21 @@ def test_custom_header_capture(self):
177202
assert_equals("that", django_span.data.custom.__dict__['tags']["http.X-Capture-That"])
178203

179204
def test_with_incoming_context(self):
180-
request_headers = {}
205+
request_headers = dict()
181206
request_headers['X-Instana-T'] = '1'
182207
request_headers['X-Instana-S'] = '1'
183208

184209
response = self.http.request('GET', self.live_server_url + '/', headers=request_headers)
185210

186-
assert_equals(response.status, 200)
211+
assert response
212+
assert_equals(200, response.status)
213+
assert('X-Instana-T' in response.headers)
214+
assert(int(response.headers['X-Instana-T'], 16))
215+
self.assertEqual('1', response.headers['X-Instana-T'])
216+
assert('X-Instana-S' in response.headers)
217+
assert(int(response.headers['X-Instana-S'], 16))
218+
assert('X-Instana-L' in response.headers)
219+
assert_equals('1', response.headers['X-Instana-L'])
187220

188221
spans = self.recorder.queued_spans()
189222
assert_equals(1, len(spans))
@@ -194,13 +227,21 @@ def test_with_incoming_context(self):
194227
assert_equals(django_span.p, 1)
195228

196229
def test_with_incoming_mixed_case_context(self):
197-
request_headers = {}
198-
request_headers['X-InSTANa-T'] = '1'
199-
request_headers['X-instana-S'] = '1'
230+
request_headers = dict()
231+
request_headers['X-InSTANa-T'] = '0000000000000001'
232+
request_headers['X-instana-S'] = '0000000000000001'
200233

201234
response = self.http.request('GET', self.live_server_url + '/', headers=request_headers)
202235

203-
assert_equals(response.status, 200)
236+
assert response
237+
assert_equals(200, response.status)
238+
assert('X-Instana-T' in response.headers)
239+
assert(int(response.headers['X-Instana-T'], 16))
240+
self.assertEqual('1', response.headers['X-Instana-T'])
241+
assert('X-Instana-S' in response.headers)
242+
assert(int(response.headers['X-Instana-S'], 16))
243+
assert('X-Instana-L' in response.headers)
244+
assert_equals('1', response.headers['X-Instana-L'])
204245

205246
spans = self.recorder.queued_spans()
206247
assert_equals(1, len(spans))

tests/test_wsgi.py

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ def test_get_request(self):
4040
urllib3_span = spans[1]
4141
test_span = spans[2]
4242

43-
assert(response)
43+
assert response
4444
self.assertEqual(200, response.status)
45+
assert('X-Instana-T' in response.headers)
46+
assert(int(response.headers['X-Instana-T'], 16))
47+
assert('X-Instana-S' in response.headers)
48+
assert(int(response.headers['X-Instana-S'], 16))
49+
assert('X-Instana-L' in response.headers)
50+
self.assertEqual('1', response.headers['X-Instana-L'])
4551

4652
# Same traceId
4753
self.assertEqual(test_span.t, urllib3_span.t)
@@ -83,8 +89,14 @@ def test_complex_request(self):
8389
urllib3_span = spans[3]
8490
test_span = spans[4]
8591

86-
assert(response)
92+
assert response
8793
self.assertEqual(200, response.status)
94+
assert('X-Instana-T' in response.headers)
95+
assert(int(response.headers['X-Instana-T'], 16))
96+
assert('X-Instana-S' in response.headers)
97+
assert(int(response.headers['X-Instana-S'], 16))
98+
assert('X-Instana-L' in response.headers)
99+
self.assertEqual('1', response.headers['X-Instana-L'])
88100

89101
# Same traceId
90102
trace_id = test_span.t
@@ -141,8 +153,14 @@ def test_custom_header_capture(self):
141153
urllib3_span = spans[1]
142154
test_span = spans[2]
143155

144-
assert(response)
156+
assert response
145157
self.assertEqual(200, response.status)
158+
assert('X-Instana-T' in response.headers)
159+
assert(int(response.headers['X-Instana-T'], 16))
160+
assert('X-Instana-S' in response.headers)
161+
assert(int(response.headers['X-Instana-S'], 16))
162+
assert('X-Instana-L' in response.headers)
163+
self.assertEqual('1', response.headers['X-Instana-L'])
146164

147165
# Same traceId
148166
self.assertEqual(test_span.t, urllib3_span.t)
@@ -190,6 +208,12 @@ def test_secret_scrubbing(self):
190208

191209
assert response
192210
self.assertEqual(200, response.status)
211+
assert('X-Instana-T' in response.headers)
212+
assert(int(response.headers['X-Instana-T'], 16))
213+
assert('X-Instana-S' in response.headers)
214+
assert(int(response.headers['X-Instana-S'], 16))
215+
assert('X-Instana-L' in response.headers)
216+
self.assertEqual('1', response.headers['X-Instana-L'])
193217

194218
# Same traceId
195219
self.assertEqual(test_span.t, urllib3_span.t)
@@ -219,13 +243,21 @@ def test_secret_scrubbing(self):
219243
self.assertEqual(2, len(wsgi_span.stack))
220244

221245
def test_with_incoming_context(self):
222-
request_headers = {}
223-
request_headers['X-Instana-T'] = '1'
224-
request_headers['X-Instana-S'] = '1'
246+
request_headers = dict()
247+
request_headers['X-Instana-T'] = '0000000000000001'
248+
request_headers['X-Instana-S'] = '0000000000000001'
225249

226250
response = self.http.request('GET', 'http://127.0.0.1:5000/', headers=request_headers)
227251

228-
self.assertEqual(response.status, 200)
252+
assert response
253+
self.assertEqual(200, response.status)
254+
assert('X-Instana-T' in response.headers)
255+
assert(int(response.headers['X-Instana-T'], 16))
256+
self.assertEqual('1', response.headers['X-Instana-T'])
257+
assert('X-Instana-S' in response.headers)
258+
assert(int(response.headers['X-Instana-S'], 16))
259+
assert('X-Instana-L' in response.headers)
260+
self.assertEqual('1', response.headers['X-Instana-L'])
229261

230262
spans = self.recorder.queued_spans()
231263
self.assertEqual(1, len(spans))
@@ -236,13 +268,21 @@ def test_with_incoming_context(self):
236268
self.assertEqual(django_span.p, 1)
237269

238270
def test_with_incoming_mixed_case_context(self):
239-
request_headers = {}
240-
request_headers['X-InSTANa-T'] = '1'
241-
request_headers['X-instana-S'] = '1'
271+
request_headers = dict()
272+
request_headers['X-InSTANa-T'] = '0000000000000001'
273+
request_headers['X-instana-S'] = '0000000000000001'
242274

243275
response = self.http.request('GET', 'http://127.0.0.1:5000/', headers=request_headers)
244276

245-
self.assertEqual(response.status, 200)
277+
assert response
278+
self.assertEqual(200, response.status)
279+
assert('X-Instana-T' in response.headers)
280+
assert(int(response.headers['X-Instana-T'], 16))
281+
self.assertEqual('1', response.headers['X-Instana-T'])
282+
assert('X-Instana-S' in response.headers)
283+
assert(int(response.headers['X-Instana-S'], 16))
284+
assert('X-Instana-L' in response.headers)
285+
self.assertEqual('1', response.headers['X-Instana-L'])
246286

247287
spans = self.recorder.queued_spans()
248288
self.assertEqual(1, len(spans))

0 commit comments

Comments
 (0)