Merge ~sajoupa/charm-telegraf:split_prometheus_client_config into charm-telegraf:master
- Git
- lp:~sajoupa/charm-telegraf
- split_prometheus_client_config
- Merge into master
Status: | Superseded |
---|---|
Proposed branch: | ~sajoupa/charm-telegraf:split_prometheus_client_config |
Merge into: | charm-telegraf:master |
Diff against target: |
387 lines (+137/-73) 4 files modified
src/reactive/telegraf.py (+112/-45) src/templates/prometheus_client.tmpl (+3/-0) src/templates/telegraf.conf.tmpl (+0/-6) src/tests/unit/test_telegraf.py (+22/-22) |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Canonical IS Reviewers | Pending | ||
BootStack Reviewers | Pending | ||
Review via email: mp+397010@code.launchpad.net |
This proposal has been superseded by a proposal from 2021-02-18.
Commit message
Split prometheus_client output config out of the main config file. Allow a prometheus-client relation to prometheus even if prometheus_
Description of the change
Laurent Sesquès (sajoupa) wrote : | # |
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : | # |
This merge proposal is being monitored by mergebot. Change the status to Approved to merge.
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : | # |
Unable to determine commit message from repository - please click "Set commit message" and enter the commit message manually.
Haw Loeung (hloeung) : | # |
Unmerged commits
- 3c4a87e... by Laurent Sesquès
-
small fix: move flag changes to the right if stanza for config.changed
- f550c15... by Laurent Sesquès
-
Split prometheus_client output config out of the main config file. Allow a prometheus-client relation to prometheus even if prometheus_
output_ port is set (and use that port in the relation).
Preview Diff
1 | diff --git a/src/reactive/telegraf.py b/src/reactive/telegraf.py | |||
2 | index c139276..e2c0874 100644 | |||
3 | --- a/src/reactive/telegraf.py | |||
4 | +++ b/src/reactive/telegraf.py | |||
5 | @@ -159,7 +159,14 @@ def list_config_files(): | |||
6 | 159 | current_states = get_states() | 159 | current_states = get_states() |
7 | 160 | 160 | ||
8 | 161 | for plugin in list_supported_plugins(): | 161 | for plugin in list_supported_plugins(): |
10 | 162 | if "plugins.{}.configured".format(plugin) in current_states.keys(): | 162 | # The prometheus_client plugin can be configured either from a relation or |
11 | 163 | # from the juju config | ||
12 | 164 | if ("plugins.{}.configured".format(plugin) in current_states.keys()) or ( | ||
13 | 165 | "{}.configured".format(plugin) in current_states.keys() | ||
14 | 166 | ): | ||
15 | 167 | # The "prometheus-client" relation sets the "prometheus_client" plugin | ||
16 | 168 | if plugin == "prometheus-client": | ||
17 | 169 | plugin = "prometheus_client" | ||
18 | 163 | config_path = "{}/{}.conf".format(get_configs_dir(), plugin) | 170 | config_path = "{}/{}.conf".format(get_configs_dir(), plugin) |
19 | 164 | config_files.append(config_path) | 171 | config_files.append(config_path) |
20 | 165 | 172 | ||
21 | @@ -503,9 +510,6 @@ def configure_telegraf(): # noqa: C901 | |||
22 | 503 | ) | 510 | ) |
23 | 504 | context["logfile"] = "" | 511 | context["logfile"] = "" |
24 | 505 | 512 | ||
25 | 506 | if get_prometheus_port(): | ||
26 | 507 | context["prometheus_output_port"] = get_prometheus_port() | ||
27 | 508 | check_prometheus_port("prometheus_output", get_prometheus_port()) | ||
28 | 509 | context["extra_options"] = get_extra_options() | 513 | context["extra_options"] = get_extra_options() |
29 | 510 | 514 | ||
30 | 511 | if get_socket_listener_port(): | 515 | if get_socket_listener_port(): |
31 | @@ -621,6 +625,7 @@ def install_telegraf(): | |||
32 | 621 | 625 | ||
33 | 622 | @when("telegraf.installed") | 626 | @when("telegraf.installed") |
34 | 623 | @when("apt.installed.telegraf") | 627 | @when("apt.installed.telegraf") |
35 | 628 | @when("plugins.prometheus-client.configured") | ||
36 | 624 | @when_not("telegraf.configured") | 629 | @when_not("telegraf.configured") |
37 | 625 | @when_not("telegraf.apt.configured") | 630 | @when_not("telegraf.apt.configured") |
38 | 626 | def configure_telegraf_deb(): | 631 | def configure_telegraf_deb(): |
39 | @@ -629,6 +634,7 @@ def configure_telegraf_deb(): | |||
40 | 629 | 634 | ||
41 | 630 | @when("telegraf.installed") | 635 | @when("telegraf.installed") |
42 | 631 | @when("snap.installed.telegraf") | 636 | @when("snap.installed.telegraf") |
43 | 637 | @when("plugins.prometheus-client.configured") | ||
44 | 632 | @when_not("telegraf.configured") | 638 | @when_not("telegraf.configured") |
45 | 633 | @when_not("telegraf.snap.configured") | 639 | @when_not("telegraf.snap.configured") |
46 | 634 | def configure_telegraf_snap(): | 640 | def configure_telegraf_snap(): |
47 | @@ -653,6 +659,7 @@ def handle_config_changes(): | |||
48 | 653 | if config.changed("extra_options"): | 659 | if config.changed("extra_options"): |
49 | 654 | for plugin in list_supported_plugins(): | 660 | for plugin in list_supported_plugins(): |
50 | 655 | clear_flag("plugins.{}.configured".format(plugin)) | 661 | clear_flag("plugins.{}.configured".format(plugin)) |
51 | 662 | clear_flag("prometheus-client.relation.configured") | ||
52 | 656 | # if something else changed, let's reconfigure telegraf itself just in case | 663 | # if something else changed, let's reconfigure telegraf itself just in case |
53 | 657 | 664 | ||
54 | 658 | if config.changed("extra_plugins"): | 665 | if config.changed("extra_plugins"): |
55 | @@ -665,6 +672,12 @@ def handle_config_changes(): | |||
56 | 665 | ): | 672 | ): |
57 | 666 | clear_flag("telegraf.installed") | 673 | clear_flag("telegraf.installed") |
58 | 667 | clear_flag("extra_plugins.configured") | 674 | clear_flag("extra_plugins.configured") |
59 | 675 | clear_flag("plugins.prometheus-client.configured") | ||
60 | 676 | clear_flag("prometheus-client.relation.configured") | ||
61 | 677 | |||
62 | 678 | if config.changed("prometheus_output_port"): | ||
63 | 679 | clear_flag("plugins.prometheus-client.configured") | ||
64 | 680 | clear_flag("prometheus-client.relation.configured") | ||
65 | 668 | clear_flag("telegraf.configured") | 681 | clear_flag("telegraf.configured") |
66 | 669 | clear_flag("telegraf.apt.configured") | 682 | clear_flag("telegraf.apt.configured") |
67 | 670 | clear_flag("telegraf.snap.configured") | 683 | clear_flag("telegraf.snap.configured") |
68 | @@ -1183,59 +1196,113 @@ def influxdb_api_output(influxdb): | |||
69 | 1183 | set_flag("telegraf.needs_reload") | 1196 | set_flag("telegraf.needs_reload") |
70 | 1184 | 1197 | ||
71 | 1185 | 1198 | ||
72 | 1199 | def generate_prometheus_output_config(prometheus_output_port): | ||
73 | 1200 | # If extra_options are set for prometheus_client, let's integrate them | ||
74 | 1201 | extra_options = get_extra_options() | ||
75 | 1202 | options = extra_options["outputs"].get("prometheus_client", {}) | ||
76 | 1203 | listen = options.pop("listen", None) | ||
77 | 1204 | if not listen: | ||
78 | 1205 | listen = ":{}".format(prometheus_output_port) | ||
79 | 1206 | elif int(listen.split(":", 1)[1]) != prometheus_output_port: | ||
80 | 1207 | hookenv.log( | ||
81 | 1208 | """prometheus_output_port is {}, but extra_options would set it | ||
82 | 1209 | to {}. Choosing {} from prometheus_output_port.""".format( | ||
83 | 1210 | prometheus_output_port, | ||
84 | 1211 | int(listen.split(":", 1)[1]), | ||
85 | 1212 | prometheus_output_port, | ||
86 | 1213 | ), | ||
87 | 1214 | level=hookenv.WARNING, | ||
88 | 1215 | ) | ||
89 | 1216 | listen = "{}:{}".format(listen.split(":", 1)[0], prometheus_output_port) | ||
90 | 1217 | |||
91 | 1218 | return { | ||
92 | 1219 | "listen": listen, | ||
93 | 1220 | "extra_options": render_extra_options( | ||
94 | 1221 | "outputs", "prometheus_client", extra_options=extra_options | ||
95 | 1222 | ), | ||
96 | 1223 | } | ||
97 | 1224 | |||
98 | 1225 | |||
99 | 1226 | def render_prometheus_client_config(port): | ||
100 | 1227 | config_path = "{}/{}.conf".format(get_configs_dir(), "prometheus_client") | ||
101 | 1228 | hookenv.log( | ||
102 | 1229 | "Updating {} plugin config file. Port is {}".format("prometheus_client", port), | ||
103 | 1230 | level=hookenv.INFO, | ||
104 | 1231 | ) | ||
105 | 1232 | context = generate_prometheus_output_config(port) | ||
106 | 1233 | render( | ||
107 | 1234 | source="prometheus_client.tmpl", | ||
108 | 1235 | templates_dir=get_templates_dir(), | ||
109 | 1236 | target=config_path, | ||
110 | 1237 | context=context, | ||
111 | 1238 | ) | ||
112 | 1239 | |||
113 | 1240 | |||
114 | 1186 | @when("prometheus-client.available") | 1241 | @when("prometheus-client.available") |
121 | 1187 | @when("telegraf.installed") | 1242 | @when_not("prometheus-client.relation.configured") |
122 | 1188 | def prometheus_client(prometheus): | 1243 | def configure_prometheus_client_with_relation(prometheus): |
123 | 1189 | template = """ | 1244 | hookenv.log( |
124 | 1190 | [[outputs.prometheus_client]] | 1245 | "Configuring prometheus_client output plugin, with prometheus-client relation", |
125 | 1191 | listen = "{{ listen }}" | 1246 | level=hookenv.DEBUG, |
126 | 1192 | """ | 1247 | ) |
127 | 1248 | if get_prometheus_port(): | ||
128 | 1249 | port = get_prometheus_port() | ||
129 | 1250 | else: | ||
130 | 1251 | # We have a relation with prometheus, but removed the port number config. | ||
131 | 1252 | # Default to 9126. | ||
132 | 1253 | port = "9126" | ||
133 | 1254 | # We'll iterate through the prometheus-client relation counterparts, | ||
134 | 1255 | # inform them of our address so that they scrape it, and get their egress subnets | ||
135 | 1256 | # so that we can allow them | ||
136 | 1257 | remote_egress_subnets = [] | ||
137 | 1193 | for relation_id in hookenv.relation_ids("prometheus-client"): | 1258 | for relation_id in hookenv.relation_ids("prometheus-client"): |
138 | 1194 | # if juju 2.x+ then we'll attempt to get the network space address | 1259 | # if juju 2.x+ then we'll attempt to get the network space address |
139 | 1195 | try: | 1260 | try: |
141 | 1196 | hookenv.log("Network Info") | 1261 | hookenv.log("Getting local network info", level=hookenv.DEBUG) |
142 | 1197 | network_info = hookenv.network_get( | 1262 | network_info = hookenv.network_get( |
143 | 1198 | "prometheus-client", relation_id=relation_id | 1263 | "prometheus-client", relation_id=relation_id |
144 | 1199 | ) | 1264 | ) |
146 | 1200 | hookenv.log(network_info) | 1265 | hookenv.log(network_info, level=hookenv.DEBUG) |
147 | 1201 | if "ingress-addresses" in network_info: | 1266 | if "ingress-addresses" in network_info: |
148 | 1202 | ip_addr = network_info.get("ingress-addresses")[0] | 1267 | ip_addr = network_info.get("ingress-addresses")[0] |
149 | 1203 | else: | 1268 | else: |
150 | 1204 | ip_addr = hookenv.network_get_primary_address("prometheus-client") | 1269 | ip_addr = hookenv.network_get_primary_address("prometheus-client") |
151 | 1270 | for unit in hookenv.related_units(relation_id): | ||
152 | 1271 | hookenv.log( | ||
153 | 1272 | "Getting remote egress subnet for relation {} - {}".format( | ||
154 | 1273 | unit, relation_id | ||
155 | 1274 | ), | ||
156 | 1275 | level=hookenv.DEBUG, | ||
157 | 1276 | ) | ||
158 | 1277 | remote_egress_subnets.append( | ||
159 | 1278 | hookenv.relation_get("egress-subnets", unit, relation_id) | ||
160 | 1279 | ) | ||
161 | 1205 | except NotImplementedError: | 1280 | except NotImplementedError: |
162 | 1206 | # if that fails, just let prometheus.configure(...) do it's default | 1281 | # if that fails, just let prometheus.configure(...) do it's default |
163 | 1207 | ip_addr = None | 1282 | ip_addr = None |
164 | 1208 | if get_prometheus_port(): | ||
165 | 1209 | hookenv.log("Prometheus configured globally, skipping plugin setup") | ||
166 | 1210 | prometheus.configure( | ||
167 | 1211 | get_prometheus_port(), hostname=ip_addr, private_address=ip_addr | ||
168 | 1212 | ) | ||
169 | 1213 | set_flag("prometheus-client.configured") | ||
170 | 1214 | # bail out, nothing more need to be configured here | ||
171 | 1215 | return | ||
172 | 1216 | port = 9126 | ||
173 | 1217 | extra_options = get_extra_options() | ||
174 | 1218 | options = extra_options["outputs"].get("prometheus-client", {}) | ||
175 | 1219 | listen = options.pop("listen", None) | ||
176 | 1220 | if listen is not None: | ||
177 | 1221 | hookenv.log( | ||
178 | 1222 | "Configuring prometheus_client plugin to listen on: '{}'".format(listen) | ||
179 | 1223 | ) | ||
180 | 1224 | port = int(listen.split(":", 1)[1]) | ||
181 | 1225 | else: | ||
182 | 1226 | listen = ":{}".format(port) | ||
183 | 1227 | check_prometheus_port("prometheus_output", port) | ||
184 | 1228 | prometheus.configure(port, hostname=ip_addr, private_address=ip_addr) | 1283 | prometheus.configure(port, hostname=ip_addr, private_address=ip_addr) |
192 | 1229 | config_path = "{}/{}.conf".format(get_configs_dir(), "prometheus-client") | 1284 | check_prometheus_port("prometheus_output", port) |
193 | 1230 | hookenv.log("Updating {} plugin config file".format("prometheus-client")) | 1285 | render_prometheus_client_config(port) |
194 | 1231 | context = {"listen": listen} | 1286 | set_flag("plugins.prometheus-client.configured") |
195 | 1232 | content = render_template(template, context) + render_extra_options( | 1287 | set_flag("prometheus-client.relation.configured") |
196 | 1233 | "outputs", "prometheus_client", extra_options=extra_options | 1288 | set_flag("telegraf.needs_reload") |
197 | 1234 | ) | 1289 | |
198 | 1235 | host.write_file(config_path, content.encode("utf-8")) | 1290 | |
199 | 1291 | @when_not("prometheus-client.available") | ||
200 | 1292 | @when_not("plugins.prometheus-client.configured") | ||
201 | 1293 | def configure_prometheus_client(): | ||
202 | 1294 | hookenv.log("Configuring prometheus_client output plugin", level=hookenv.DEBUG) | ||
203 | 1295 | if get_prometheus_port(): | ||
204 | 1296 | port = get_prometheus_port() | ||
205 | 1297 | else: | ||
206 | 1298 | # No relation to prometheus, no port configured: do not configure the plugin | ||
207 | 1236 | set_flag("plugins.prometheus-client.configured") | 1299 | set_flag("plugins.prometheus-client.configured") |
210 | 1237 | set_flag("telegraf.needs_reload") | 1300 | return |
211 | 1238 | set_flag("prometheus-client.configured") | 1301 | check_prometheus_port("prometheus_output", port) |
212 | 1302 | render_prometheus_client_config(port) | ||
213 | 1303 | set_flag("plugins.prometheus-client.configured") | ||
214 | 1304 | set_flag("telegraf.needs_reload") | ||
215 | 1305 | clear_flag("prometheus-client.relation.configured") | ||
216 | 1239 | 1306 | ||
217 | 1240 | 1307 | ||
218 | 1241 | def convert_days(time_string): | 1308 | def convert_days(time_string): |
219 | @@ -1293,17 +1360,15 @@ def render_prometheus_rules(prometheus_rules): | |||
220 | 1293 | 1360 | ||
221 | 1294 | 1361 | ||
222 | 1295 | @when_not("prometheus-client.available") | 1362 | @when_not("prometheus-client.available") |
224 | 1296 | @when("plugins.prometheus-client.configured") | 1363 | @when("prometheus-client.relation.configured") |
225 | 1297 | def prometheus_client_departed(): | 1364 | def prometheus_client_departed(): |
226 | 1298 | hookenv.log("prometheus-client relation not available") | 1365 | hookenv.log("prometheus-client relation not available") |
228 | 1299 | config_path = "{}/{}.conf".format(get_configs_dir(), "prometheus-client") | 1366 | config_path = "{}/{}.conf".format(get_configs_dir(), "prometheus_client") |
229 | 1300 | rels = hookenv.relations_of_type("prometheus-client") | 1367 | rels = hookenv.relations_of_type("prometheus-client") |
230 | 1301 | if not rels and os.path.exists(config_path): | 1368 | if not rels and os.path.exists(config_path): |
231 | 1302 | hookenv.log("Deleting {} plugin config file".format("prometheus-client")) | 1369 | hookenv.log("Deleting {} plugin config file".format("prometheus-client")) |
232 | 1303 | os.unlink(config_path) | 1370 | os.unlink(config_path) |
233 | 1304 | clear_flag("plugins.prometheus-client.configured") | 1371 | clear_flag("plugins.prometheus-client.configured") |
234 | 1305 | set_flag("telegraf.needs_reload") | ||
235 | 1306 | clear_flag("prometheus-client.configured") | ||
236 | 1307 | 1372 | ||
237 | 1308 | 1373 | ||
238 | 1309 | @when( | 1374 | @when( |
239 | @@ -1382,6 +1447,8 @@ def grafana_dashboard_import_failed(): | |||
240 | 1382 | 1447 | ||
241 | 1383 | 1448 | ||
242 | 1384 | @when("telegraf.needs_reload") | 1449 | @when("telegraf.needs_reload") |
243 | 1450 | @when("telegraf.installed") | ||
244 | 1451 | @when("telegraf.configured") | ||
245 | 1385 | def start_or_restart(): | 1452 | def start_or_restart(): |
246 | 1386 | states = sorted( | 1453 | states = sorted( |
247 | 1387 | [ | 1454 | [ |
248 | diff --git a/src/templates/prometheus_client.tmpl b/src/templates/prometheus_client.tmpl | |||
249 | 1388 | new file mode 100644 | 1455 | new file mode 100644 |
250 | index 0000000..add2099 | |||
251 | --- /dev/null | |||
252 | +++ b/src/templates/prometheus_client.tmpl | |||
253 | @@ -0,0 +1,3 @@ | |||
254 | 1 | [[outputs.prometheus_client]] | ||
255 | 2 | listen = "{{ listen }}" | ||
256 | 3 | {%- if extra_options %}{{ extra_options }}{%- endif %} | ||
257 | diff --git a/src/templates/telegraf.conf.tmpl b/src/templates/telegraf.conf.tmpl | |||
258 | index af59ed5..966b4a9 100644 | |||
259 | --- a/src/templates/telegraf.conf.tmpl | |||
260 | +++ b/src/templates/telegraf.conf.tmpl | |||
261 | @@ -80,12 +80,6 @@ | |||
262 | 80 | 80 | ||
263 | 81 | {{ outputs }} | 81 | {{ outputs }} |
264 | 82 | 82 | ||
265 | 83 | {% if prometheus_output_port %} | ||
266 | 84 | [[outputs.prometheus_client]] | ||
267 | 85 | listen = ":{{ prometheus_output_port }}" | ||
268 | 86 | {%- if extra_options['outputs']['prometheus_client'] %}{{ render_options('prometheus_client', 'outputs', extra_options['outputs']) }}{%- endif %} | ||
269 | 87 | {%- endif %} | ||
270 | 88 | |||
271 | 89 | ############################################################################### | 83 | ############################################################################### |
272 | 90 | # INPUTS # | 84 | # INPUTS # |
273 | 91 | ############################################################################### | 85 | ############################################################################### |
274 | diff --git a/src/tests/unit/test_telegraf.py b/src/tests/unit/test_telegraf.py | |||
275 | index 2cfbdc8..09e1575 100644 | |||
276 | --- a/src/tests/unit/test_telegraf.py | |||
277 | +++ b/src/tests/unit/test_telegraf.py | |||
278 | @@ -753,12 +753,10 @@ def test_prometheus_global(monkeypatch, config): | |||
279 | 753 | monkeypatch.setattr(telegraf.hookenv, "open_port", lambda p: open_ports.add(p)) | 753 | monkeypatch.setattr(telegraf.hookenv, "open_port", lambda p: open_ports.add(p)) |
280 | 754 | monkeypatch.setattr(telegraf.hookenv, "close_port", lambda p: open_ports.remove(p)) | 754 | monkeypatch.setattr(telegraf.hookenv, "close_port", lambda p: open_ports.remove(p)) |
281 | 755 | config["prometheus_output_port"] = "default" | 755 | config["prometheus_output_port"] = "default" |
288 | 756 | telegraf.configure_telegraf() | 756 | telegraf.configure_prometheus_client() |
289 | 757 | expected = """ | 757 | expected = '''[[outputs.prometheus_client]] |
290 | 758 | [[outputs.prometheus_client]] | 758 | listen = ":9103"''' |
291 | 759 | listen = ":9103" | 759 | config_file = base_dir().join("telegraf.d", "prometheus_client.conf") |
286 | 760 | """ | ||
287 | 761 | config_file = base_dir().join("telegraf.conf") | ||
292 | 762 | assert expected in config_file.read() | 760 | assert expected in config_file.read() |
293 | 763 | 761 | ||
294 | 764 | 762 | ||
295 | @@ -777,15 +775,13 @@ outputs: | |||
296 | 777 | cpu: ["cpu0"] | 775 | cpu: ["cpu0"] |
297 | 778 | 776 | ||
298 | 779 | """ | 777 | """ |
302 | 780 | telegraf.configure_telegraf() | 778 | telegraf.configure_prometheus_client() |
303 | 781 | expected = """ | 779 | expected = """[[outputs.prometheus_client]] |
301 | 782 | [[outputs.prometheus_client]] | ||
304 | 783 | listen = ":9103" | 780 | listen = ":9103" |
305 | 784 | namedrop = ["aerospike*"] | 781 | namedrop = ["aerospike*"] |
306 | 785 | [outputs.prometheus_client.tagpass] | 782 | [outputs.prometheus_client.tagpass] |
310 | 786 | cpu = ["cpu0"] | 783 | cpu = ["cpu0"]""" |
311 | 787 | """ | 784 | config_file = base_dir().join("telegraf.d", "prometheus_client.conf") |
309 | 788 | config_file = base_dir().join("telegraf.conf") | ||
312 | 789 | assert expected in config_file.read() | 785 | assert expected in config_file.read() |
313 | 790 | 786 | ||
314 | 791 | 787 | ||
315 | @@ -1076,11 +1072,13 @@ def test_influxdb_api_output(monkeypatch, config): | |||
316 | 1076 | 1072 | ||
317 | 1077 | 1073 | ||
318 | 1078 | def test_prometheus_client_output(mocker, monkeypatch, config): | 1074 | def test_prometheus_client_output(mocker, monkeypatch, config): |
320 | 1079 | config["prometheus_output_port"] = "" # Not enabled globally | 1075 | config["prometheus_output_port"] = "" # Not configured globally |
321 | 1080 | monkeypatch.setattr(telegraf.hookenv, "open_port", lambda p: None) | 1076 | monkeypatch.setattr(telegraf.hookenv, "open_port", lambda p: None) |
322 | 1081 | 1077 | ||
323 | 1082 | relation_ids = ["prometheus-client:0"] | 1078 | relation_ids = ["prometheus-client:0"] |
324 | 1079 | related_units = ["prometheus/0"] | ||
325 | 1083 | monkeypatch.setattr(telegraf.hookenv, "relation_ids", lambda r: relation_ids) | 1080 | monkeypatch.setattr(telegraf.hookenv, "relation_ids", lambda r: relation_ids) |
326 | 1081 | monkeypatch.setattr(telegraf.hookenv, "related_units", lambda r: related_units) | ||
327 | 1084 | 1082 | ||
328 | 1085 | network_get_primary_address = mocker.patch.object( | 1083 | network_get_primary_address = mocker.patch.object( |
329 | 1086 | telegraf.hookenv, "network_get_primary_address", return_value="foo" | 1084 | telegraf.hookenv, "network_get_primary_address", return_value="foo" |
330 | @@ -1089,32 +1087,33 @@ def test_prometheus_client_output(mocker, monkeypatch, config): | |||
331 | 1089 | mocker.patch.object( | 1087 | mocker.patch.object( |
332 | 1090 | telegraf.hookenv, "network_get", return_value={"ingress_addresses": ["1.2.3.4"]} | 1088 | telegraf.hookenv, "network_get", return_value={"ingress_addresses": ["1.2.3.4"]} |
333 | 1091 | ) | 1089 | ) |
334 | 1090 | mocker.patch.object(telegraf.hookenv, "relation_get", return_value="5.6.7.8/32") | ||
335 | 1092 | 1091 | ||
336 | 1093 | interface = mocker.Mock(spec=RelationBase) | 1092 | interface = mocker.Mock(spec=RelationBase) |
337 | 1094 | interface.configure = mocker.Mock() | 1093 | interface.configure = mocker.Mock() |
339 | 1095 | telegraf.prometheus_client(interface) | 1094 | telegraf.configure_prometheus_client_with_relation(interface) |
340 | 1096 | expected = """ | 1095 | expected = """ |
341 | 1097 | [[outputs.prometheus_client]] | 1096 | [[outputs.prometheus_client]] |
342 | 1098 | listen = ":9126" | 1097 | listen = ":9126" |
343 | 1099 | """ | 1098 | """ |
344 | 1100 | assert ( | 1099 | assert ( |
346 | 1101 | configs_dir().join("prometheus-client.conf").read().strip() == expected.strip() | 1100 | configs_dir().join("prometheus_client.conf").read().strip() == expected.strip() |
347 | 1102 | ) | 1101 | ) |
348 | 1103 | network_get_primary_address.assert_called_once_with("prometheus-client") | 1102 | network_get_primary_address.assert_called_once_with("prometheus-client") |
349 | 1104 | interface.configure.assert_called_once_with( | 1103 | interface.configure.assert_called_once_with( |
351 | 1105 | 9126, hostname="foo", private_address="foo" | 1104 | "9126", hostname="foo", private_address="foo" |
352 | 1106 | ) | 1105 | ) |
353 | 1107 | 1106 | ||
354 | 1108 | 1107 | ||
355 | 1109 | def test_prometheus_client_output_departed(mocker, monkeypatch, config): | 1108 | def test_prometheus_client_output_departed(mocker, monkeypatch, config): |
357 | 1110 | configs_dir().join("prometheus-client.conf").write("empty") | 1109 | configs_dir().join("prometheus_client.conf").write("empty") |
358 | 1111 | relations = [1] | 1110 | relations = [1] |
359 | 1112 | monkeypatch.setattr(telegraf.hookenv, "relations_of_type", lambda n: relations) | 1111 | monkeypatch.setattr(telegraf.hookenv, "relations_of_type", lambda n: relations) |
360 | 1113 | telegraf.prometheus_client_departed() | 1112 | telegraf.prometheus_client_departed() |
362 | 1114 | assert configs_dir().join("prometheus-client.conf").exists() | 1113 | assert configs_dir().join("prometheus_client.conf").exists() |
363 | 1115 | relations.pop() | 1114 | relations.pop() |
364 | 1116 | telegraf.prometheus_client_departed() | 1115 | telegraf.prometheus_client_departed() |
366 | 1117 | assert not configs_dir().join("prometheus-client.conf").exists() | 1116 | assert not configs_dir().join("prometheus_client.conf").exists() |
367 | 1118 | 1117 | ||
368 | 1119 | 1118 | ||
369 | 1120 | # Integration tests (kind of) | 1119 | # Integration tests (kind of) |
370 | @@ -1208,15 +1207,16 @@ def test_restart_on_output_plugin_relation_departed(mocker, monkeypatch, config) | |||
371 | 1208 | ) | 1207 | ) |
372 | 1209 | config["prometheus_output_port"] = "" | 1208 | config["prometheus_output_port"] = "" |
373 | 1210 | bus.discover() | 1209 | bus.discover() |
374 | 1210 | set_flag("telegraf.installed") | ||
375 | 1211 | set_flag("snap.telegraf.installed") | 1211 | set_flag("snap.telegraf.installed") |
376 | 1212 | set_flag("telegraf.configured") | 1212 | set_flag("telegraf.configured") |
377 | 1213 | interface = mocker.Mock(spec=RelationBase) | 1213 | interface = mocker.Mock(spec=RelationBase) |
378 | 1214 | interface.configure = mocker.Mock() | 1214 | interface.configure = mocker.Mock() |
381 | 1215 | telegraf.prometheus_client(interface) | 1215 | telegraf.configure_prometheus_client_with_relation(interface) |
382 | 1216 | assert configs_dir().join("prometheus-client.conf").exists() | 1216 | assert configs_dir().join("prometheus_client.conf").exists() |
383 | 1217 | # dispatch, file should be gone and telegraf restarted. | 1217 | # dispatch, file should be gone and telegraf restarted. |
384 | 1218 | bus.dispatch() | 1218 | bus.dispatch() |
386 | 1219 | assert not configs_dir().join("prometheus-client.conf").exists() | 1219 | assert not configs_dir().join("prometheus_client.conf").exists() |
387 | 1220 | service_restart.assert_called_with(telegraf.DEB_SERVICE) | 1220 | service_restart.assert_called_with(telegraf.DEB_SERVICE) |
388 | 1221 | 1221 | ||
389 | 1222 | 1222 |
passes make test