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

Span the annotation.message in alerts as YAML multiline strings. #412

Merged
merged 5 commits into from
Nov 2, 2021

Conversation

aallawala
Copy link
Contributor

@aallawala aallawala commented Oct 18, 2021

What this PR does:
This PR attempts to fix the issue where the annotation message is split across two lines instead of one. This causes an issue for our alertmanager config template to not render correctly since it splits up the printf template across the two lines.

Before:

groups:
- name: cortex_alerts
  rules:
  - alert: CortexIngesterUnhealthy
    annotations:
      message: Cortex cluster {{ $labels.cluster }}/{{ $labels.namespace }} has {{
        printf "%f" $value }} unhealthy ingester(s).
    expr: |
      min by (cluster, namespace) (cortex_ring_members{state="Unhealthy", name="ingester"}) > 0
    for: 15m
    labels:
      severity: critical

After

groups:
- name: cortex_alerts
  rules:
  - alert: CortexIngesterUnhealthy
    annotations:
      message: |
        Cortex cluster {{ $labels.cluster }}/{{ $labels.namespace }} has {{ printf "%f" $value }} unhealthy ingester(s).
    expr: |
      min by (cluster, namespace) (cortex_ring_members{state="Unhealthy", name="ingester"}) > 0
    for: 15m
    labels:
      severity: critical

Which issue(s) this PR fixes:
Fixes #

Checklist

  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

@aallawala aallawala requested a review from a team as a code owner October 18, 2021 22:23
@aallawala aallawala changed the title multiline annotation for CortexIngesterUnhealthy alert use multiline wrapper for the alert annotation.message templates Oct 18, 2021
@aallawala aallawala changed the title use multiline wrapper for the alert annotation.message templates Span the annotation.message in alerts as YAML multiline strings. Oct 18, 2021
Copy link
Collaborator

@pracucci pracucci left a comment

Choose a reason for hiding this comment

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

Changes LGTM, but why does this even fix your issue? Could you clarify, please?

@aallawala
Copy link
Contributor Author

Changes LGTM, but why does this even fix your issue? Could you clarify, please?

Thank you @pracucci for taking a look.

Spanning the template across multiple lines affects us since we utilize a separate Go templating layer on top of the Go templating layer expected by the alertmanager. Our templates in code without this change look something like this:

groups:
- name: cortex_alerts
  rules:
  - alert: CortexIngesterUnhealthy
    annotations:
      message: Cortex cluster {{ `{{ "{{ $labels.cluster }}" }}` }}/{{ `{{ "{{ $labels.namespace }}" }}` }} has {{ `{{ "{{
        printf \"%.*f\" $value }}" }}` }} unhealthy ingester(s).
    expr: |
      min by (cluster, namespace) (cortex_ring_members{state="Unhealthy", name="ingester"}) > 0
    for: 15m
    labels:
      severity: critical

The line delimit prior to the printf falls between a {{ }} that causes the Go templating to fail.

@pracucci
Copy link
Collaborator

I'm still not sure why this change outputs the message on a single line instead only cutting it. That was my question. Could you elaborate on it, please?

@aallawala
Copy link
Contributor Author

I'm still not sure why this change outputs the message on a single line instead only cutting it. That was my question. Could you elaborate on it, please?

From what I understand from the jsonnet spec, wrapping the message with a ||| in the jsonnet template preserves the line endings whereas the single-quoted ' and double-quoted " strings are allowed to span multiple lines in the output of the jsonnet.

@pracucci
Copy link
Collaborator

pracucci commented Nov 2, 2021

I'm still not sure why this change outputs the message on a single line instead only cutting it. That was my question. Could you elaborate on it, please?

From what I understand from the jsonnet spec, wrapping the message with a ||| in the jsonnet template preserves the line endings whereas the single-quoted ' and double-quoted " strings are allowed to span multiple lines in the output of the jsonnet.

I didn't know it, but now it makes perfect sense.

@pracucci pracucci merged commit 591e1f8 into grafana:main Nov 2, 2021
@aallawala aallawala deleted the aja_bugfix_alerts branch November 2, 2021 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants