Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs in web title and message templates rendering and visual representation #1747

Merged
merged 17 commits into from
May 2, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Rewrite alertmanager template
iskhakov committed Apr 14, 2023

Verified

This commit was signed with the committer’s verified signature.
rezkiy37 Michael (Mykhailo) Kravchenko
commit 93caf2dbe565c4e672b5149960b53ec0edb94827
115 changes: 64 additions & 51 deletions engine/config_integrations/alertmanager.py
Original file line number Diff line number Diff line change
@@ -20,60 +20,83 @@
<br>Creating contact points and routes for other alertmanagers...
{% endif %}"""

# Default templates
slack_title = """\
{# Usually title is located in payload.labels.alertname #}
{% set title = payload.get("labels", {}).get("alertname", "No title (check Web Title Template)") %}
{# Combine the title from different built-in variables into slack-formatted url #}
*<{{ grafana_oncall_link }}|#{{ grafana_oncall_incident_id }} {{ title }}>* via {{ integration_name }}
{% if source_link %}
(*<{{ source_link }}|source>*)
{%- endif %}
# Web
web_title = """{{- payload.labels.alertname }}
"""
web_message = """\
{%- set annotations = payload.annotations -%}
{%- set labels = payload.labels -%}

{%- set severity = payload.labels.severity | default("Unknown") -%}
{%- set severity_emoji = {"critical": "🚨", "warning": "⚠️" }[severity] | default("⁉️") -%}
**Severity**: {{ severity }}{{ severity_emoji }}

{% set status = payload.status | default("") -%}
{%- set status_emoji = {"firing": "🔥", "resolved": "✅"}[status] | default("⚠️") -%}
**Status**: {{ status }}{{ status_emoji }}

{%- if "message" in payload.annotations %}
{{ annotations.message }}
{% set _ = annotations.pop('message') %}
{% endif %}

{%- if "runbook_url" in payload.annotations %}
[📖 Runbook]({{ payload.annotations.runbook_url }})
{% set _ = annotations.pop('runbook_url') %}
{% endif %}

**🏷️ Labels:**
```
{% for k, v in payload["labels"].items() -%}
{{ k }}={{ v }}
{% endfor %}
```

slack_message = """\
{{- payload.message }}
{%- if "status" in payload -%}
*Status*: {{ payload.status }}
{% endif -%}
*Labels:* {% for k, v in payload["labels"].items() %}
{{ k }}: {{ v }}{% endfor %}
*Annotations:*
{%- for k, v in payload.get("annotations", {}).items() %}
{#- render annotation as slack markdown url if it starts with http #}
{{ k }}: {% if v.startswith("http") %} <{{v}}|here> {% else %} {{v}} {% endif -%}
{%- if annotations |length > 0%}
**Other annotations:**
{%- for k, v in payload["annotations"].items() %}
{#- render annotation as markdown url if it starts with http #}
*{{ k }}*: {% if v.startswith("http") %} [here]({{v}}){% else %} {{v}} {% endif -%}
{% endfor %}
{% endif %}
""" # noqa: W291

web_image_url = None

slack_image_url = None
# Behaviour
source_link = "{{ payload.generatorURL }}"

grouping_id = "{{ payload.labels }}"

web_title = """\
{# Usually title is located in payload.labels.alertname #}
{{- payload.get("labels", {}).get("alertname", "No title (check Web Title Template)") }}
resolve_condition = """\
{{ payload.status == "resolved" }}
"""

web_message = """\
{{- payload.message }}
{%- if "status" in payload %}
**Status**: {{ payload.status }}
{% endif -%}
**Labels:** {% for k, v in payload["labels"].items() %}
*{{ k }}*: {{ v }}{% endfor %}
**Annotations:**
{%- for k, v in payload.get("annotations", {}).items() %}
{#- render annotation as markdown url if it starts with http #}
*{{ k }}*: {% if v.startswith("http") %} [here]({{v}}){% else %} {{v}} {% endif -%}
{% endfor %}
acknowledge_condition = None

# Slack
slack_title = """\
*<{{ grafana_oncall_link }}|#{{ grafana_oncall_incident_id }} {{ web_title_template }}>* via {{ integration_name }}
{% if source_link %}
(*<{{ source_link }}|source>*)
{%- endif %}
"""

slack_message = """\
{{ web_message_template }}
""" # noqa: W291


web_image_url = slack_image_url
slack_image_url = web_image_url

sms_title = '{{ payload.get("labels", {}).get("alertname", "Title undefined") }}'
phone_call_title = sms_title
# SMS
sms_title = web_title

telegram_title = sms_title
# Phone
phone_call_title = web_title

# Telegram
telegram_title = web_title

telegram_message = """\
{{- payload.messsage }}
@@ -88,17 +111,7 @@
{{ k }}: {{ v }}
{% endfor %}""" # noqa: W291

telegram_image_url = slack_image_url

source_link = "{{ payload.generatorURL }}"

grouping_id = "{{ payload.labels }}"

resolve_condition = """\
{{ payload.get("status", "") == "resolved" }}
"""

acknowledge_condition = None
telegram_image_url = web_image_url

tests = {
"payload": {