From 157e59ac88391a2f37aa30cce3f5bb00db801d60 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Tue, 24 May 2022 14:44:33 -0700 Subject: [PATCH] Add Healthchecks monitoring hook "send_logs" option to enable/disable sending borgmatic logs to the Healthchecks server (#460). --- NEWS | 2 ++ borgmatic/config/schema.yaml | 6 ++++++ borgmatic/hooks/healthchecks.py | 8 ++++++-- tests/unit/hooks/test_healthchecks.py | 16 ++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 9de69c6..e66c87f 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,8 @@ logs to send to the Healthchecks server. * #402: Remove the error when "archive_name_format" is specified but a retention prefix isn't. * #420: Warn when an unsupported variable is used in a hook command. + * #460: Add Healthchecks monitoring hook "send_logs" option to enable/disable sending borgmatic + logs to the Healthchecks server. * #525: Add Healthchecks monitoring hook "states" option to only enable pinging for particular monitoring states (start, finish, fail). * #528: Improve the error message when a configuration override contains an invalid value. diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index 70fdeb1..352c4dc 100644 --- a/borgmatic/config/schema.yaml +++ b/borgmatic/config/schema.yaml @@ -892,6 +892,12 @@ properties: Healthchecks ping URL or UUID to notify when a backup begins, ends, or errors. example: https://hc-ping.com/your-uuid-here + send_logs: + type: boolean + description: | + Send borgmatic logs to Healthchecks as part the + "finish" state. Defaults to true. + example: false ping_body_limit: type: integer description: | diff --git a/borgmatic/hooks/healthchecks.py b/borgmatic/hooks/healthchecks.py index 055cfaf..56b1e07 100644 --- a/borgmatic/hooks/healthchecks.py +++ b/borgmatic/hooks/healthchecks.py @@ -71,9 +71,13 @@ def format_buffered_logs_for_payload(): def initialize_monitor(hook_config, config_filename, monitoring_log_level, dry_run): ''' - Add a handler to the root logger that stores in memory the most recent logs emitted. That - way, we can send them all to Healthchecks upon a finish or failure state. + Add a handler to the root logger that stores in memory the most recent logs emitted. That way, + we can send them all to Healthchecks upon a finish or failure state. But skip this if the + "send_logs" option is false. ''' + if hook_config.get('send_logs') is False: + return + ping_body_limit = max( hook_config.get('ping_body_limit', DEFAULT_PING_BODY_LIMIT_BYTES) - len(PAYLOAD_TRUNCATION_INDICATOR), diff --git a/tests/unit/hooks/test_healthchecks.py b/tests/unit/hooks/test_healthchecks.py index eaac9ff..3a27cd6 100644 --- a/tests/unit/hooks/test_healthchecks.py +++ b/tests/unit/hooks/test_healthchecks.py @@ -106,6 +106,22 @@ def test_initialize_monitor_creates_log_handler_with_zero_ping_body_limit(): ) +def test_initialize_monitor_creates_log_handler_when_send_logs_true(): + flexmock(module).should_receive('Forgetful_buffering_handler').once() + + module.initialize_monitor( + {'send_logs': True}, 'test.yaml', monitoring_log_level=1, dry_run=False + ) + + +def test_initialize_monitor_bails_when_send_logs_false(): + flexmock(module).should_receive('Forgetful_buffering_handler').never() + + module.initialize_monitor( + {'send_logs': False}, 'test.yaml', monitoring_log_level=1, dry_run=False + ) + + def test_ping_monitor_hits_ping_url_for_start_state(): flexmock(module).should_receive('Forgetful_buffering_handler') hook_config = {'ping_url': 'https://example.com'}