Merge ~barryprice/charm-telegraf:split_prometheus_client_config into charm-telegraf:master
- Git
- lp:~barryprice/charm-telegraf
- split_prometheus_client_config
- Merge into master
Proposed by
Barry Price
Status: | Merged |
---|---|
Approved by: | Barry Price |
Approved revision: | 6296374c10538503897d0bebd576742e5fe414e2 |
Merged at revision: | 681fc188e8d79ae8420393be2b00d62d76099f7b |
Proposed branch: | ~barryprice/charm-telegraf:split_prometheus_client_config |
Merge into: | charm-telegraf:master |
Diff against target: |
382 lines (+132/-73) 4 files modified
src/reactive/telegraf.py (+107/-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 |
---|---|---|---|
Haw Loeung | +1 | Approve | |
Canonical IS Reviewers | Pending | ||
BootStack Reviewers | Pending | ||
Review via email: mp+398218@code.launchpad.net |
This proposal supersedes a proposal from 2021-01-27.
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
To post a comment you must log in.
Revision history for this message
Laurent Sesquès (sajoupa) wrote : Posted in a previous version of this proposal | # |
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : Posted in a previous version of this proposal | # |
This merge proposal is being monitored by mergebot. Change the status to Approved to merge.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : Posted in a previous version of this proposal | # |
Unable to determine commit message from repository - please click "Set commit message" and enter the commit message manually.
Revision history for this message
Haw Loeung (hloeung) : Posted in a previous version of this proposal | # |
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : | # |
This merge proposal is being monitored by mergebot. Change the status to Approved to merge.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : | # |
Change successfully merged at revision 681fc188e8d79ae
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | diff --git a/src/reactive/telegraf.py b/src/reactive/telegraf.py | |||
2 | index d8fd268..82cf10e 100644 | |||
3 | --- a/src/reactive/telegraf.py | |||
4 | +++ b/src/reactive/telegraf.py | |||
5 | @@ -183,7 +183,14 @@ def list_config_files(): | |||
6 | 183 | current_states = get_states() | 183 | current_states = get_states() |
7 | 184 | 184 | ||
8 | 185 | for plugin in list_supported_plugins(): | 185 | for plugin in list_supported_plugins(): |
10 | 186 | if "plugins.{}.configured".format(plugin) in current_states.keys(): | 186 | # The prometheus_client plugin can be configured either from a relation or |
11 | 187 | # from the juju config | ||
12 | 188 | if ("plugins.{}.configured".format(plugin) in current_states.keys()) or ( | ||
13 | 189 | "{}.configured".format(plugin) in current_states.keys() | ||
14 | 190 | ): | ||
15 | 191 | # The "prometheus-client" relation sets the "prometheus_client" plugin | ||
16 | 192 | if plugin == "prometheus-client": | ||
17 | 193 | plugin = "prometheus_client" | ||
18 | 187 | config_path = "{}/{}.conf".format(get_configs_dir(), plugin) | 194 | config_path = "{}/{}.conf".format(get_configs_dir(), plugin) |
19 | 188 | config_files.append(config_path) | 195 | config_files.append(config_path) |
20 | 189 | 196 | ||
21 | @@ -527,9 +534,6 @@ def configure_telegraf(): # noqa: C901 | |||
22 | 527 | ) | 534 | ) |
23 | 528 | context["logfile"] = "" | 535 | context["logfile"] = "" |
24 | 529 | 536 | ||
25 | 530 | if get_prometheus_port(): | ||
26 | 531 | context["prometheus_output_port"] = get_prometheus_port() | ||
27 | 532 | check_prometheus_port("prometheus_output", get_prometheus_port()) | ||
28 | 533 | context["extra_options"] = get_extra_options() | 537 | context["extra_options"] = get_extra_options() |
29 | 534 | 538 | ||
30 | 535 | if get_socket_listener_port(): | 539 | if get_socket_listener_port(): |
31 | @@ -645,6 +649,7 @@ def install_telegraf(): | |||
32 | 645 | 649 | ||
33 | 646 | @when("telegraf.installed") | 650 | @when("telegraf.installed") |
34 | 647 | @when("apt.installed.telegraf") | 651 | @when("apt.installed.telegraf") |
35 | 652 | @when("plugins.prometheus-client.configured") | ||
36 | 648 | @when_not("telegraf.configured") | 653 | @when_not("telegraf.configured") |
37 | 649 | @when_not("telegraf.apt.configured") | 654 | @when_not("telegraf.apt.configured") |
38 | 650 | def configure_telegraf_deb(): | 655 | def configure_telegraf_deb(): |
39 | @@ -653,6 +658,7 @@ def configure_telegraf_deb(): | |||
40 | 653 | 658 | ||
41 | 654 | @when("telegraf.installed") | 659 | @when("telegraf.installed") |
42 | 655 | @when("snap.installed.telegraf") | 660 | @when("snap.installed.telegraf") |
43 | 661 | @when("plugins.prometheus-client.configured") | ||
44 | 656 | @when_not("telegraf.configured") | 662 | @when_not("telegraf.configured") |
45 | 657 | @when_not("telegraf.snap.configured") | 663 | @when_not("telegraf.snap.configured") |
46 | 658 | def configure_telegraf_snap(): | 664 | def configure_telegraf_snap(): |
47 | @@ -677,6 +683,7 @@ def handle_config_changes(): | |||
48 | 677 | if config.changed("extra_options"): | 683 | if config.changed("extra_options"): |
49 | 678 | for plugin in list_supported_plugins(): | 684 | for plugin in list_supported_plugins(): |
50 | 679 | clear_flag("plugins.{}.configured".format(plugin)) | 685 | clear_flag("plugins.{}.configured".format(plugin)) |
51 | 686 | clear_flag("prometheus-client.relation.configured") | ||
52 | 680 | # if something else changed, let's reconfigure telegraf itself just in case | 687 | # if something else changed, let's reconfigure telegraf itself just in case |
53 | 681 | 688 | ||
54 | 682 | if config.changed("extra_plugins"): | 689 | if config.changed("extra_plugins"): |
55 | @@ -689,6 +696,12 @@ def handle_config_changes(): | |||
56 | 689 | ): | 696 | ): |
57 | 690 | clear_flag("telegraf.installed") | 697 | clear_flag("telegraf.installed") |
58 | 691 | clear_flag("extra_plugins.configured") | 698 | clear_flag("extra_plugins.configured") |
59 | 699 | clear_flag("plugins.prometheus-client.configured") | ||
60 | 700 | clear_flag("prometheus-client.relation.configured") | ||
61 | 701 | |||
62 | 702 | if config.changed("prometheus_output_port"): | ||
63 | 703 | clear_flag("plugins.prometheus-client.configured") | ||
64 | 704 | clear_flag("prometheus-client.relation.configured") | ||
65 | 692 | clear_flag("telegraf.configured") | 705 | clear_flag("telegraf.configured") |
66 | 693 | clear_flag("telegraf.apt.configured") | 706 | clear_flag("telegraf.apt.configured") |
67 | 694 | clear_flag("telegraf.snap.configured") | 707 | clear_flag("telegraf.snap.configured") |
68 | @@ -1207,59 +1220,108 @@ def influxdb_api_output(influxdb): | |||
69 | 1207 | set_flag("telegraf.needs_reload") | 1220 | set_flag("telegraf.needs_reload") |
70 | 1208 | 1221 | ||
71 | 1209 | 1222 | ||
72 | 1223 | def generate_prometheus_output_config(prometheus_output_port): | ||
73 | 1224 | # If extra_options are set for prometheus_client, let's integrate them | ||
74 | 1225 | extra_options = get_extra_options() | ||
75 | 1226 | options = extra_options["outputs"].get("prometheus_client", {}) | ||
76 | 1227 | listen = options.pop("listen", None) | ||
77 | 1228 | if not listen: | ||
78 | 1229 | listen = ":{}".format(prometheus_output_port) | ||
79 | 1230 | elif int(listen.split(":", 1)[1]) != prometheus_output_port: | ||
80 | 1231 | hookenv.log( | ||
81 | 1232 | """prometheus_output_port is {}, but extra_options would set it | ||
82 | 1233 | to {}. Choosing {} from prometheus_output_port.""".format( | ||
83 | 1234 | prometheus_output_port, | ||
84 | 1235 | int(listen.split(":", 1)[1]), | ||
85 | 1236 | prometheus_output_port, | ||
86 | 1237 | ), | ||
87 | 1238 | level=hookenv.WARNING, | ||
88 | 1239 | ) | ||
89 | 1240 | listen = "{}:{}".format(listen.split(":", 1)[0], prometheus_output_port) | ||
90 | 1241 | |||
91 | 1242 | return { | ||
92 | 1243 | "listen": listen, | ||
93 | 1244 | "extra_options": render_extra_options( | ||
94 | 1245 | "outputs", "prometheus_client", extra_options=extra_options | ||
95 | 1246 | ), | ||
96 | 1247 | } | ||
97 | 1248 | |||
98 | 1249 | |||
99 | 1250 | def render_prometheus_client_config(port): | ||
100 | 1251 | config_path = "{}/{}.conf".format(get_configs_dir(), "prometheus_client") | ||
101 | 1252 | hookenv.log( | ||
102 | 1253 | "Updating {} plugin config file. Port is {}".format("prometheus_client", port), | ||
103 | 1254 | level=hookenv.INFO, | ||
104 | 1255 | ) | ||
105 | 1256 | context = generate_prometheus_output_config(port) | ||
106 | 1257 | render( | ||
107 | 1258 | source="prometheus_client.tmpl", | ||
108 | 1259 | templates_dir=get_templates_dir(), | ||
109 | 1260 | target=config_path, | ||
110 | 1261 | context=context, | ||
111 | 1262 | ) | ||
112 | 1263 | |||
113 | 1264 | |||
114 | 1210 | @when("prometheus-client.available") | 1265 | @when("prometheus-client.available") |
121 | 1211 | @when("telegraf.installed") | 1266 | @when_not("prometheus-client.relation.configured") |
122 | 1212 | def prometheus_client(prometheus): | 1267 | def configure_prometheus_client_with_relation(prometheus): |
123 | 1213 | template = """ | 1268 | hookenv.log( |
124 | 1214 | [[outputs.prometheus_client]] | 1269 | "Configuring prometheus_client output plugin, with prometheus-client relation", |
125 | 1215 | listen = "{{ listen }}" | 1270 | level=hookenv.DEBUG, |
126 | 1216 | """ | 1271 | ) |
127 | 1272 | port = get_prometheus_port() or "9126" | ||
128 | 1273 | # We'll iterate through the prometheus-client relation counterparts, | ||
129 | 1274 | # inform them of our address so that they scrape it, and get their egress subnets | ||
130 | 1275 | # so that we can allow them | ||
131 | 1276 | remote_egress_subnets = [] | ||
132 | 1217 | for relation_id in hookenv.relation_ids("prometheus-client"): | 1277 | for relation_id in hookenv.relation_ids("prometheus-client"): |
133 | 1218 | # if juju 2.x+ then we'll attempt to get the network space address | 1278 | # if juju 2.x+ then we'll attempt to get the network space address |
134 | 1219 | try: | 1279 | try: |
136 | 1220 | hookenv.log("Network Info") | 1280 | hookenv.log("Getting local network info", level=hookenv.DEBUG) |
137 | 1221 | network_info = hookenv.network_get( | 1281 | network_info = hookenv.network_get( |
138 | 1222 | "prometheus-client", relation_id=relation_id | 1282 | "prometheus-client", relation_id=relation_id |
139 | 1223 | ) | 1283 | ) |
141 | 1224 | hookenv.log(network_info) | 1284 | hookenv.log(network_info, level=hookenv.DEBUG) |
142 | 1225 | if "ingress-addresses" in network_info: | 1285 | if "ingress-addresses" in network_info: |
143 | 1226 | ip_addr = network_info.get("ingress-addresses")[0] | 1286 | ip_addr = network_info.get("ingress-addresses")[0] |
144 | 1227 | else: | 1287 | else: |
145 | 1228 | ip_addr = hookenv.network_get_primary_address("prometheus-client") | 1288 | ip_addr = hookenv.network_get_primary_address("prometheus-client") |
146 | 1289 | for unit in hookenv.related_units(relation_id): | ||
147 | 1290 | hookenv.log( | ||
148 | 1291 | "Getting remote egress subnet for relation {} - {}".format( | ||
149 | 1292 | unit, relation_id | ||
150 | 1293 | ), | ||
151 | 1294 | level=hookenv.DEBUG, | ||
152 | 1295 | ) | ||
153 | 1296 | remote_egress_subnets.append( | ||
154 | 1297 | hookenv.relation_get("egress-subnets", unit, relation_id) | ||
155 | 1298 | ) | ||
156 | 1229 | except NotImplementedError: | 1299 | except NotImplementedError: |
157 | 1230 | # if that fails, just let prometheus.configure(...) do it's default | 1300 | # if that fails, just let prometheus.configure(...) do it's default |
158 | 1231 | ip_addr = None | 1301 | ip_addr = None |
159 | 1232 | if get_prometheus_port(): | ||
160 | 1233 | hookenv.log("Prometheus configured globally, skipping plugin setup") | ||
161 | 1234 | prometheus.configure( | ||
162 | 1235 | get_prometheus_port(), hostname=ip_addr, private_address=ip_addr | ||
163 | 1236 | ) | ||
164 | 1237 | set_flag("prometheus-client.configured") | ||
165 | 1238 | # bail out, nothing more need to be configured here | ||
166 | 1239 | return | ||
167 | 1240 | port = 9126 | ||
168 | 1241 | extra_options = get_extra_options() | ||
169 | 1242 | options = extra_options["outputs"].get("prometheus-client", {}) | ||
170 | 1243 | listen = options.pop("listen", None) | ||
171 | 1244 | if listen is not None: | ||
172 | 1245 | hookenv.log( | ||
173 | 1246 | "Configuring prometheus_client plugin to listen on: '{}'".format(listen) | ||
174 | 1247 | ) | ||
175 | 1248 | port = int(listen.split(":", 1)[1]) | ||
176 | 1249 | else: | ||
177 | 1250 | listen = ":{}".format(port) | ||
178 | 1251 | check_prometheus_port("prometheus_output", port) | ||
179 | 1252 | prometheus.configure(port, hostname=ip_addr, private_address=ip_addr) | 1302 | prometheus.configure(port, hostname=ip_addr, private_address=ip_addr) |
187 | 1253 | config_path = "{}/{}.conf".format(get_configs_dir(), "prometheus-client") | 1303 | check_prometheus_port("prometheus_output", port) |
188 | 1254 | hookenv.log("Updating {} plugin config file".format("prometheus-client")) | 1304 | render_prometheus_client_config(port) |
189 | 1255 | context = {"listen": listen} | 1305 | set_flag("plugins.prometheus-client.configured") |
190 | 1256 | content = render_template(template, context) + render_extra_options( | 1306 | set_flag("prometheus-client.relation.configured") |
191 | 1257 | "outputs", "prometheus_client", extra_options=extra_options | 1307 | set_flag("telegraf.needs_reload") |
192 | 1258 | ) | 1308 | |
193 | 1259 | host.write_file(config_path, content.encode("utf-8")) | 1309 | |
194 | 1310 | @when_not("prometheus-client.available") | ||
195 | 1311 | @when_not("plugins.prometheus-client.configured") | ||
196 | 1312 | def configure_prometheus_client(): | ||
197 | 1313 | hookenv.log("Configuring prometheus_client output plugin", level=hookenv.DEBUG) | ||
198 | 1314 | if get_prometheus_port(): | ||
199 | 1315 | port = get_prometheus_port() | ||
200 | 1316 | else: | ||
201 | 1317 | # No relation to prometheus, no port configured: do not configure the plugin | ||
202 | 1260 | set_flag("plugins.prometheus-client.configured") | 1318 | set_flag("plugins.prometheus-client.configured") |
205 | 1261 | set_flag("telegraf.needs_reload") | 1319 | return |
206 | 1262 | set_flag("prometheus-client.configured") | 1320 | check_prometheus_port("prometheus_output", port) |
207 | 1321 | render_prometheus_client_config(port) | ||
208 | 1322 | set_flag("plugins.prometheus-client.configured") | ||
209 | 1323 | set_flag("telegraf.needs_reload") | ||
210 | 1324 | clear_flag("prometheus-client.relation.configured") | ||
211 | 1263 | 1325 | ||
212 | 1264 | 1326 | ||
213 | 1265 | def convert_days(time_string): | 1327 | def convert_days(time_string): |
214 | @@ -1317,17 +1379,15 @@ def render_prometheus_rules(prometheus_rules): | |||
215 | 1317 | 1379 | ||
216 | 1318 | 1380 | ||
217 | 1319 | @when_not("prometheus-client.available") | 1381 | @when_not("prometheus-client.available") |
219 | 1320 | @when("plugins.prometheus-client.configured") | 1382 | @when("prometheus-client.relation.configured") |
220 | 1321 | def prometheus_client_departed(): | 1383 | def prometheus_client_departed(): |
221 | 1322 | hookenv.log("prometheus-client relation not available") | 1384 | hookenv.log("prometheus-client relation not available") |
223 | 1323 | config_path = "{}/{}.conf".format(get_configs_dir(), "prometheus-client") | 1385 | config_path = "{}/{}.conf".format(get_configs_dir(), "prometheus_client") |
224 | 1324 | rels = hookenv.relations_of_type("prometheus-client") | 1386 | rels = hookenv.relations_of_type("prometheus-client") |
225 | 1325 | if not rels and os.path.exists(config_path): | 1387 | if not rels and os.path.exists(config_path): |
226 | 1326 | hookenv.log("Deleting {} plugin config file".format("prometheus-client")) | 1388 | hookenv.log("Deleting {} plugin config file".format("prometheus-client")) |
227 | 1327 | os.unlink(config_path) | 1389 | os.unlink(config_path) |
228 | 1328 | clear_flag("plugins.prometheus-client.configured") | 1390 | clear_flag("plugins.prometheus-client.configured") |
229 | 1329 | set_flag("telegraf.needs_reload") | ||
230 | 1330 | clear_flag("prometheus-client.configured") | ||
231 | 1331 | 1391 | ||
232 | 1332 | 1392 | ||
233 | 1333 | @when( | 1393 | @when( |
234 | @@ -1406,6 +1466,8 @@ def grafana_dashboard_import_failed(): | |||
235 | 1406 | 1466 | ||
236 | 1407 | 1467 | ||
237 | 1408 | @when("telegraf.needs_reload") | 1468 | @when("telegraf.needs_reload") |
238 | 1469 | @when("telegraf.installed") | ||
239 | 1470 | @when("telegraf.configured") | ||
240 | 1409 | def start_or_restart(): | 1471 | def start_or_restart(): |
241 | 1410 | states = sorted( | 1472 | states = sorted( |
242 | 1411 | [ | 1473 | [ |
243 | diff --git a/src/templates/prometheus_client.tmpl b/src/templates/prometheus_client.tmpl | |||
244 | 1412 | new file mode 100644 | 1474 | new file mode 100644 |
245 | index 0000000..add2099 | |||
246 | --- /dev/null | |||
247 | +++ b/src/templates/prometheus_client.tmpl | |||
248 | @@ -0,0 +1,3 @@ | |||
249 | 1 | [[outputs.prometheus_client]] | ||
250 | 2 | listen = "{{ listen }}" | ||
251 | 3 | {%- if extra_options %}{{ extra_options }}{%- endif %} | ||
252 | diff --git a/src/templates/telegraf.conf.tmpl b/src/templates/telegraf.conf.tmpl | |||
253 | index af59ed5..966b4a9 100644 | |||
254 | --- a/src/templates/telegraf.conf.tmpl | |||
255 | +++ b/src/templates/telegraf.conf.tmpl | |||
256 | @@ -80,12 +80,6 @@ | |||
257 | 80 | 80 | ||
258 | 81 | {{ outputs }} | 81 | {{ outputs }} |
259 | 82 | 82 | ||
260 | 83 | {% if prometheus_output_port %} | ||
261 | 84 | [[outputs.prometheus_client]] | ||
262 | 85 | listen = ":{{ prometheus_output_port }}" | ||
263 | 86 | {%- if extra_options['outputs']['prometheus_client'] %}{{ render_options('prometheus_client', 'outputs', extra_options['outputs']) }}{%- endif %} | ||
264 | 87 | {%- endif %} | ||
265 | 88 | |||
266 | 89 | ############################################################################### | 83 | ############################################################################### |
267 | 90 | # INPUTS # | 84 | # INPUTS # |
268 | 91 | ############################################################################### | 85 | ############################################################################### |
269 | diff --git a/src/tests/unit/test_telegraf.py b/src/tests/unit/test_telegraf.py | |||
270 | index 62a5595..d1ecd6b 100644 | |||
271 | --- a/src/tests/unit/test_telegraf.py | |||
272 | +++ b/src/tests/unit/test_telegraf.py | |||
273 | @@ -809,12 +809,10 @@ def test_prometheus_global(monkeypatch, config): | |||
274 | 809 | monkeypatch.setattr(telegraf.hookenv, "open_port", lambda p: open_ports.add(p)) | 809 | monkeypatch.setattr(telegraf.hookenv, "open_port", lambda p: open_ports.add(p)) |
275 | 810 | monkeypatch.setattr(telegraf.hookenv, "close_port", lambda p: open_ports.remove(p)) | 810 | monkeypatch.setattr(telegraf.hookenv, "close_port", lambda p: open_ports.remove(p)) |
276 | 811 | config["prometheus_output_port"] = "default" | 811 | config["prometheus_output_port"] = "default" |
283 | 812 | telegraf.configure_telegraf() | 812 | telegraf.configure_prometheus_client() |
284 | 813 | expected = """ | 813 | expected = '''[[outputs.prometheus_client]] |
285 | 814 | [[outputs.prometheus_client]] | 814 | listen = ":9103"''' |
286 | 815 | listen = ":9103" | 815 | config_file = base_dir().join("telegraf.d", "prometheus_client.conf") |
281 | 816 | """ | ||
282 | 817 | config_file = base_dir().join("telegraf.conf") | ||
287 | 818 | assert expected in config_file.read() | 816 | assert expected in config_file.read() |
288 | 819 | 817 | ||
289 | 820 | 818 | ||
290 | @@ -833,15 +831,13 @@ outputs: | |||
291 | 833 | cpu: ["cpu0"] | 831 | cpu: ["cpu0"] |
292 | 834 | 832 | ||
293 | 835 | """ | 833 | """ |
297 | 836 | telegraf.configure_telegraf() | 834 | telegraf.configure_prometheus_client() |
298 | 837 | expected = """ | 835 | expected = """[[outputs.prometheus_client]] |
296 | 838 | [[outputs.prometheus_client]] | ||
299 | 839 | listen = ":9103" | 836 | listen = ":9103" |
300 | 840 | namedrop = ["aerospike*"] | 837 | namedrop = ["aerospike*"] |
301 | 841 | [outputs.prometheus_client.tagpass] | 838 | [outputs.prometheus_client.tagpass] |
305 | 842 | cpu = ["cpu0"] | 839 | cpu = ["cpu0"]""" |
306 | 843 | """ | 840 | config_file = os.path.join(base_dir(), "telegraf.d", "prometheus_client.conf") |
304 | 844 | config_file = base_dir().join("telegraf.conf") | ||
307 | 845 | assert expected in config_file.read() | 841 | assert expected in config_file.read() |
308 | 846 | 842 | ||
309 | 847 | 843 | ||
310 | @@ -1132,11 +1128,13 @@ def test_influxdb_api_output(monkeypatch, config): | |||
311 | 1132 | 1128 | ||
312 | 1133 | 1129 | ||
313 | 1134 | def test_prometheus_client_output(mocker, monkeypatch, config): | 1130 | def test_prometheus_client_output(mocker, monkeypatch, config): |
315 | 1135 | config["prometheus_output_port"] = "" # Not enabled globally | 1131 | config["prometheus_output_port"] = "" # Not configured globally |
316 | 1136 | monkeypatch.setattr(telegraf.hookenv, "open_port", lambda p: None) | 1132 | monkeypatch.setattr(telegraf.hookenv, "open_port", lambda p: None) |
317 | 1137 | 1133 | ||
318 | 1138 | relation_ids = ["prometheus-client:0"] | 1134 | relation_ids = ["prometheus-client:0"] |
319 | 1135 | related_units = ["prometheus/0"] | ||
320 | 1139 | monkeypatch.setattr(telegraf.hookenv, "relation_ids", lambda r: relation_ids) | 1136 | monkeypatch.setattr(telegraf.hookenv, "relation_ids", lambda r: relation_ids) |
321 | 1137 | monkeypatch.setattr(telegraf.hookenv, "related_units", lambda r: related_units) | ||
322 | 1140 | 1138 | ||
323 | 1141 | network_get_primary_address = mocker.patch.object( | 1139 | network_get_primary_address = mocker.patch.object( |
324 | 1142 | telegraf.hookenv, "network_get_primary_address", return_value="foo" | 1140 | telegraf.hookenv, "network_get_primary_address", return_value="foo" |
325 | @@ -1145,32 +1143,33 @@ def test_prometheus_client_output(mocker, monkeypatch, config): | |||
326 | 1145 | mocker.patch.object( | 1143 | mocker.patch.object( |
327 | 1146 | telegraf.hookenv, "network_get", return_value={"ingress_addresses": ["1.2.3.4"]} | 1144 | telegraf.hookenv, "network_get", return_value={"ingress_addresses": ["1.2.3.4"]} |
328 | 1147 | ) | 1145 | ) |
329 | 1146 | mocker.patch.object(telegraf.hookenv, "relation_get", return_value="5.6.7.8/32") | ||
330 | 1148 | 1147 | ||
331 | 1149 | interface = mocker.Mock(spec=RelationBase) | 1148 | interface = mocker.Mock(spec=RelationBase) |
332 | 1150 | interface.configure = mocker.Mock() | 1149 | interface.configure = mocker.Mock() |
334 | 1151 | telegraf.prometheus_client(interface) | 1150 | telegraf.configure_prometheus_client_with_relation(interface) |
335 | 1152 | expected = """ | 1151 | expected = """ |
336 | 1153 | [[outputs.prometheus_client]] | 1152 | [[outputs.prometheus_client]] |
337 | 1154 | listen = ":9126" | 1153 | listen = ":9126" |
338 | 1155 | """ | 1154 | """ |
339 | 1156 | assert ( | 1155 | assert ( |
341 | 1157 | configs_dir().join("prometheus-client.conf").read().strip() == expected.strip() | 1156 | configs_dir().join("prometheus_client.conf").read().strip() == expected.strip() |
342 | 1158 | ) | 1157 | ) |
343 | 1159 | network_get_primary_address.assert_called_once_with("prometheus-client") | 1158 | network_get_primary_address.assert_called_once_with("prometheus-client") |
344 | 1160 | interface.configure.assert_called_once_with( | 1159 | interface.configure.assert_called_once_with( |
346 | 1161 | 9126, hostname="foo", private_address="foo" | 1160 | "9126", hostname="foo", private_address="foo" |
347 | 1162 | ) | 1161 | ) |
348 | 1163 | 1162 | ||
349 | 1164 | 1163 | ||
350 | 1165 | def test_prometheus_client_output_departed(mocker, monkeypatch, config): | 1164 | def test_prometheus_client_output_departed(mocker, monkeypatch, config): |
352 | 1166 | configs_dir().join("prometheus-client.conf").write("empty") | 1165 | configs_dir().join("prometheus_client.conf").write("empty") |
353 | 1167 | relations = [1] | 1166 | relations = [1] |
354 | 1168 | monkeypatch.setattr(telegraf.hookenv, "relations_of_type", lambda n: relations) | 1167 | monkeypatch.setattr(telegraf.hookenv, "relations_of_type", lambda n: relations) |
355 | 1169 | telegraf.prometheus_client_departed() | 1168 | telegraf.prometheus_client_departed() |
357 | 1170 | assert configs_dir().join("prometheus-client.conf").exists() | 1169 | assert configs_dir().join("prometheus_client.conf").exists() |
358 | 1171 | relations.pop() | 1170 | relations.pop() |
359 | 1172 | telegraf.prometheus_client_departed() | 1171 | telegraf.prometheus_client_departed() |
361 | 1173 | assert not configs_dir().join("prometheus-client.conf").exists() | 1172 | assert not configs_dir().join("prometheus_client.conf").exists() |
362 | 1174 | 1173 | ||
363 | 1175 | 1174 | ||
364 | 1176 | # Integration tests (kind of) | 1175 | # Integration tests (kind of) |
365 | @@ -1264,15 +1263,16 @@ def test_restart_on_output_plugin_relation_departed(mocker, monkeypatch, config) | |||
366 | 1264 | ) | 1263 | ) |
367 | 1265 | config["prometheus_output_port"] = "" | 1264 | config["prometheus_output_port"] = "" |
368 | 1266 | bus.discover() | 1265 | bus.discover() |
369 | 1266 | set_flag("telegraf.installed") | ||
370 | 1267 | set_flag("snap.telegraf.installed") | 1267 | set_flag("snap.telegraf.installed") |
371 | 1268 | set_flag("telegraf.configured") | 1268 | set_flag("telegraf.configured") |
372 | 1269 | interface = mocker.Mock(spec=RelationBase) | 1269 | interface = mocker.Mock(spec=RelationBase) |
373 | 1270 | interface.configure = mocker.Mock() | 1270 | interface.configure = mocker.Mock() |
376 | 1271 | telegraf.prometheus_client(interface) | 1271 | telegraf.configure_prometheus_client_with_relation(interface) |
377 | 1272 | assert configs_dir().join("prometheus-client.conf").exists() | 1272 | assert configs_dir().join("prometheus_client.conf").exists() |
378 | 1273 | # dispatch, file should be gone and telegraf restarted. | 1273 | # dispatch, file should be gone and telegraf restarted. |
379 | 1274 | bus.dispatch() | 1274 | bus.dispatch() |
381 | 1275 | assert not configs_dir().join("prometheus-client.conf").exists() | 1275 | assert not configs_dir().join("prometheus_client.conf").exists() |
382 | 1276 | service_restart.assert_called_with(telegraf.DEB_SERVICE) | 1276 | service_restart.assert_called_with(telegraf.DEB_SERVICE) |
383 | 1277 | 1277 | ||
384 | 1278 | 1278 |
passes make test