-
Notifications
You must be signed in to change notification settings - Fork 113
feat: Add a health check endpoint #2670
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cff88b1
Add healthcheck endpoint
mstopa-splunk 804bceb
Set poetry package-mode to false
mstopa-splunk fd6355b
Update doc examples and statefulset.yaml
mstopa-splunk b1497cd
Fix string to boolean conversion
mstopa-splunk 40bff22
Add CI healthcheck test
mstopa-splunk 2125754
Add CI healthcheck test
mstopa-splunk f8577df
Additional CI healthcheck test
mstopa-splunk f9e86ed
Add unit tests
mstopa-splunk b988142
Fix test-test-healthcheck-unit-tests
mstopa-splunk 9950816
Fix test-test-healthcheck-unit-tests
mstopa-splunk ad45256
Merge branch 'develop' into add_healthcheck_endpoint
mstopa-splunk 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
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
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
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
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
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
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
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
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
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
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
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
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
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,113 @@ | ||
from flask import Flask, jsonify | ||
import logging | ||
import os | ||
import subprocess | ||
|
||
app = Flask(__name__) | ||
|
||
def str_to_bool(value): | ||
return str(value).strip().lower() in { | ||
'true', | ||
'1', | ||
't', | ||
'y', | ||
'yes' | ||
} | ||
|
||
class Config: | ||
SC4S_DEST_SPLUNK_HEC_DEFAULT_URL = os.getenv('SC4S_DEST_SPLUNK_HEC_DEFAULT_URL') | ||
HEALTHCHECK_PORT = int(os.getenv('SC4S_LISTEN_STATUS_PORT', '8080')) | ||
CHECK_QUEUE_SIZE = str_to_bool(os.getenv('HEALTHCHECK_CHECK_QUEUE_SIZE', "false")) | ||
MAX_QUEUE_SIZE = int(os.getenv('HEALTHCHECK_MAX_QUEUE_SIZE', '10000')) | ||
|
||
logging.basicConfig( | ||
format=f"%(asctime)s - healthcheck.py - %(levelname)s - %(message)s", | ||
datefmt="%Y-%m-%d %H:%M:%S" | ||
) | ||
logger = logging.getLogger(__name__) | ||
|
||
def check_syslog_ng_health() -> bool: | ||
"""Check the health of the syslog-ng process.""" | ||
try: | ||
result = subprocess.run( | ||
['syslog-ng-ctl', 'healthcheck', '-t', '1'], | ||
capture_output=True, | ||
text=True, | ||
timeout=5 | ||
) | ||
if result.returncode == 0: | ||
return True | ||
|
||
logger.error(f"syslog-ng healthcheck failed: {result.stderr.strip()}") | ||
return False | ||
except subprocess.TimeoutExpired: | ||
logger.error("syslog-ng healthcheck timed out.") | ||
return False | ||
except Exception as e: | ||
logger.exception(f"Unexpected error during syslog-ng healthcheck: {e}") | ||
return False | ||
|
||
def check_queue_size( | ||
sc4s_dest_splunk_hec_default=Config.SC4S_DEST_SPLUNK_HEC_DEFAULT_URL, | ||
max_queue_size=Config.MAX_QUEUE_SIZE | ||
) -> bool: | ||
"""Check syslog-ng queue size and compare it against the configured maximum limit.""" | ||
if not sc4s_dest_splunk_hec_default: | ||
logger.error( | ||
"SC4S_DEST_SPLUNK_HEC_DEFAULT_URL not configured. " | ||
"Ensure the default HEC destination is set, or disable HEALTHCHECK_CHECK_QUEUE_SIZE." | ||
) | ||
return False | ||
|
||
try: | ||
result = subprocess.run( | ||
['syslog-ng-ctl', 'stats'], | ||
capture_output=True, | ||
text=True, | ||
timeout=5 | ||
) | ||
if result.returncode != 0: | ||
logger.error(f"syslog-ng stats command failed: {result.stderr.strip()}") | ||
return False | ||
|
||
stats = result.stdout.splitlines() | ||
destination_stat = next( | ||
(s for s in stats if ";queued;" in s and sc4s_dest_splunk_hec_default in s), | ||
None | ||
) | ||
if not destination_stat: | ||
logger.error("No matching queue stats found for the destination URL.") | ||
return False | ||
|
||
queue_size = int(destination_stat.split(";")[-1]) | ||
if queue_size > max_queue_size: | ||
logger.warning( | ||
f"Queue size {queue_size} exceeds the maximum limit of {max_queue_size}." | ||
) | ||
return False | ||
|
||
return True | ||
except subprocess.TimeoutExpired: | ||
logger.error("syslog-ng stats command timed out.") | ||
return False | ||
except Exception as e: | ||
logger.exception(f"Unexpected error checking queue size: {e}") | ||
return False | ||
|
||
@app.route('/health', methods=['GET']) | ||
def healthcheck(): | ||
if Config.CHECK_QUEUE_SIZE: | ||
if not check_syslog_ng_health(): | ||
return jsonify({'status': 'unhealthy: syslog-ng healthcheck failed'}), 503 | ||
if not check_queue_size(): | ||
return jsonify({'status': 'unhealthy: queue size exceeded limit'}), 503 | ||
else: | ||
if not check_syslog_ng_health(): | ||
return jsonify({'status': 'unhealthy: syslog-ng healthcheck failed'}), 503 | ||
|
||
logger.info("Service is healthy.") | ||
return jsonify({'status': 'healthy'}), 200 | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(host='0.0.0.0', port=Config.HEALTHCHECK_PORT) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.