diff --git a/NEWS b/NEWS index 9de69c6ca..e66c87fba 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 70fdeb1b1..352c4dcb1 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 055cfaf02..56b1e07e2 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 eaac9ff55..3a27cd6d4 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'}