-
Notifications
You must be signed in to change notification settings - Fork 207
Add Kafka backend #453
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
Open
QuentinRillet
wants to merge
2
commits into
codingjoe:main
Choose a base branch
from
QuentinRillet:feat_add_kafka_healthcheck
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Kafka backend #453
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import django | ||
|
|
||
| if django.VERSION < (3, 2): | ||
| default_app_config = "health_check.contrib.kafka.apps.HealthCheckConfig" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| from django.apps import AppConfig | ||
|
|
||
| from health_check.plugins import plugin_dir | ||
|
|
||
|
|
||
| class HealthCheckConfig(AppConfig): | ||
| name = "health_check.contrib.kafka" | ||
|
|
||
| def ready(self): | ||
| from .backends import KafkaHealthCheck | ||
|
|
||
| plugin_dir.register(KafkaHealthCheck) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import logging | ||
| import importlib | ||
|
|
||
| from django.conf import settings | ||
|
|
||
| from health_check.backends import BaseHealthCheckBackend | ||
| from health_check.exceptions import ServiceUnavailable | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| try: | ||
| kafka_module = importlib.import_module("kafka") | ||
| except ImportError: | ||
| kafka_module = None | ||
|
|
||
| if not kafka_module: | ||
| raise ImportError( | ||
| "No kafka-python or kafka-python-ng library found. Please install one of them." | ||
| ) | ||
|
|
||
| KafkaAdminClient = getattr(kafka_module, "KafkaAdminClient", None) | ||
| KafkaError = getattr(importlib.import_module("kafka.errors"), "KafkaError", None) | ||
|
|
||
| if not KafkaAdminClient or not KafkaError: | ||
| raise ImportError( | ||
| "KafkaAdminClient or KafkaError not available. Please check your installations." | ||
| ) | ||
|
|
||
|
|
||
| class KafkaHealthCheck(BaseHealthCheckBackend): | ||
| """Health check for Kafka.""" | ||
|
|
||
| namespace = None | ||
|
|
||
| def check_status(self): | ||
| """Check Kafka service by opening and closing a broker channel.""" | ||
| logger.debug("Checking for a KAFKA_URL on django settings...") | ||
|
|
||
| bootstrap_servers = getattr(settings, "KAFKA_URL", None) | ||
|
|
||
| logger.debug( | ||
| "Got %s as the kafka_url. Connecting to kafka...", bootstrap_servers | ||
| ) | ||
|
|
||
| logger.debug("Attempting to connect to kafka...") | ||
| try: | ||
| admin_client = KafkaAdminClient( | ||
| bootstrap_servers=bootstrap_servers, | ||
| request_timeout_ms=3000, # 3 secondes max | ||
| api_version_auto_timeout_ms=1000, | ||
| ) | ||
| # Ping léger : on liste les topics (lecture metadata) | ||
| admin_client.list_topics() | ||
| except KafkaError as e: | ||
| self.add_error(ServiceUnavailable("Unknown error"), e) | ||
| else: | ||
| logger.debug("Connection established. Kafka is healthy.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| from unittest import mock | ||
|
|
||
| from kafka.errors import KafkaError | ||
|
|
||
| from health_check.contrib.kafka.backends import KafkaHealthCheck | ||
|
|
||
|
|
||
| class TestKafkaHealthCheck: | ||
| """Test Kafka health check.""" | ||
|
|
||
| @mock.patch("health_check.contrib.kafka.backends.getattr") | ||
| @mock.patch("health_check.contrib.kafka.backends.Connection") | ||
| def test_broker_refused_connection(self, mocked_connection, mocked_getattr): | ||
| """Test when the connection to Kafka is refused.""" | ||
| mocked_getattr.return_value = "KAFKA_URL" | ||
|
|
||
| conn_exception = ConnectionRefusedError("Refused connection") | ||
|
|
||
| # mock returns | ||
| mocked_conn = mock.MagicMock() | ||
| mocked_connection.return_value.__enter__.return_value = mocked_conn | ||
| mocked_conn.connect.side_effect = conn_exception | ||
|
|
||
| # instantiates the class | ||
| kafka_healthchecker = KafkaHealthCheck() | ||
|
|
||
| # invokes the method check_status() | ||
| kafka_healthchecker.check_status() | ||
| assert len(kafka_healthchecker.errors), 1 | ||
|
|
||
| # mock assertions | ||
| mocked_connection.assert_called_once_with("KAFKA_URL") | ||
|
|
||
| @mock.patch("health_check.contrib.kafka.backends.getattr") | ||
| @mock.patch("health_check.contrib.kafka.backends.Connection") | ||
| def test_broker_auth_error(self, mocked_connection, mocked_getattr): | ||
| """Test that the connection to Kafka has an authentication error.""" | ||
| mocked_getattr.return_value = "KAFKA_URL" | ||
|
|
||
| conn_exception = KafkaError("Refused connection") | ||
|
|
||
| # mock returns | ||
| mocked_conn = mock.MagicMock() | ||
| mocked_connection.return_value.__enter__.return_value = mocked_conn | ||
| mocked_conn.connect.side_effect = conn_exception | ||
|
|
||
| # instantiates the class | ||
| rabbitmq_healthchecker = KafkaHealthCheck() | ||
|
|
||
| # invokes the method check_status() | ||
| rabbitmq_healthchecker.check_status() | ||
| assert len(rabbitmq_healthchecker.errors), 1 | ||
|
|
||
| # mock assertions | ||
| mocked_connection.assert_called_once_with("KAFKA_URL") | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Django 3.2 has reached end of life 😉