Add Healthchecks monitoring hook "skip_states" option to disable pinging for particular monitoring states (#525).

This commit is contained in:
Dan Helfman 2022-05-24 13:59:28 -07:00
parent b9741f4d0b
commit 865eff7d98
4 changed files with 52 additions and 1 deletions

4
NEWS
View file

@ -3,6 +3,8 @@
logs to send to the Healthchecks server. logs to send to the Healthchecks server.
* #402: Remove the error when "archive_name_format" is specified but a retention prefix isn't. * #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. * #420: Warn when an unsupported variable is used in a hook command.
* #525: Add Healthchecks monitoring hook "skip_states" option to disable pinging for particular
monitoring states.
* #528: Improve the error message when a configuration override contains an invalid value. * #528: Improve the error message when a configuration override contains an invalid value.
* #531: BREAKING: When deep merging common configuration, merge colliding list values by appending * #531: BREAKING: When deep merging common configuration, merge colliding list values by appending
them. Previously, one list replaced the other. them. Previously, one list replaced the other.
@ -12,7 +14,7 @@
* Add a randomized delay to the sample systemd timer to spread out the load on a server. * Add a randomized delay to the sample systemd timer to spread out the load on a server.
* Change the configuration format for borgmatic monitoring hooks (Healthchecks, Cronitor, * Change the configuration format for borgmatic monitoring hooks (Healthchecks, Cronitor,
PagerDuty, and Cronhub) to specify the ping URL / integration key as a named option. The intent PagerDuty, and Cronhub) to specify the ping URL / integration key as a named option. The intent
is to support additional options in the future. This change is backwards-compatible. is to support additional options (some in this release). This change is backwards-compatible.
* Add emojis to documentation table of contents to make it easier to find particular how-to and * Add emojis to documentation table of contents to make it easier to find particular how-to and
reference guides at a glance. reference guides at a glance.

View file

@ -901,6 +901,19 @@ properties:
send all logs and disable this truncation. Defaults send all logs and disable this truncation. Defaults
to 100000. to 100000.
example: 200000 example: 200000
skip_states:
type: array
items:
type: string
enum:
- start
- finish
- fail
uniqueItems: true
description: |
List of one or more monitoring states to skip
pinging for: "start", "finish", and/or "fail".
Defaults to pinging for all states.
description: | description: |
Configuration for a monitoring integration with Configuration for a monitoring integration with
Healthchecks. Create an account at https://healthchecks.io Healthchecks. Create an account at https://healthchecks.io

View file

@ -98,6 +98,12 @@ def ping_monitor(hook_config, config_filename, state, monitoring_log_level, dry_
) )
dry_run_label = ' (dry run; not actually pinging)' if dry_run else '' dry_run_label = ' (dry run; not actually pinging)' if dry_run else ''
if state.name.lower() in hook_config.get('skip_states', []):
logger.info(
f'{config_filename}: Skipping Healthchecks {state.name.lower()} ping due to configured skip states'
)
return
healthchecks_state = MONITOR_STATE_TO_HEALTHCHECKS.get(state) healthchecks_state = MONITOR_STATE_TO_HEALTHCHECKS.get(state)
if healthchecks_state: if healthchecks_state:
ping_url = '{}/{}'.format(ping_url, healthchecks_state) ping_url = '{}/{}'.format(ping_url, healthchecks_state)

View file

@ -185,3 +185,33 @@ def test_ping_monitor_dry_run_does_not_hit_ping_url():
monitoring_log_level=1, monitoring_log_level=1,
dry_run=True, dry_run=True,
) )
def test_ping_monitor_with_skip_states_does_not_hit_ping_url():
flexmock(module).should_receive('Forgetful_buffering_handler')
hook_config = {'ping_url': 'https://example.com', 'skip_states': ['start']}
flexmock(module.requests).should_receive('post').never()
module.ping_monitor(
hook_config,
'config.yaml',
state=module.monitor.State.START,
monitoring_log_level=1,
dry_run=True,
)
def test_ping_monitor_hits_ping_url_with_non_matching_skip_states():
flexmock(module).should_receive('Forgetful_buffering_handler')
hook_config = {'ping_url': 'https://example.com', 'skip_states': ['finish']}
flexmock(module.requests).should_receive('post').with_args(
'https://example.com/start', data=''.encode('utf-8')
)
module.ping_monitor(
hook_config,
'config.yaml',
state=module.monitor.State.START,
monitoring_log_level=1,
dry_run=False,
)