Merge lp:~verterok/charms/trusty/telegraf/fix-haproxy-format into lp:~ubuntuone-hackers/charms/trusty/telegraf/trunk

Proposed by Guillermo Gonzalez
Status: Merged
Approved by: Guillermo Gonzalez
Approved revision: 19
Merged at revision: 19
Proposed branch: lp:~verterok/charms/trusty/telegraf/fix-haproxy-format
Merge into: lp:~ubuntuone-hackers/charms/trusty/telegraf/trunk
Prerequisite: lp:~verterok/charms/trusty/telegraf/fix-influxdb-template
Diff against target: 215 lines (+104/-26)
6 files modified
.bzrignore (+2/-0)
README.md (+9/-10)
config.yaml (+9/-10)
hooks/actions.py (+29/-6)
hooks/tests/__init__.py (+6/-0)
hooks/tests/test_actions.py (+49/-0)
To merge this branch: bzr merge lp:~verterok/charms/trusty/telegraf/fix-haproxy-format
Reviewer Review Type Date Requested Status
Wes Mason (community) Approve
Review via email: mp+290739@code.launchpad.net

Commit message

Fix config rendering: extra_options is now valid YAML (without force string values) and add unittests

Description of the change

- fix config rendering: extra_options is now valid YAML (without force string values)
- added unittests for these changes

