Skip to content

Commit b3ec98c

Browse files
authored
fix handling of responses with aiohttp4 (#788)
The not-yet-released aiohttp 4.0 changes a bit how HTTP exceptions work. They are no longer inheriting from Response.
1 parent aa3b5f8 commit b3ec98c

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

elasticapm/contrib/aiohttp/middleware.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030

3131
import aiohttp
32-
from aiohttp.web import Response, middleware
32+
from aiohttp.web import HTTPException, Response, middleware
3333

3434
import elasticapm
3535
from elasticapm.conf import constants
@@ -82,13 +82,14 @@ async def handle_request(request, handler):
8282
context={"request": get_data_from_request(request, elasticapm_client.config, constants.ERROR)}
8383
)
8484
elasticapm.set_transaction_result("HTTP 5xx", override=False)
85-
if isinstance(exc, Response):
85+
elasticapm.set_context({"status_code": 500}, "response")
86+
# some exceptions are response-like, e.g. have headers and status code. Let's try and capture them
87+
if isinstance(exc, (Response, HTTPException)):
8688
elasticapm.set_context(
8789
lambda: get_data_from_response(exc, elasticapm_client.config, constants.ERROR), # noqa: F821
8890
"response",
8991
)
90-
else:
91-
elasticapm.set_context({"status_code": 500}, "response")
92+
9293
raise
9394
finally:
9495
elasticapm_client.end_transaction()

elasticapm/contrib/aiohttp/utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2828
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2929
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
from typing import Union
3031

31-
from aiohttp.web import Request, Response
32+
from aiohttp.web import HTTPException, Request, Response
3233

3334
from elasticapm.conf import Config
3435
from elasticapm.utils import compat, get_url_dict
@@ -49,11 +50,11 @@ def get_data_from_request(request: Request, config: Config, event_type: str):
4950
return result
5051

5152

52-
def get_data_from_response(response: Response, config: Config, event_type: str):
53+
def get_data_from_response(response: Union[HTTPException, Response], config: Config, event_type: str):
5354
result = {}
54-
55-
if isinstance(getattr(response, "status", None), compat.integer_types):
56-
result["status_code"] = response.status
55+
status = getattr(response, "status", getattr(response, "status_code", None))
56+
if isinstance(status, compat.integer_types):
57+
result["status_code"] = status
5758
if config.capture_headers and getattr(response, "headers", None):
5859
headers = response.headers
5960
result["headers"] = {key: ";".join(headers.getall(key)) for key in compat.iterkeys(headers)}

0 commit comments

Comments
 (0)