Merge ~sajoupa/charm-telegraf:expose_ip_ranges into charm-telegraf:master
- Git
- lp:~sajoupa/charm-telegraf
- expose_ip_ranges
- Merge into master
Status: | Rejected |
---|---|
Rejected by: | Laurent Sesquès |
Proposed branch: | ~sajoupa/charm-telegraf:expose_ip_ranges |
Merge into: | charm-telegraf:master |
Diff against target: |
495 lines (+193/-78) 6 files modified
src/config.yaml (+6/-0) src/reactive/telegraf.py (+139/-50) src/templates/prometheus_client.tmpl (+4/-0) src/templates/telegraf.conf.tmpl (+0/-6) src/tests/functional/tests/bundles/bionic-monitoring.yaml (+1/-0) src/tests/unit/test_telegraf.py (+43/-22) |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Paul Goins | Needs Information | ||
Canonical IS Reviewers | Pending | ||
Canonical IS Reviewers | Pending | ||
Review via email: mp+396503@code.launchpad.net |
Commit message
Add support for prometheus_
Also fixing:
- allow setting prometheus_
- fix restart condition when the prometheus-client plugin is changed
- make the default port 9103 instead of 9216 when related to prometheus (https:/
Description of the change
Haw Loeung (hloeung) wrote : | # |
Haw Loeung (hloeung) : | # |
Haw Loeung (hloeung) : | # |
Laurent Sesquès (sajoupa) wrote : | # |
Thanks, I addressed them.
Laurent Sesquès (sajoupa) wrote : | # |
(saving comments, hopefully for real this time)
🤖 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.
Paul Goins (vultaire) wrote : | # |
Thanks for the MR.
This seems like a fairly large MR if the intent is purely to address bug #1890102. I wonder if the code can be reduced here? I haven't read the whole MR yet, but I've read 1/3rd to 1/2, and it seems like maybe it's addressing something else as well? Could you explain?
Additionally, as axino noted on the above bug, this can be addressed, albeit more verbose, via the extra_options config value. Is this insufficient? And if it is, could we focus the change on adding the config and updating the template to consume that config value?
To be clear, I'm not trying to brush off these changes - but I am not understanding why all these changes are needed based upon the bug this is connected to. If there are other bugs to be fixed, it'd be better to fix those via additional MRs, in my opinion.
Best Regards,
Paul Goins
Laurent Sesquès (sajoupa) wrote : | # |
Thanks for the review.
We can do about anything with exta_options, but that's not ideal. Also, if we set an IP range manually with extra_options, then to allow related prometheuses, we must manually add their IP addresses to extra_options (and remove them on relation-departed, etc.).
This change also allows setting prometheus_
Laurent Sesquès (sajoupa) wrote : | # |
> https:/
> 9216 is "New Relic exporter", according to IANA it's bacula-sd so I made it
> default to 9103 (admittedly, changing the default could be in a separate MP).
9103 is bacula-sd actually according to IANA. But Prometheus considers it as the collectd port, so still a better default (also the telegraf charm default).
Laurent Sesquès (sajoupa) wrote : | # |
I've split this MP into 4 smaller ones:
https:/
on which depends:
https:/
on which depends:
https:/
on which depends:
https:/
Will mark this one 'rejected'.
Unmerged commits
- a9d2bea... by Laurent Sesquès
-
prometheus_ip_range support: a few code improvements
- fa4e7c9... by Laurent Sesquès
-
add unit tests for prometheus_ip_range
- 68a5407... by Laurent Sesquès
-
remove TODO comment about get_prometheus_
port(), actually not relevant - 689fb2b... by Laurent Sesquès
-
rework flags and wording for prometheus-client
- 8623c5f... by Laurent Sesquès
-
prometheus_client output plugin: fix reactive logic, update unit tests
- 9359340... by Laurent Sesquès
-
revamp prometheus-client output configuration to add ip_range, fix bugs with prometheus relation
Preview Diff
1 | diff --git a/src/config.yaml b/src/config.yaml |
2 | index 66f6105..b94071b 100644 |
3 | --- a/src/config.yaml |
4 | +++ b/src/config.yaml |
5 | @@ -206,3 +206,9 @@ options: |
6 | Note: The complete datasource inserted into dashboard will be |
7 | "$prometheus_datasource - Juju generated source" |
8 | This matches how the Prometheus2/Grafana charms format the datasource. |
9 | + prometheus_ip_range: |
10 | + default: "" |
11 | + type: string |
12 | + description: > |
13 | + If set, the list of IP Ranges which are allowed to access metrics. |
14 | + ex: prometheus_ip_range = "[192.168.0.0/24, 192.168.1.0/30]" |
15 | diff --git a/src/reactive/telegraf.py b/src/reactive/telegraf.py |
16 | index 136c271..fe71d43 100644 |
17 | --- a/src/reactive/telegraf.py |
18 | +++ b/src/reactive/telegraf.py |
19 | @@ -159,7 +159,14 @@ def list_config_files(): |
20 | current_states = get_states() |
21 | |
22 | for plugin in list_supported_plugins(): |
23 | - if "plugins.{}.configured".format(plugin) in current_states.keys(): |
24 | + # The prometheus_client plugin can be configured either from a relation or |
25 | + # from the juju config |
26 | + if ("plugins.{}.configured".format(plugin) in current_states.keys()) or ( |
27 | + "{}.configured".format(plugin) in current_states.keys() |
28 | + ): |
29 | + # The "prometheus-client" relation sets the "prometheus_client" plugin |
30 | + if plugin == "prometheus-client": |
31 | + plugin = "prometheus_client" |
32 | config_path = "{}/{}.conf".format(get_configs_dir(), plugin) |
33 | config_files.append(config_path) |
34 | |
35 | @@ -327,8 +334,14 @@ def get_prometheus_port(): |
36 | return False |
37 | if config.get("prometheus_output_port") == "default": |
38 | return 9103 |
39 | - else: |
40 | - return int(config.get("prometheus_output_port")) |
41 | + return int(config.get("prometheus_output_port")) |
42 | + |
43 | + |
44 | +def get_prometheus_ip_range(): |
45 | + config = hookenv.config() |
46 | + if config.get("prometheus_ip_range") == "": |
47 | + return [] |
48 | + return config.get("prometheus_ip_range").split(",") |
49 | |
50 | |
51 | def get_socket_listener_port(): |
52 | @@ -415,6 +428,7 @@ def update_sysstat_config_with_sdac_xall(path="/etc/sysstat/sysstat"): |
53 | |
54 | |
55 | def configure_telegraf(): # noqa: C901 |
56 | + hookenv.log("Generating telegraf.conf", level=hookenv.DEBUG) |
57 | update_sysstat_config_with_sdac_xall() |
58 | config = hookenv.config() |
59 | context = config.copy() |
60 | @@ -503,9 +517,6 @@ def configure_telegraf(): # noqa: C901 |
61 | ) |
62 | context["logfile"] = "" |
63 | |
64 | - if get_prometheus_port(): |
65 | - context["prometheus_output_port"] = get_prometheus_port() |
66 | - check_prometheus_port("prometheus_output", get_prometheus_port()) |
67 | context["extra_options"] = get_extra_options() |
68 | |
69 | if get_socket_listener_port(): |
70 | @@ -566,10 +577,10 @@ def configure_telegraf(): # noqa: C901 |
71 | context=context, |
72 | ) |
73 | |
74 | + # Make sure that only the right service is enabled, then defer to start_or_restart() |
75 | for service in [DEB_SERVICE, SNAP_SERVICE]: |
76 | if service == get_service(): |
77 | host.service_resume(service) |
78 | - host.service_reload(service) |
79 | else: |
80 | try: |
81 | host.service_pause(service) |
82 | @@ -621,6 +632,7 @@ def install_telegraf(): |
83 | |
84 | @when("telegraf.installed") |
85 | @when("apt.installed.telegraf") |
86 | +@when("plugins.prometheus-client.configured") |
87 | @when_not("telegraf.configured") |
88 | @when_not("telegraf.apt.configured") |
89 | def configure_telegraf_deb(): |
90 | @@ -629,6 +641,7 @@ def configure_telegraf_deb(): |
91 | |
92 | @when("telegraf.installed") |
93 | @when("snap.installed.telegraf") |
94 | +@when("plugins.prometheus-client.configured") |
95 | @when_not("telegraf.configured") |
96 | @when_not("telegraf.snap.configured") |
97 | def configure_telegraf_snap(): |
98 | @@ -653,6 +666,7 @@ def handle_config_changes(): |
99 | if config.changed("extra_options"): |
100 | for plugin in list_supported_plugins(): |
101 | clear_flag("plugins.{}.configured".format(plugin)) |
102 | + clear_flag("prometheus-client.relation.configured") |
103 | # if something else changed, let's reconfigure telegraf itself just in case |
104 | |
105 | if config.changed("extra_plugins"): |
106 | @@ -665,6 +679,14 @@ def handle_config_changes(): |
107 | ): |
108 | clear_flag("telegraf.installed") |
109 | clear_flag("extra_plugins.configured") |
110 | + clear_flag("plugins.prometheus-client.configured") |
111 | + clear_flag("prometheus-client.relation.configured") |
112 | + |
113 | + if config.changed("prometheus_output_port") or config.changed( |
114 | + "prometheus_ip_range" |
115 | + ): |
116 | + clear_flag("plugins.prometheus-client.configured") |
117 | + clear_flag("prometheus-client.relation.configured") |
118 | clear_flag("telegraf.configured") |
119 | clear_flag("telegraf.apt.configured") |
120 | clear_flag("telegraf.snap.configured") |
121 | @@ -983,7 +1005,7 @@ def redis_input(redis): |
122 | [[inputs.redis]] |
123 | servers = ["tcp://{{ host }}:{{ port }}"] |
124 | # Until https://github.com/influxdata/telegraf/issues/5036 is fixed |
125 | - fielddrop = ["aof_last_bgrewrite_status","aof_last_write_status","maxmemory_policy","rdb_last_bgsave_status","used_memory_dataset_perc","used_memory_peak_perc"] |
126 | + fielddrop = ["aof_last_bgrewrite_status","aof_last_write_status","maxmemory_policy","rdb_last_bgsave_status","used_memory_dataset_perc","used_memory_peak_perc"] # noqa E501 |
127 | """ # noqa E501 (inline template) |
128 | config_path = "{}/{}.conf".format(get_configs_dir(), "redis") |
129 | |
130 | @@ -1185,59 +1207,124 @@ def influxdb_api_output(influxdb): |
131 | set_flag("telegraf.needs_reload") |
132 | |
133 | |
134 | +def generate_prometheus_output_config(prometheus_output_port, prometheus_ip_range): |
135 | + # If extra_options are set for prometheus_client, let's integrate them |
136 | + extra_options = get_extra_options() |
137 | + options = extra_options["outputs"].get("prometheus_client", {}) |
138 | + listen = options.pop("listen", None) |
139 | + if not listen: |
140 | + listen = ":{}".format(prometheus_output_port) |
141 | + elif int(listen.split(":", 1)[1]) != prometheus_output_port: |
142 | + hookenv.log( |
143 | + """prometheus_output_port is {}, but extra_options would set it |
144 | + to {}. Choosing {} from prometheus_output_port.""".format( |
145 | + prometheus_output_port, |
146 | + int(listen.split(":", 1)[1]), |
147 | + prometheus_output_port, |
148 | + ), |
149 | + level=hookenv.WARNING, |
150 | + ) |
151 | + listen = "{}:{}".format(listen.split(":", 1)[0], prometheus_output_port) |
152 | + |
153 | + options_ip_range = options.pop("ip_range", []) |
154 | + ip_range = options_ip_range + prometheus_ip_range |
155 | + |
156 | + return { |
157 | + "listen": listen, |
158 | + "ip_range": ip_range, |
159 | + "extra_options": render_extra_options( |
160 | + "outputs", "prometheus_client", extra_options=extra_options |
161 | + ), |
162 | + } |
163 | + |
164 | + |
165 | +def render_prometheus_client_config(port, ip_range): |
166 | + config_path = "{}/{}.conf".format(get_configs_dir(), "prometheus_client") |
167 | + hookenv.log( |
168 | + "Updating {} plugin config file. Port is {} and ip_range is {}".format( |
169 | + "prometheus_client", port, ip_range |
170 | + ), |
171 | + level=hookenv.INFO, |
172 | + ) |
173 | + context = generate_prometheus_output_config(port, ip_range) |
174 | + render( |
175 | + source="prometheus_client.tmpl", |
176 | + templates_dir=get_templates_dir(), |
177 | + target=config_path, |
178 | + context=context, |
179 | + ) |
180 | + |
181 | + |
182 | @when("prometheus-client.available") |
183 | -@when("telegraf.installed") |
184 | -def prometheus_client(prometheus): |
185 | - template = """ |
186 | -[[outputs.prometheus_client]] |
187 | - listen = "{{ listen }}" |
188 | -""" |
189 | +@when_not("prometheus-client.relation.configured") |
190 | +def configure_prometheus_client_with_relation(prometheus): |
191 | + hookenv.log( |
192 | + "Configuring prometheus_client output plugin, with prometheus-client relation", |
193 | + level=hookenv.DEBUG, |
194 | + ) |
195 | + if get_prometheus_port(): |
196 | + port = get_prometheus_port() |
197 | + else: |
198 | + # We have a relation with prometheus, but removed the port number config. |
199 | + # Default to 9103. |
200 | + port = "9103" |
201 | + # We'll iterate through the prometheus-client relation counterparts, |
202 | + # inform them of our address so that they scrape it, and get their egress subnets |
203 | + # so that we can allow them |
204 | + remote_egress_subnets = [] |
205 | for relation_id in hookenv.relation_ids("prometheus-client"): |
206 | # if juju 2.x+ then we'll attempt to get the network space address |
207 | try: |
208 | - hookenv.log("Network Info") |
209 | + hookenv.log("Getting local betwork info", level=hookenv.DEBUG) |
210 | network_info = hookenv.network_get( |
211 | "prometheus-client", relation_id=relation_id |
212 | ) |
213 | - hookenv.log(network_info) |
214 | + hookenv.log(network_info, level=hookenv.DEBUG) |
215 | if "ingress-addresses" in network_info: |
216 | ip_addr = network_info.get("ingress-addresses")[0] |
217 | else: |
218 | ip_addr = hookenv.network_get_primary_address("prometheus-client") |
219 | + for unit in hookenv.related_units(relation_id): |
220 | + hookenv.log( |
221 | + "Getting remote egress subnet for relation {} - {}".format( |
222 | + unit, relation_id |
223 | + ), |
224 | + level=hookenv.DEBUG, |
225 | + ) |
226 | + remote_egress_subnets.append( |
227 | + hookenv.relation_get("egress-subnets", unit, relation_id) |
228 | + ) |
229 | except NotImplementedError: |
230 | # if that fails, just let prometheus.configure(...) do it's default |
231 | ip_addr = None |
232 | - if get_prometheus_port(): |
233 | - hookenv.log("Prometheus configured globally, skipping plugin setup") |
234 | - prometheus.configure( |
235 | - get_prometheus_port(), hostname=ip_addr, private_address=ip_addr |
236 | - ) |
237 | - set_flag("prometheus-client.configured") |
238 | - # bail out, nothing more need to be configured here |
239 | - return |
240 | - port = 9126 |
241 | - extra_options = get_extra_options() |
242 | - options = extra_options["outputs"].get("prometheus-client", {}) |
243 | - listen = options.pop("listen", None) |
244 | - if listen is not None: |
245 | - hookenv.log( |
246 | - "Configuring prometheus_client plugin to listen on: '{}'".format(listen) |
247 | - ) |
248 | - port = int(listen.split(":", 1)[1]) |
249 | - else: |
250 | - listen = ":{}".format(port) |
251 | - check_prometheus_port("prometheus_output", port) |
252 | prometheus.configure(port, hostname=ip_addr, private_address=ip_addr) |
253 | - config_path = "{}/{}.conf".format(get_configs_dir(), "prometheus-client") |
254 | - hookenv.log("Updating {} plugin config file".format("prometheus-client")) |
255 | - context = {"listen": listen} |
256 | - content = render_template(template, context) + render_extra_options( |
257 | - "outputs", "prometheus_client", extra_options=extra_options |
258 | - ) |
259 | - host.write_file(config_path, content.encode("utf-8")) |
260 | + check_prometheus_port("prometheus_output", port) |
261 | + # If prometheus_ip_range is empty, all remote IPs are allowed |
262 | + ip_range = get_prometheus_ip_range() |
263 | + if ip_range != []: |
264 | + ip_range = ip_range + remote_egress_subnets |
265 | + render_prometheus_client_config(port, ip_range) |
266 | + set_flag("plugins.prometheus-client.configured") |
267 | + set_flag("prometheus-client.relation.configured") |
268 | + set_flag("telegraf.needs_reload") |
269 | + |
270 | + |
271 | +@when_not("prometheus-client.available") |
272 | +@when_not("plugins.prometheus-client.configured") |
273 | +def configure_prometheus_client(): |
274 | + hookenv.log("Configuring prometheus_client output plugin", level=hookenv.DEBUG) |
275 | + if get_prometheus_port(): |
276 | + port = get_prometheus_port() |
277 | + else: |
278 | + # No relation tp prometheus, no port configured: do not configure the plugin |
279 | set_flag("plugins.prometheus-client.configured") |
280 | - set_flag("telegraf.needs_reload") |
281 | - set_flag("prometheus-client.configured") |
282 | + return |
283 | + check_prometheus_port("prometheus_output", port) |
284 | + ip_range = get_prometheus_ip_range() |
285 | + render_prometheus_client_config(port, ip_range) |
286 | + set_flag("plugins.prometheus-client.configured") |
287 | + set_flag("telegraf.needs_reload") |
288 | + clear_flag("prometheus-client.relation.configured") |
289 | |
290 | |
291 | def convert_days(time_string): |
292 | @@ -1295,21 +1382,21 @@ def render_prometheus_rules(prometheus_rules): |
293 | |
294 | |
295 | @when_not("prometheus-client.available") |
296 | -@when("plugins.prometheus-client.configured") |
297 | +@when("prometheus-client.relation.configured") |
298 | def prometheus_client_departed(): |
299 | hookenv.log("prometheus-client relation not available") |
300 | - config_path = "{}/{}.conf".format(get_configs_dir(), "prometheus-client") |
301 | + config_path = "{}/{}.conf".format(get_configs_dir(), "prometheus_client") |
302 | rels = hookenv.relations_of_type("prometheus-client") |
303 | if not rels and os.path.exists(config_path): |
304 | hookenv.log("Deleting {} plugin config file".format("prometheus-client")) |
305 | os.unlink(config_path) |
306 | clear_flag("plugins.prometheus-client.configured") |
307 | - set_flag("telegraf.needs_reload") |
308 | - clear_flag("prometheus-client.configured") |
309 | |
310 | |
311 | @when( |
312 | - "prometheus-client.configured", "endpoint.dashboards.joined", "leadership.is_leader" |
313 | + "plugins.prometheus-client.configured", |
314 | + "endpoint.dashboards.joined", |
315 | + "leadership.is_leader", |
316 | ) |
317 | @when_not("grafana.configured") |
318 | def register_grafana_dashboard(): |
319 | @@ -1384,6 +1471,8 @@ def grafana_dashboard_import_failed(): |
320 | |
321 | |
322 | @when("telegraf.needs_reload") |
323 | +@when("telegraf.installed") |
324 | +@when("telegraf.configured") |
325 | def start_or_restart(): |
326 | states = sorted( |
327 | [ |
328 | diff --git a/src/templates/prometheus_client.tmpl b/src/templates/prometheus_client.tmpl |
329 | new file mode 100644 |
330 | index 0000000..a6e3c06 |
331 | --- /dev/null |
332 | +++ b/src/templates/prometheus_client.tmpl |
333 | @@ -0,0 +1,4 @@ |
334 | +[[outputs.prometheus_client]] |
335 | + listen = "{{ listen }}" |
336 | + ip_range = {{ ip_range }} |
337 | +{%- if extra_options %}{{ extra_options }}{%- endif %} |
338 | diff --git a/src/templates/telegraf.conf.tmpl b/src/templates/telegraf.conf.tmpl |
339 | index af59ed5..966b4a9 100644 |
340 | --- a/src/templates/telegraf.conf.tmpl |
341 | +++ b/src/templates/telegraf.conf.tmpl |
342 | @@ -80,12 +80,6 @@ |
343 | |
344 | {{ outputs }} |
345 | |
346 | -{% if prometheus_output_port %} |
347 | -[[outputs.prometheus_client]] |
348 | - listen = ":{{ prometheus_output_port }}" |
349 | - {%- if extra_options['outputs']['prometheus_client'] %}{{ render_options('prometheus_client', 'outputs', extra_options['outputs']) }}{%- endif %} |
350 | -{%- endif %} |
351 | - |
352 | ############################################################################### |
353 | # INPUTS # |
354 | ############################################################################### |
355 | diff --git a/src/tests/functional/tests/bundles/bionic-monitoring.yaml b/src/tests/functional/tests/bundles/bionic-monitoring.yaml |
356 | index 120eab8..c7a487b 100644 |
357 | --- a/src/tests/functional/tests/bundles/bionic-monitoring.yaml |
358 | +++ b/src/tests/functional/tests/bundles/bionic-monitoring.yaml |
359 | @@ -8,6 +8,7 @@ applications: |
360 | num_units: 0 |
361 | options: |
362 | prometheus_datasource: prometheus2 |
363 | + prometheus_ip_range: '0.0.0.0/0' |
364 | grafana: |
365 | charm: cs:grafana |
366 | num_units: 1 |
367 | diff --git a/src/tests/unit/test_telegraf.py b/src/tests/unit/test_telegraf.py |
368 | index afc3625..9b7343b 100644 |
369 | --- a/src/tests/unit/test_telegraf.py |
370 | +++ b/src/tests/unit/test_telegraf.py |
371 | @@ -753,12 +753,11 @@ def test_prometheus_global(monkeypatch, config): |
372 | monkeypatch.setattr(telegraf.hookenv, "open_port", lambda p: open_ports.add(p)) |
373 | monkeypatch.setattr(telegraf.hookenv, "close_port", lambda p: open_ports.remove(p)) |
374 | config["prometheus_output_port"] = "default" |
375 | - telegraf.configure_telegraf() |
376 | - expected = """ |
377 | -[[outputs.prometheus_client]] |
378 | + telegraf.configure_prometheus_client() |
379 | + expected = """[[outputs.prometheus_client]] |
380 | listen = ":9103" |
381 | -""" |
382 | - config_file = base_dir().join("telegraf.conf") |
383 | + ip_range = []""" |
384 | + config_file = base_dir().join("telegraf.d", "prometheus_client.conf") |
385 | assert expected in config_file.read() |
386 | |
387 | |
388 | @@ -777,15 +776,14 @@ outputs: |
389 | cpu: ["cpu0"] |
390 | |
391 | """ |
392 | - telegraf.configure_telegraf() |
393 | - expected = """ |
394 | -[[outputs.prometheus_client]] |
395 | + telegraf.configure_prometheus_client() |
396 | + expected = """[[outputs.prometheus_client]] |
397 | listen = ":9103" |
398 | + ip_range = [] |
399 | namedrop = ["aerospike*"] |
400 | [outputs.prometheus_client.tagpass] |
401 | - cpu = ["cpu0"] |
402 | -""" |
403 | - config_file = base_dir().join("telegraf.conf") |
404 | + cpu = ["cpu0"]""" |
405 | + config_file = base_dir().join("telegraf.d", "prometheus_client.conf") |
406 | assert expected in config_file.read() |
407 | |
408 | |
409 | @@ -1072,7 +1070,9 @@ def test_prometheus_client_output(mocker, monkeypatch, config): |
410 | monkeypatch.setattr(telegraf.hookenv, "open_port", lambda p: None) |
411 | |
412 | relation_ids = ["prometheus-client:0"] |
413 | + related_units = ["prometheus/0"] |
414 | monkeypatch.setattr(telegraf.hookenv, "relation_ids", lambda r: relation_ids) |
415 | + monkeypatch.setattr(telegraf.hookenv, "related_units", lambda r: related_units) |
416 | |
417 | network_get_primary_address = mocker.patch.object( |
418 | telegraf.hookenv, "network_get_primary_address", return_value="foo" |
419 | @@ -1082,28 +1082,48 @@ def test_prometheus_client_output(mocker, monkeypatch, config): |
420 | telegraf.hookenv, "network_get", return_value={"ingress_addresses": ["1.2.3.4"]} |
421 | ) |
422 | |
423 | + mocker.patch.object(telegraf.hookenv, "relation_get", return_value="5.6.7.8/32") |
424 | + |
425 | interface = mocker.Mock(spec=RelationBase) |
426 | interface.configure = mocker.Mock() |
427 | - telegraf.prometheus_client(interface) |
428 | + |
429 | + # Test that telegraf will allow scraping from related prometheus, and from ranges in |
430 | + # prometheus_ip_range |
431 | + config["prometheus_ip_range"] = "127.0.0.1/32,10.0.0.0/8" |
432 | + telegraf.configure_prometheus_client_with_relation(interface) |
433 | expected = """ |
434 | [[outputs.prometheus_client]] |
435 | - listen = ":9126" |
436 | + listen = ":9103" |
437 | + ip_range = ['127.0.0.1/32', '10.0.0.0/8', '5.6.7.8/32'] |
438 | """ |
439 | assert ( |
440 | - configs_dir().join("prometheus-client.conf").read().strip() == expected.strip() |
441 | + configs_dir().join("prometheus_client.conf").read().strip() == expected.strip() |
442 | ) |
443 | + # prometheus_ip_range is empty, test that all IPs are allowed, not just the related |
444 | + # prometheus |
445 | + # prometheus_output_port is also empty, we should get 9103 by default |
446 | network_get_primary_address.assert_called_once_with("prometheus-client") |
447 | interface.configure.assert_called_once_with( |
448 | - 9126, hostname="foo", private_address="foo" |
449 | + "9103", hostname="foo", private_address="foo" |
450 | + ) |
451 | + config["prometheus_ip_range"] = "" |
452 | + telegraf.configure_prometheus_client_with_relation(interface) |
453 | + expected = """ |
454 | + [[outputs.prometheus_client]] |
455 | + listen = ":9103" |
456 | + ip_range = [] |
457 | +""" |
458 | + assert ( |
459 | + configs_dir().join("prometheus_client.conf").read().strip() == expected.strip() |
460 | ) |
461 | |
462 | |
463 | def test_prometheus_client_output_departed(mocker, monkeypatch, config): |
464 | - configs_dir().join("prometheus-client.conf").write("empty") |
465 | + configs_dir().join("prometheus_client.conf").write("empty") |
466 | relations = [1] |
467 | monkeypatch.setattr(telegraf.hookenv, "relations_of_type", lambda n: relations) |
468 | telegraf.prometheus_client_departed() |
469 | - assert configs_dir().join("prometheus-client.conf").exists() |
470 | + assert configs_dir().join("prometheus_client.conf").exists() |
471 | relations.pop() |
472 | telegraf.prometheus_client_departed() |
473 | assert not configs_dir().join("prometheus-client.conf").exists() |
474 | @@ -1198,17 +1218,18 @@ def test_restart_on_output_plugin_relation_departed(mocker, monkeypatch, config) |
475 | mocker.patch.object( |
476 | telegraf.hookenv, "network_get", return_value={"ingress_addresses": ["1.2.3.4"]} |
477 | ) |
478 | - config["prometheus_output_port"] = "" |
479 | + config["prometheus_output_port"] = "9103" |
480 | bus.discover() |
481 | + set_flag("telegraf.installed") |
482 | set_flag("snap.telegraf.installed") |
483 | set_flag("telegraf.configured") |
484 | interface = mocker.Mock(spec=RelationBase) |
485 | interface.configure = mocker.Mock() |
486 | - telegraf.prometheus_client(interface) |
487 | - assert configs_dir().join("prometheus-client.conf").exists() |
488 | - # dispatch, file should be gone and telegraf restarted. |
489 | + telegraf.configure_prometheus_client_with_relation(interface) |
490 | + assert configs_dir().join("prometheus_client.conf").exists() |
491 | + # dispatch, file should still be present and telegraf restarted. |
492 | bus.dispatch() |
493 | - assert not configs_dir().join("prometheus-client.conf").exists() |
494 | + assert configs_dir().join("prometheus_client.conf").exists() |
495 | service_restart.assert_called_with(telegraf.DEB_SERVICE) |
496 | |
497 |
Quick first pass, see comment.