To post a comment you must log in.
Revision history for this message
Wes Mason (wesmason) wrote :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file '.bzrignore'
2--- .bzrignore 1970-01-01 00:00:00 +0000
3+++ .bzrignore 2016-04-01 14:32:37 +0000
4@@ -0,0 +1,2 @@
5+__pycache__
6+.cache
7
8=== modified file 'README.md'
9--- README.md 2016-03-30 20:52:57 +0000
10+++ README.md 2016-04-01 14:32:37 +0000
11@@ -28,25 +28,24 @@
12
13 inputs:
14 cpu:
15- percpu: 'false'
16- fielddrop: '["time_*"]'
17+ percpu: false
18+ fielddrop: ["time_*"]
19 disk:
20- mount_points: '["/"]'
21- ignore_fs: '["tmpfs", "devtmpfs"]'
22+ mount_points: ["/"]
23+ ignore_fs: ["tmpfs", "devtmpfs"]
24 elasticsearch:
25- local: 'false'
26- cluster_health: 'true'
27+ local: false
28+ cluster_health: true
29 postgresql:
30- databases: '["foo", "bar"]'
31+ databases: ["foo", "bar"]
32 tagpass:
33- db: '["template", "postgres"]'
34+ db: ["template", "postgres"]
35 outputs:
36 influxdb:
37- precision: "ms"
38+ precision: ms
39
40 This extra options will only be applied to plugins defined in templates/base_inputs.conf and any other plugins configured via relations.
41
42-
43 To use a different metrics storage, e.g: graphite. the plugin configuration needs to be set as a base64 string in outputs_config configuration.
44
45 For exmaple, save the following config to a file:
46
47=== modified file 'config.yaml'
48--- config.yaml 2016-03-30 17:57:57 +0000
49+++ config.yaml 2016-04-01 14:32:37 +0000
50@@ -86,23 +86,22 @@
51 type: string
52 description: |
53 YAML with extra options for out|inputs managed by relations or in the default config.
54- Values must be strings.
55 example:
56 inputs:
57 cpu:
58- percpu: 'false'
59- fielddrop: '["time_*"]'
60+ percpu: false
61+ fielddrop: ["time_*"]
62 disk:
63- mount_points: '["/"]'
64- ignore_fs: '["tmpfs", "devtmpfs"]'
65+ mount_points: ["/"]
66+ ignore_fs: ["tmpfs", "devtmpfs"]
67 elasticsearch:
68- local: 'false'
69- cluster_health: 'true'
70+ local: false
71+ cluster_health: true
72 postgresql:
73- databases: '["foo", "bar"]'
74+ databases: ["foo", "bar"]
75 tagpass:
76- db: '["template", "postgres"]'
77+ db: ["template", "postgres"]
78 outputs:
79 influxdb:
80- precision: "ms"
81+ precision: ms
82
83
84=== modified file 'hooks/actions.py'
85--- hooks/actions.py 2016-04-01 14:32:37 +0000
86+++ hooks/actions.py 2016-04-01 14:32:37 +0000
87@@ -93,7 +93,6 @@
88
89 def render_config(service_name):
90 config = hookenv.config()
91- extra_options = get_extra_options()
92 context = config.copy()
93 inputs = base64.b64decode(config['inputs_config'])
94 outputs = base64.b64decode(config['outputs_config'])
95@@ -107,10 +106,7 @@
96 context["inputs"] = inputs
97 else:
98 # use base inputs from charm templates
99- with open('templates/base_inputs.conf', 'r') as fd:
100- context["inputs"] = render_template(
101- fd.read(),
102- {'extra_options': extra_options['inputs']})
103+ context["inputs"] = render_base_inputs()
104 if outputs:
105 context["outputs"] = outputs
106 else:
107@@ -131,6 +127,15 @@
108 render(source='telegraf.conf.tmpl', target=CONFIG_FILE, context=context)
109
110
111+def render_base_inputs():
112+ extra_options = get_extra_options()
113+ # use base inputs from charm templates
114+ with open(os.path.join(hookenv.charm_dir(), 'templates/base_inputs.conf'), 'r') as fd:
115+ return render_template(
116+ fd.read(),
117+ {'extra_options': extra_options['inputs']})
118+
119+
120 def get_remote_unit_name():
121 types = hookenv.relation_types()
122 for rel_type in types:
123@@ -144,7 +149,25 @@
124 extra_options_raw = hookenv.config()['extra_options']
125 extra_opts = yaml.load(extra_options_raw) or {}
126 extra_options.update(extra_opts)
127- return extra_options
128+ # jsonify value, required as the telegraf config format is similar to raw
129+ # json
130+ json_vals = {}
131+ # kind level
132+ for k, v in extra_options.items():
133+ json_vals[k] = {}
134+ # plugins level
135+ for plugin, values in v.items():
136+ json_vals[k][plugin] = {}
137+ # inner plugin (aka key:value)
138+ for key, val in values.items():
139+ if key in ('tagpass', 'tagdrop'):
140+ # this is a tagpass/drop, we need to go deeper
141+ json_vals[k][plugin][key] = {}
142+ for tag, tagvalue in val.items():
143+ json_vals[k][plugin][key][tag] = json.dumps(tagvalue)
144+ else:
145+ json_vals[k][plugin][key] = json.dumps(val)
146+ return json_vals
147
148
149 def render_extra_options(kind, name):
150
151=== added directory 'hooks/tests'
152=== added file 'hooks/tests/__init__.py'
153--- hooks/tests/__init__.py 1970-01-01 00:00:00 +0000
154+++ hooks/tests/__init__.py 2016-04-01 14:32:37 +0000
155@@ -0,0 +1,6 @@
156+import sys
157+
158+from pkg_resources import resource_filename
159+
160+# allow importing actions from the hooks directory
161+sys.path.append(resource_filename(__name__, '../../hooks'))
162
163=== added file 'hooks/tests/test_actions.py'
164--- hooks/tests/test_actions.py 1970-01-01 00:00:00 +0000
165+++ hooks/tests/test_actions.py 2016-04-01 14:32:37 +0000
166@@ -0,0 +1,49 @@
167+"""actions.py tests"""
168+import os
169+
170+import pytest
171+
172+import actions
173+
174+
175+def test_render_extra_options(monkeypatch):
176+ extra_options = """
177+ inputs:
178+ test:
179+ boolean: true
180+ string: 10s
181+ list: ["a", "b"]
182+"""
183+ config = {'extra_options': extra_options}
184+ monkeypatch.setattr(actions.hookenv, 'config', lambda: config)
185+ content = actions.render_extra_options('inputs', 'test')
186+ expected = """ boolean = true\n list = ["a", "b"]\n string = "10s"\n"""
187+ assert sorted(content.split()) == sorted(expected.split())
188+
189+
190+def test_render_base_inputs(monkeypatch):
191+ base_inputs_opts = """
192+inputs:
193+ cpu:
194+ foo: 10s
195+ percpu: false
196+ fielddrop: ["time_*"]
197+ tagpass:
198+ cpu: ["cpu0"]
199+"""
200+ config = {'extra_options': base_inputs_opts}
201+ monkeypatch.setattr(actions.hookenv, 'config', lambda: config)
202+ charm_dir = os.path.join(os.path.dirname(actions.__file__), "../")
203+ monkeypatch.setattr(actions.hookenv, 'charm_dir', lambda: charm_dir)
204+ content = actions.render_base_inputs()
205+ expected = """
206+# Read metrics about cpu usage
207+[[inputs.cpu]]
208+ fielddrop = ["time_*"]
209+ foo = "10s"
210+ [inputs.cpu.tagpass]
211+ cpu = ["cpu0"]
212+ percpu = false
213+"""
214+ assert content[:len(expected)] == expected
215+

Subscribers

People subscribed via source and target branches