Skip to content

Commit

Permalink
Refactor rulemacro for more common case
Browse files Browse the repository at this point in the history
  • Loading branch information
kfdm committed Aug 15, 2019
1 parent 8e3fea4 commit 7aa968c
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion promgen/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def render_rules(rules=None):
for r in rules:
rule_list[str(r.content_object)].append({
'alert': r.name,
'expr': macro.rulemacro(r.clause, r),
'expr': macro.rulemacro(r),
'for': r.duration,
'labels': r.labels,
'annotations': r.annotations,
Expand Down
2 changes: 1 addition & 1 deletion promgen/templates/promgen/rule_update.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ <h1>Rule: {{ rule.name }}</h1>
<td class="col-xs-2"><a href="{% url 'rule-edit' r.pk %}">{{ r.name }}</a></td>
<td class="col-xs-8">
<code data-href="{% url 'rule-test' r.id %}" data-source="self" data-target="#ajax-clause-check">
{{ r.clause|rulemacro:r }}
{{ r|rulemacro }}
</code>
</td>
</tr>
Expand Down
9 changes: 7 additions & 2 deletions promgen/templatetags/promgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ def to_prom(value):


@register.filter()
def rulemacro(value, rule):
def rulemacro(rule, clause=None):
'''
Macro rule expansion
Assuming a list of rules with children and parents, expand our macro to exclude child rules
Can optionally pass expression to render in the context of the passed rule
.. code-block:: none
foo{<exclude>} / bar{<exclude>} > 5 # Parent Rule
Expand All @@ -53,6 +55,9 @@ def rulemacro(value, rule):
foo{project="B"} / bar{project="B"} > 4
'''

if not clause:
clause = rule.clause

labels = collections.defaultdict(list)
for r in rule.overrides.all():
labels[r.content_type.model].append(r.content_object.name)
Expand All @@ -63,7 +68,7 @@ def rulemacro(value, rule):
macro = ','.join(
sorted('{}!~"{}"'.format(k, v) for k, v in filters.items())
)
return value.replace(EXCLUSION_MACRO, macro)
return clause.replace(EXCLUSION_MACRO, macro)


@register.simple_tag
Expand Down
2 changes: 1 addition & 1 deletion promgen/tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_macro(self, mock_post):
rules['project']['model'] = models.Rule.objects.get(pk=project_rule.pk)

for k, r in rules.items():
self.assertEquals(macro.rulemacro(r['model'].clause, r['model']), r['assert'], 'Expansion wrong for %s' % k)
self.assertEquals(macro.rulemacro(r['model']), r['assert'], 'Expansion wrong for %s' % k)

@override_settings(PROMGEN=TEST_SETTINGS)
@mock.patch('django.dispatch.dispatcher.Signal.send')
Expand Down
2 changes: 1 addition & 1 deletion promgen/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ def post(self, request, pk):
else:
rule = get_object_or_404(models.Rule, id=pk)

query = macro.rulemacro(request.POST['query'], rule)
query = macro.rulemacro(rule, request.POST['query'])
# Since our rules affect all servers we use Promgen's proxy-query to test our rule
# against all the servers at once
url = resolve_domain('proxy-query')
Expand Down

0 comments on commit 7aa968c

Please sign in to comment.