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

Improve template to handle empty group labels #2794

Merged
merged 5 commits into from
Aug 17, 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
25 changes: 14 additions & 11 deletions engine/config_integrations/alertmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,27 @@

# Web
web_title = """\
{%- set groupLabels = payload.groupLabels.copy() -%}
{%- set alertname = groupLabels.pop('alertname') | default("") -%}
{%- set groupLabels = payload.get("groupLabels", {}).copy() -%}
{%- set alertname = groupLabels.pop('alertname', "") -%}


[{{ payload.status }}{% if payload.status == 'firing' %}:{{ payload.numFiring }}{% endif %}] {{ alertname }} {% if groupLabels | length > 0 %}({{ groupLabels|join(", ") }}){% endif %}
""" # noqa
[{{ payload.status }}{% if payload.status == 'firing' and payload.numFiring %}:{{ payload.numFiring }}{% endif %}] {{ alertname }} {% if groupLabels | length > 0 %}({{ groupLabels|join(", ") }}){% endif %}""" # noqa

web_message = """\
{%- set annotations = payload.commonAnnotations.copy() -%}
{%- set annotations = payload.get("commonAnnotations", {}).copy() -%}
{%- set groupLabels = payload.get("groupLabels", {}) -%}
{%- set commonLabels = payload.get("commonLabels", {}) -%}
{%- set severity = groupLabels.severity -%}

{% set severity = payload.groupLabels.severity -%}
{% if severity %}
{%- set severity_emoji = {"critical": ":rotating_light:", "warning": ":warning:" }[severity] | default(":question:") -%}
Severity: {{ severity }} {{ severity_emoji }}
{% endif %}

{%- set status = payload.status | default("Unknown") %}
{%- set status = payload.get("status", "Unknown") %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious, what is the difference here? I think previous version is more straightforward and clear for non-python developers

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just aligned it with other .getusages. ( It was used in previous templates also)

{%- set status_emoji = {"firing": ":fire:", "resolved": ":white_check_mark:"}[status] | default(":warning:") %}
Status: {{ status }} {{ status_emoji }} (on the source)
{% if status == "firing" %}
{% if status == "firing" and payload.numFiring %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add check for numResolved?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm checking the numFiring here to not to render empty ':', since alerts from migrated legacy alertmanager have no numFiring field. I'm still thinking how to do it better.

Firing alerts – {{ payload.numFiring }}
Resolved alerts – {{ payload.numResolved }}
{% endif %}
Expand All @@ -56,12 +57,14 @@
{%- set _ = annotations.pop('runbook_url_internal') -%}
{%- endif %}

{% if groupLabels | length > 0 -%}
GroupLabels:
{%- for k, v in payload["groupLabels"].items() %}
{%- for k, v in groupLabels.items() %}
- {{ k }}: {{ v }}
{%- endfor %}
{% endif %}

{% if payload["commonLabels"] | length > 0 -%}
{% if commonLabels | length > 0 -%}
CommonLabels:
{%- for k, v in payload["commonLabels"].items() %}
- {{ k }}: {{ v }}
Expand All @@ -82,7 +85,7 @@
# Slack
slack_title = """\
{%- set groupLabels = payload.groupLabels.copy() -%}
{%- set alertname = groupLabels.pop('alertname') | default("") -%}
{%- set alertname = groupLabels.pop('alertname', "") -%}
*<{{ grafana_oncall_link }}|#{{ grafana_oncall_incident_id }} {{ web_title }}>* via {{ integration_name }}
{% if source_link %}
(*<{{ source_link }}|source>*)
Expand Down
Loading