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

RabbitMQ Healthcheck Contribution #197

Merged
merged 11 commits into from
Jul 30, 2018
Merged

RabbitMQ Healthcheck Contribution #197

merged 11 commits into from
Jul 30, 2018

Conversation

ipeternella
Copy link
Contributor

This pull request contains the following implementations:

  • A class to perform the healthcheck of a RabbitMQ service;
  • Tests for the RabbitMQ Healthchecker;
  • An updated README.rst with the instructions on how to configure the RabbitMQ healthchecker.

Copy link
Collaborator

@codingjoe codingjoe left a comment

Choose a reason for hiding this comment

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

Hi @IgooorGP great work! Really appreciate the effort. Especially about including tests!

I do have some comments. Please also make sure to add documentation.

.gitignore Outdated
@@ -102,3 +102,8 @@ ENV/

# pytest
.pytest_cache/

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add editor specific ignores to you global git ignore. Not to a repository.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello there @codingjoe! I have removed these files to a global git ignore. Sorry for this mistake.


def ready(self):
from .backends import RabbitMQHealthCheck
plugin_dir.register(RabbitMQHealthCheck)
Copy link
Collaborator

Choose a reason for hiding this comment

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

missing newline

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

class RabbitMQHealthCheck(BaseHealthCheckBackend):
"""
Healthchecker for RabbitMQ.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please drop trailing blank line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

def __get_broker_url(self):
"""
Gets the broker url based on django.confg.settings dictionary.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please drop trailing blank line

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 have removed this method due to the mentioned complexity of the configuration.

"""
broker_url = getattr(settings, "BROKER_URL", None)

if broker_url is None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have the feeling this drives complexity. I'd rather have only one approach on how to configure this correctly.

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 have removed this method due to the mentioned complexity. Right now, the RabbitMQ healthchecker only looks for a BROKER_URL variable on django settings dict.


logger.info("Got broker_url: %s. Creating a connection to the broker...", broker_url)

conn = pika.BlockingConnection(pika.URLParameters(broker_url))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am not so sure about pika. Most people will use celery and it's amqplib fork.

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 have removed pika from the dependencies. Instead of it, I use Kombu which is a dependency of the Celery package and therefore comes together with Celery! Kombu uses pyamqp by default according to its docs http://docs.celeryproject.org/projects/kombu/en/latest/userguide/connections.html

logger.info("RabbitMQ is healthy.")

except ConnectionClosed as e:
self.add_error(ServiceUnavailable("Unable to connect to RabbitMQ: Connection refused."), e)
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can also just use raise ServiceUnavailable("your message") from e

@@ -9,3 +9,4 @@ pydocstyle
pep8-naming
pytest
pytest-django
pika
Copy link
Collaborator

Choose a reason for hiding this comment

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

yet another dependency...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed pika dependency.

setup.cfg Outdated
@@ -35,6 +35,9 @@ norecursedirs=venv env .eggs
DJANGO_SETTINGS_MODULE=tests.testapp.settings
addopts = --tb=short -rxs

[pep8]
max-line-length = 119
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unrelated change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this unnecessary change.



if __name__ == "__main__":
unittest.main()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Kudos for writing tests. I really appreciate it. But we use pytest as a test runner.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed from the tests.

@ipeternella
Copy link
Contributor Author

ipeternella commented Jul 18, 2018

Hello @codingjoe!

Thanks for the code review. I have implemented the requested changes. Please, could you take another look to see if it is acceptable now? Should you have any comments or new requests, please do so.

Thanks a lot!

Copy link
Collaborator

@codingjoe codingjoe left a comment

Choose a reason for hiding this comment

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

Almost there! Just some small changes and this is ready to roll. Thanks 👍

broker_url = getattr(settings, "BROKER_URL", None)

try:
logger.info("Got %s as the broker_url. Connecting to rabbit...")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Argument for %s is missing

broker_url = getattr(settings, "BROKER_URL", None)

try:
logger.info("Got %s as the broker_url. Connecting to rabbit...")
Copy link
Collaborator

Choose a reason for hiding this comment

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

please use debug level here

conn = Connection(broker_url)

# opens and closes a channel to rabbitmq to make sure the service is up
logger.info("Openning a channel to RabbitMQ...")
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here, debug please

conn.channel()

logger.info("Closing the channel to RabbitMQ...")
conn.close()
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there a context manager you could use? Even if something fails the connection should be closed


broker_url = getattr(settings, "BROKER_URL", None)

try:
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is too much happening in the try block. Please be more narrow, where you expect the exception to occour.

Checks if RabbitMQ service is up by opening and closing
a broker channel.
"""
logger.info("Checking for a broker_url on django settings...")
Copy link
Collaborator

Choose a reason for hiding this comment

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

debug please

from health_check.contrib.rabbitmq.backends import RabbitMQHealthCheck


class TestRabbitMQHealthCheck(unittest.TestCase):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again, we don't use UnitTest but pytest. Please just use a call and switch the assert to simple assert instead of self.assertEqual and co

@ipeternella
Copy link
Contributor Author

Hey there @codingjoe ! Thanks again for the review. I have changed the code to use the kombu connection as a context to release opened resources. Also, I decided to use the connect method of the kombu library for the healthcheck because it is easier to understand, produces the same result as before and solves the problem with too many statements in the try/except blocks. Oh, of course, I have changed the tests as well. Please, could you review the code again to make sure everything's all right?

Thanks a lot!

@codecov-io
Copy link

codecov-io commented Jul 30, 2018

Codecov Report

Merging #197 into master will decrease coverage by 1.12%.
The diff coverage is 63.63%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #197      +/-   ##
==========================================
- Coverage   75.39%   74.27%   -1.13%     
==========================================
  Files          29       32       +3     
  Lines         313      346      +33     
==========================================
+ Hits          236      257      +21     
- Misses         77       89      +12
Impacted Files Coverage Δ
health_check/contrib/rabbitmq/apps.py 0% <0%> (ø)
health_check/contrib/rabbitmq/__init__.py 100% <100%> (ø)
health_check/contrib/rabbitmq/backends.py 80% <80%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6064578...c248647. Read the comment docs.

@codingjoe codingjoe merged commit e49faf9 into revsys:master Jul 30, 2018
@codingjoe
Copy link
Collaborator

Released in 3.7.0

@GabLeRoux GabLeRoux mentioned this pull request Nov 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants