Skip to content

logging(apis): better logging for api failures #56432

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

Merged
merged 6 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 10 additions & 8 deletions src/sentry/shared_integrations/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ def _request(
if span and existing_transaction:
span.set_data("existing_transaction", existing_transaction)

extra = {"url": full_url}
# It shouldn't be possible for integration_type to be null.
if self.integration_type:
extra[self.integration_type] = self.name

try:
with build_session() as session:
finalized_request = self.finalize_request(_prepared_request)
Expand All @@ -266,30 +271,27 @@ def _request(
return resp
resp.raise_for_status()
except RestrictedIPAddress as e:
self.track_response_data("restricted_ip_address", span, e)
self.track_response_data("restricted_ip_address", span, e, extra=extra)
self.record_error(e)
raise ApiHostError.from_exception(e) from e
except ConnectionError as e:
self.track_response_data("connection_error", span, e)
self.track_response_data("connection_error", span, e, extra=extra)
self.record_error(e)
raise ApiHostError.from_exception(e) from e
except Timeout as e:
self.track_response_data("timeout", span, e)
self.track_response_data("timeout", span, e, extra=extra)
self.record_error(e)
raise ApiTimeoutError.from_exception(e) from e
except HTTPError as e:
error_resp = e.response
if error_resp is None:
self.track_response_data("unknown", span, e)

# It shouldn't be possible for integration_type to be null.
extra = {"url": full_url}
if self.integration_type:
extra[self.integration_type] = self.name
self.logger.exception("request.error", extra=extra)
self.record_error(e)
raise ApiError("Internal Error", url=full_url) from e
self.track_response_data(error_resp.status_code, span, e)

self.track_response_data(error_resp.status_code, span, e, extra=extra)
self.record_error(e)
raise ApiError.from_response(error_resp, url=full_url) from e

Expand Down
3 changes: 3 additions & 0 deletions src/sentry/shared_integrations/track_response.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import logging
from typing import Mapping

from django.utils.functional import cached_property
from rest_framework.response import Response
Expand Down Expand Up @@ -41,6 +42,7 @@ def track_response_data(
span: Span | None = None,
error: Exception | None = None,
resp: Response | None = None,
extra: Mapping[str, str] | None = None,
) -> None:
# if no span was passed, create a dummy to which to add data to avoid having to wrap every
# span call in `if span`
Expand All @@ -58,6 +60,7 @@ def track_response_data(
span.set_status(str(code))

extra = {
**(extra or {}),
"status_string": str(code),
"error": str(error)[:256] if error else None,
}
Expand Down