Merge ~sajoupa/charm-telegraf:tags_avoid_conflicts into charm-telegraf:master

Proposed by Laurent Sesquès
Status: Merged
Approved by: Laurent Sesquès
Approved revision: ad837868cf05f58faf504244da36fe2f43480aba
Merged at revision: 4d2c141f6be61f11f16936468d68e8f8bb1e7703
Proposed branch: ~sajoupa/charm-telegraf:tags_avoid_conflicts
Merge into: charm-telegraf:master
Diff against target: 72 lines (+19/-11)
2 files modified
src/reactive/telegraf.py (+16/-9)
src/tests/unit/test_telegraf.py (+3/-2)
Reviewer Review Type Date Requested Status
Benjamin Allot Approve
Canonical IS Reviewers Pending
BootStack Reviewers Pending
Review via email: mp+399721@code.launchpad.net

Commit message

tags: avoid conflicts if we set juju_*, with priority to the custom set ones

To post a comment you must log in.
Revision history for this message
Benjamin Allot (ballot) wrote :

LGTM (small not blocking comment inline)

review: Approve
Revision history for this message
Laurent Sesquès (sajoupa) wrote :

Addressed Ben's comment (using an OrderedDict to get an identical output at each run).
Passed `make test`.

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 :

Unable to determine commit message from repository - please click "Set commit message" and enter the commit message manually.

Revision history for this message
Benjamin Allot (ballot) wrote :

Ordereddict works to keep a consistent output but doesn't sort the tag in the configuration file.

I think sorting the tag increase the readability anyway but it's not a blocker indeed.

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Failed to merge change (unable to merge source repository due to conflicts), setting status to needs review.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 4d2c141f6be61f11f16936468d68e8f8bb1e7703

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/reactive/telegraf.py b/src/reactive/telegraf.py
2index 4c06424..99af5d8 100644
3--- a/src/reactive/telegraf.py
4+++ b/src/reactive/telegraf.py
5@@ -16,6 +16,7 @@
6
7 import base64
8 import binascii
9+import collections
10 import hashlib
11 import io
12 import ipaddress
13@@ -564,19 +565,25 @@ def configure_telegraf(): # noqa: C901
14 except binascii.Error:
15 # not bas64, probably already up to date configs
16 pass
17- tags = []
18
19- if config["tags"]:
20- for tag in config["tags"].split(","):
21- key, value = tag.split("=")
22- tags.append('{} = "{}"'.format(key, value))
23- # add extra juju-related tags to be exposed as labels
24- tags.append('juju_application = "{}"'.format(get_remote_unit_name().split("/")[0]))
25- tags.append('juju_unit = "{}"'.format(get_remote_unit_name().replace("/", "-")))
26+ tags = []
27+ # Initialize juju-related tags to be exposed as labels
28+ # Using a dict to avoid duplicates, which telegraf will see as conflicts
29+ tags_dict = collections.OrderedDict()
30+ tags_dict["juju_application"] = get_remote_unit_name().split("/")[0]
31+ tags_dict["juju_unit"] = get_remote_unit_name().replace("/", "-")
32 try:
33- tags.append('juju_model = "{}"'.format(hookenv.model_name()))
34+ tags_dict["juju_model"] = hookenv.model_name()
35 except KeyError:
36 pass # support older Juju 1.x deploys
37+ # Parse juju-configured tags, which can override the 3 juju_* defined above
38+ if config["tags"]:
39+ for tag in config["tags"].split(","):
40+ key, value = tag.split("=")
41+ tags_dict[key] = value
42+ # Render the tags dict into an array
43+ for key, value in tags_dict.items():
44+ tags.append('{} = "{}"'.format(key, value))
45 context["tags"] = tags
46
47 if inputs:
48diff --git a/src/tests/unit/test_telegraf.py b/src/tests/unit/test_telegraf.py
49index 1f9372f..128ede2 100644
50--- a/src/tests/unit/test_telegraf.py
51+++ b/src/tests/unit/test_telegraf.py
52@@ -861,17 +861,18 @@ outputs:
53 def test_default_tags_juju2(monkeypatch, config):
54 monkeypatch.setattr(telegraf.hookenv, "principal_unit", lambda: "principal-unit")
55 monkeypatch.setattr(telegraf.hookenv, "open_port", lambda p: None)
56+ config["tags"] = "juju_model=custom-model-name,juju_application=custom-app-name"
57 telegraf.configure_telegraf()
58 expected = """
59 [tags]
60 # dc = "us-east-1" # will tag all metrics with dc=us-east-1
61 # rack = "1a"
62
63- juju_application = "remote-unit"
64+ juju_application = "custom-app-name"
65
66 juju_unit = "remote-unit-0"
67
68- juju_model = "telegraf-test-model"
69+ juju_model = "custom-model-name"
70
71 """
72 config_file = base_dir().join("telegraf.conf")

Subscribers

People subscribed via source and target branches

to all changes: