Merge ~ballot/charm-prometheus-alertmanager/+git/charm-prometheus-alertmanager:log-level into charm-prometheus-alertmanager:master

Proposed by Benjamin Allot
Status: Merged
Approved by: Xav Paice
Approved revision: b289d7ae413d83447ddb084654d33dc157d58b18
Merged at revision: b289d7ae413d83447ddb084654d33dc157d58b18
Proposed branch: ~ballot/charm-prometheus-alertmanager/+git/charm-prometheus-alertmanager:log-level
Merge into: charm-prometheus-alertmanager:master
Diff against target: 114 lines (+72/-0)
3 files modified
src/config.yaml (+6/-0)
src/reactive/alertmanager.py (+15/-0)
src/tests/unit/test_reactive_alertmanager.py (+51/-0)
Reviewer Review Type Date Requested Status
Xav Paice (community) Approve
Drew Freiberger (community) Approve
Review via email: mp+396993@code.launchpad.net

This proposal supersedes a proposal from 2021-01-06.

Commit message

Add the log-level config item

In order to easily debug, configuring the log-level of alertmanager can be handy.
The old default value of info (if not specified) is kept so an upgrade won't be bothered by the change.

To post a comment you must log in.
Revision history for this message
Drew Freiberger (afreiberger) wrote : Posted in a previous version of this proposal

I'd like to see this charm go into a blocked state for an incorrectly configured log level value, and would appreciate seeing the values accepted for log-level in the config.yaml description.

I am also concerned that this change when upgrade-charm runs will restart the service (which is typically frowned upon) even if no configuration change is expected. Maybe the default should be a blank string, which equates to not setting a --log.level, which should keep the config file from being updated and the service from restarting based on service context change.

review: Needs Fixing
Revision history for this message
Drew Freiberger (afreiberger) wrote :

LGTM, running tests now. Thank you so much for the contribution and updates!

review: Approve
Revision history for this message
Drew Freiberger (afreiberger) wrote :

tests pass. no functional testing in charm.

Revision history for this message
Xav Paice (xavpaice) wrote :

Ran manual functest, LGTM.

Am adding a new commit for some basic functests, outside the scope of this change.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/config.yaml b/src/config.yaml
2index 5c7bf30..fe0c1b1 100644
3--- a/src/config.yaml
4+++ b/src/config.yaml
5@@ -9,6 +9,12 @@ options:
6 type: int
7 default: 9093
8 description: "Alertmanager listening port"
9+ log-level:
10+ type: string
11+ default: ""
12+ description: |
13+ Loggging level used for alertmanager.
14+ Valid values are "error", "warn", "info" and "debug".
15 external_url:
16 default: ""
17 type: string
18diff --git a/src/reactive/alertmanager.py b/src/reactive/alertmanager.py
19index e67f0e0..c9c028c 100644
20--- a/src/reactive/alertmanager.py
21+++ b/src/reactive/alertmanager.py
22@@ -18,6 +18,7 @@ ALERTMANAGER_YML = "/var/snap/prometheus-alertmanager/current/alertmanager.yml"
23 ALERTMANAGER_DEF = "/var/snap/prometheus-alertmanager/current/daemon_arguments"
24 ALERTMANAGER_YML_TMPL = "alertmanager.yml.j2"
25 ALERTMANAGER_DEF_TMPL = "daemon_arguments.j2"
26+LOG_LEVELS = ["debug", "error", "info", "warn"]
27
28
29 def templates_changed(tmpl_list):
30@@ -121,8 +122,22 @@ def write_alertmanager_config_yml():
31 def write_alertmanager_config_def():
32 config = hookenv.config()
33 port = config.get("port", "9093")
34+ log_level = config.get("log-level")
35+ if log_level:
36+ if log_level in LOG_LEVELS:
37+ runtime_args("--log.level", "{}".format(log_level))
38+ else:
39+ hookenv.status_set(
40+ "blocked",
41+ "log-level {} is unknown."
42+ " Pick one of the following value: {}".format(
43+ log_level, ",".join(LOG_LEVELS)
44+ ),
45+ )
46+ return
47 check_ports(port)
48 runtime_args("--web.listen-address", ":{}".format(port))
49+
50 if config.get("external_url", False):
51 vars = {
52 "private_address": hookenv.unit_get("private-address"),
53diff --git a/src/tests/unit/test_reactive_alertmanager.py b/src/tests/unit/test_reactive_alertmanager.py
54index 98ff2f1..ed27ba5 100644
55--- a/src/tests/unit/test_reactive_alertmanager.py
56+++ b/src/tests/unit/test_reactive_alertmanager.py
57@@ -176,6 +176,57 @@ class TestPrometheusContext(unittest.TestCase):
58 contents, "ARGS=.*-web.listen.address :{}.*".format(fake_port)
59 )
60
61+ def test_log_level_configs(self, mock_get_peers, mock_hookenv_config, *args):
62+ config = self.def_config
63+ fake_port = "9876"
64+ fake_ext_url = "http://foo:1234/bar"
65+ config.update(
66+ {
67+ "port": fake_port,
68+ "external_url": fake_ext_url,
69+ "log-level": "debug",
70+ }
71+ )
72+ mock_hookenv_config.return_value = config
73+ alertmanager.write_alertmanager_config_yml()
74+ alertmanager.write_alertmanager_config_def()
75+ # Verify etc/default/alertmanager has --log.level
76+ with open(self.cfg_def) as fh:
77+ contents = fh.read()
78+ self.assertRegex(contents, "ARGS=.*--log.level debug.*")
79+
80+ def test_bad_log_level_configs(
81+ self,
82+ mock_get_peers,
83+ mock_hookenv_config,
84+ mock_hookenv_unit_get,
85+ mock_valudate_config,
86+ mock_data_changed,
87+ mock_host_service_running,
88+ mock_host_service_restart,
89+ mock_hookenv_status_set,
90+ *args
91+ ):
92+ config = self.def_config
93+ fake_port = "9876"
94+ fake_ext_url = "http://foo:1234/bar"
95+ config.update(
96+ {
97+ "port": fake_port,
98+ "external_url": fake_ext_url,
99+ "log-level": "dummylevel",
100+ }
101+ )
102+ mock_hookenv_config.return_value = config
103+ alertmanager.write_alertmanager_config_yml()
104+ alertmanager.write_alertmanager_config_def()
105+ # Verify charm status is blocked
106+ mock_hookenv_status_set.assert_called_with(
107+ "blocked",
108+ "log-level dummylevel is unknown."
109+ " Pick one of the following value: debug,error,info,warn",
110+ )
111+
112 @mock.patch("reactive.alertmanager.remove_state")
113 def test_install_packages_conditionally_called(
114 self,

Subscribers

People subscribed via source and target branches

to all changes: