Skip to content
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

feat: Use custom exceptions for recording errors. #193

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ Change Log
Unreleased
**********

[5.4.0] - 2023-08-28
********************
Changed
=======
* Changed ordering of certain context assignments in producer code.
* Adds custom exceptions for producing and consuming errors.

[5.3.1] - 2023-08-10
********************
Expand Down
2 changes: 1 addition & 1 deletion edx_event_bus_kafka/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
from edx_event_bus_kafka.internal.consumer import KafkaEventConsumer
from edx_event_bus_kafka.internal.producer import KafkaEventProducer, create_producer

__version__ = '5.3.1'
__version__ = '5.4.0'
10 changes: 8 additions & 2 deletions edx_event_bus_kafka/internal/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ def __init__(self, message: str, causes: list):
self.causes = causes # just used for testing


class EventConsumptionException(Exception):
"""
Indicates that we had an issue in event production. Useful for filtering on later.
"""


def _reconnect_to_db_if_needed():
"""
Reconnects the db connection if needed.
Expand Down Expand Up @@ -513,8 +519,8 @@ def record_event_consuming_error(self, run_context, error, maybe_message):
try:
# This is gross, but our record_exception wrapper doesn't take args at the moment,
# and will only read the exception from stack context.
raise Exception(error)
except BaseException:
raise EventConsumptionException(error)
except EventConsumptionException:
self._add_message_monitoring(run_context=run_context, message=maybe_kafka_message, error=error)
record_exception()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So will this end up recording the EventConsumptionException, or will it record the type of error?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Well, I guess if we lose the type of error, that's already happening, so no complaint here.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we were losing the type of error previously. This allows us to at least see a EventConsumptionException recorded as the error.class in New Relic.

logger.exception(
Expand Down
8 changes: 6 additions & 2 deletions edx_event_bus_kafka/internal/producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
confluent_kafka = None


class EventProductionException(Exception):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these live in a public module?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it needs to, since this is custom to the producing code, and we already had the pattern of keeping consumer exceptions in the consumer.py file. I am open to disagreements, though.

""" An exception we can check for when errors occur in event production code. """


def record_producing_error(error, context):
"""
Record an error in producing an event to both the monitoring system and the regular logs
Expand All @@ -49,8 +53,8 @@ def record_producing_error(error, context):
try:
# record_exception() is a wrapper around a New Relic method that can only be called within an except block,
# so first re-raise the error
raise Exception(error)
except BaseException:
raise EventProductionException(error)
except EventProductionException:
record_exception()
logger.exception(f"Error delivering message to Kafka event bus. {error=!s} {context!r}")

Expand Down