11# (c) Copyright IBM Corp. 2021
22# (c) Copyright Instana Inc. 2020
33
4+ from typing import Generator
5+
46import pytest
57import urllib3
6- from typing import Generator
78
9+ import tests .apps .pyramid .pyramid_app # noqa: F401
10+ from instana .singletons import agent , tracer
11+ from instana .span .span import get_current_span
812from instana .util .ids import hex_id
9- import tests .apps .pyramid .pyramid_app
1013from tests .helpers import testenv
11- from instana .singletons import tracer , agent
12- from instana .span .span import get_current_span
1314
1415
1516class TestPyramid :
@@ -77,7 +78,9 @@ def test_get_request(self) -> None:
7778
7879 # wsgi
7980 assert pyramid_span .n == "wsgi"
80- assert pyramid_span .data ["http" ]["host" ] == "127.0.0.1:" + str (testenv ["pyramid_port" ])
81+ assert pyramid_span .data ["http" ]["host" ] == "127.0.0.1:" + str (
82+ testenv ["pyramid_port" ]
83+ )
8184 assert pyramid_span .data ["http" ]["url" ] == "/"
8285 assert pyramid_span .data ["http" ]["method" ] == "GET"
8386 assert pyramid_span .data ["http" ]["status" ] == 200
@@ -161,7 +164,9 @@ def test_500(self) -> None:
161164
162165 # wsgi
163166 assert pyramid_span .n == "wsgi"
164- assert pyramid_span .data ["http" ]["host" ] == "127.0.0.1:" + str (testenv ["pyramid_port" ])
167+ assert pyramid_span .data ["http" ]["host" ] == "127.0.0.1:" + str (
168+ testenv ["pyramid_port" ]
169+ )
165170 assert pyramid_span .data ["http" ]["url" ] == "/500"
166171 assert pyramid_span .data ["http" ]["method" ] == "GET"
167172 assert pyramid_span .data ["http" ]["status" ] == 500
@@ -178,6 +183,56 @@ def test_500(self) -> None:
178183 assert type (urllib3_span .stack ) is list
179184 assert len (urllib3_span .stack ) > 1
180185
186+ def test_return_error_response (self ) -> None :
187+ with tracer .start_as_current_span ("test" ):
188+ response = self .http .request (
189+ "GET" , testenv ["pyramid_server" ] + "/return_error_response"
190+ )
191+
192+ spans = self .recorder .queued_spans ()
193+ assert len (spans ) == 3
194+
195+ pyramid_span = spans [0 ]
196+
197+ assert response .status == 500
198+
199+ assert pyramid_span .n == "wsgi"
200+ assert pyramid_span .data ["http" ]["host" ] == "127.0.0.1:" + str (
201+ testenv ["pyramid_port" ]
202+ )
203+ assert pyramid_span .data ["http" ]["url" ] == "/return_error_response"
204+ assert pyramid_span .data ["http" ]["method" ] == "GET"
205+ assert pyramid_span .data ["http" ]["status" ] == 500
206+ assert pyramid_span .data ["http" ]["error" ] == "b'Error'"
207+ assert pyramid_span .data ["http" ]["path_tpl" ] == "/return_error_response"
208+
209+ assert pyramid_span .ec == 1
210+
211+ def test_fail_with_http_exception (self ) -> None :
212+ with tracer .start_as_current_span ("test" ):
213+ response = self .http .request (
214+ "GET" , testenv ["pyramid_server" ] + "/fail_with_http_exception"
215+ )
216+
217+ spans = self .recorder .queued_spans ()
218+ assert len (spans ) == 3
219+
220+ pyramid_span = spans [0 ]
221+
222+ assert response .status == 520
223+
224+ assert pyramid_span .n == "wsgi"
225+ assert pyramid_span .data ["http" ]["host" ] == "127.0.0.1:" + str (
226+ testenv ["pyramid_port" ]
227+ )
228+ assert pyramid_span .data ["http" ]["url" ] == "/fail_with_http_exception"
229+ assert pyramid_span .data ["http" ]["method" ] == "GET"
230+ assert pyramid_span .data ["http" ]["status" ] == 520
231+ assert pyramid_span .data ["http" ]["error" ] == "bad request"
232+ assert pyramid_span .data ["http" ]["path_tpl" ] == "/fail_with_http_exception"
233+
234+ assert pyramid_span .ec == 1
235+
181236 def test_exception (self ) -> None :
182237 with tracer .start_as_current_span ("test" ):
183238 response = self .http .request (
@@ -211,7 +266,9 @@ def test_exception(self) -> None:
211266
212267 # wsgi
213268 assert pyramid_span .n == "wsgi"
214- assert pyramid_span .data ["http" ]["host" ] == "127.0.0.1:" + str (testenv ["pyramid_port" ])
269+ assert pyramid_span .data ["http" ]["host" ] == "127.0.0.1:" + str (
270+ testenv ["pyramid_port" ]
271+ )
215272 assert pyramid_span .data ["http" ]["url" ] == "/exception"
216273 assert pyramid_span .data ["http" ]["method" ] == "GET"
217274 assert pyramid_span .data ["http" ]["status" ] == 500
@@ -270,7 +327,9 @@ def test_response_header_capture(self) -> None:
270327
271328 # wsgi
272329 assert pyramid_span .n == "wsgi"
273- assert pyramid_span .data ["http" ]["host" ] == "127.0.0.1:" + str (testenv ["pyramid_port" ])
330+ assert pyramid_span .data ["http" ]["host" ] == "127.0.0.1:" + str (
331+ testenv ["pyramid_port" ]
332+ )
274333 assert pyramid_span .data ["http" ]["url" ] == "/response_headers"
275334 assert pyramid_span .data ["http" ]["method" ] == "GET"
276335 assert pyramid_span .data ["http" ]["status" ] == 200
@@ -341,7 +400,9 @@ def test_request_header_capture(self) -> None:
341400
342401 # wsgi
343402 assert pyramid_span .n == "wsgi"
344- assert pyramid_span .data ["http" ]["host" ] == "127.0.0.1:" + str (testenv ["pyramid_port" ])
403+ assert pyramid_span .data ["http" ]["host" ] == "127.0.0.1:" + str (
404+ testenv ["pyramid_port" ]
405+ )
345406 assert pyramid_span .data ["http" ]["url" ] == "/"
346407 assert pyramid_span .data ["http" ]["method" ] == "GET"
347408 assert pyramid_span .data ["http" ]["status" ] == 200
@@ -419,7 +480,9 @@ def test_scrub_secret_path_template(self) -> None:
419480
420481 # wsgi
421482 assert pyramid_span .n == "wsgi"
422- assert pyramid_span .data ["http" ]["host" ] == "127.0.0.1:" + str (testenv ["pyramid_port" ])
483+ assert pyramid_span .data ["http" ]["host" ] == "127.0.0.1:" + str (
484+ testenv ["pyramid_port" ]
485+ )
423486 assert pyramid_span .data ["http" ]["url" ] == "/hello_user/oswald"
424487 assert pyramid_span .data ["http" ]["method" ] == "GET"
425488 assert pyramid_span .data ["http" ]["status" ] == 200
0 commit comments