Skip to content

Commit 395589b

Browse files
committed
feat(errors): report also to datadog
as alternative to sentry
1 parent a8b04a9 commit 395589b

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

request_session/request_session.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class RequestSession(object):
5454
``requests.Session`` to be used to make the HTTP requests.
5555
:param Ddtrace ddtrace: (optional) DataDog function to be used to trace, track and
5656
send metrics for individual HTTP requests.
57+
:param bool datadog_errors: (optional) If true, errors will be sent to DataDog.
58+
Defaults to ``False``.
5759
:param str ddtrace_service_name: (optional) Service name passed to DataDog DdTrace. Set to "booking_api_requests"
5860
by default.
5961
:param Statsd statsd: (optional) Datadog module to log metrics.
@@ -84,6 +86,7 @@ def __init__(
8486
user_agent=None, # type: Optional[str]
8587
user_agent_components=None, # type: Optional[UserAgentComponents]
8688
ddtrace=None, # type: Optional[Ddtrace]
89+
datadog_errors=False, # type: bool
8790
statsd=None, # type: Optional[Statsd]
8891
sentry_client=None, # type: Optional[SentryClient]
8992
logger=None, # type: Optional[Callable]
@@ -118,6 +121,7 @@ def __init__(
118121
self.user_agent_components = user_agent_components
119122
self.ddtrace = ddtrace
120123
self.ddtrace_service_name = ddtrace_service_name
124+
self.datadog_errors = datadog_errors
121125
self.statsd = statsd
122126
self.sentry_client = sentry_client
123127
self.logger = logger
@@ -404,21 +408,29 @@ def _process(
404408
else:
405409
# Client error, request is not valid and server rejected it with
406410
# http 4xx, but not timeout, there is no point in retrying.
407-
if report and self.sentry_client is not None:
411+
if report:
408412
response_text = self.get_response_text(response)
409413
extra_data = (
410414
{"response_text": response_text}
411415
if response_text != ""
412416
else None
413417
)
414-
if callable(
415-
getattr(self.sentry_client, "capture_exception", None)
416-
):
417-
self.sentry_client.capture_exception(extras=extra_data)
418-
elif callable(
419-
getattr(self.sentry_client, "captureException", None)
420-
):
421-
self.sentry_client.captureException(extra=extra_data)
418+
419+
if self.sentry_client is not None:
420+
if callable(
421+
getattr(self.sentry_client, "capture_exception", None)
422+
):
423+
self.sentry_client.capture_exception(extras=extra_data)
424+
elif callable(
425+
getattr(self.sentry_client, "captureException", None)
426+
):
427+
self.sentry_client.captureException(extra=extra_data)
428+
429+
if self.datadog_errors and self.ddtrace is not None:
430+
span = self.ddtrace.tracer.current_span()
431+
if span:
432+
span.record_exception(error, extra_data)
433+
422434

423435
if raise_for_status:
424436
raise RequestSessionException( # pylint: disable=raise-missing-from

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
setup(
1515
name="request_session",
16-
version="0.16.3",
16+
version="0.16.4",
1717
url="https://github.com/kiwicom/request-session",
1818
description="Python HTTP requests on steroids",
1919
long_description=readme,

0 commit comments

Comments
 (0)