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

Grafana Oncall templates new helper filter regex_replace() #226

Closed
njohnstone2 opened this issue Jul 13, 2022 · 5 comments
Closed

Grafana Oncall templates new helper filter regex_replace() #226

njohnstone2 opened this issue Jul 13, 2022 · 5 comments
Labels
feature request New feature or request

Comments

@njohnstone2
Copy link
Contributor

Grafana Oncall currently supports the default jinja2 filter functions with a handful of helper functions included in this repo. I would like to propose adding a new filter to support the use of regex to replace values within a template.

Grafana alerts provide a valueString field in the payload that is sent to grafana oncall. I would like to include this value in my alerting template however customizing this field is limited to the existing jinja2 filtering functions.

Example

Given the following valueString it would be nice to have the option to strip out the var field to display a more concise summary of what values are triggering the alert.

Payload

{
  "labels": {
    "env": "prod",
    "alertname": "Prod - My Alert"
  },
  "status": "firing",
  "valueString": "[ var='B0' metric='my_metric' labels={} value=175 ]"
}

Message Template

{{- payload.message }}
{%- if "status" in payload %}
**Status**: {{ payload.status }}
{% endif -%}
{%- if "valueString" in payload -%}
**Value**: {{ payload.valueString | regex_replace("var=\'[a-zA-Z0-9]+\'", "") }}
{% endif -%}

Desired Output

Prod - My Alert
Status: firing
Value: "[ metric='my_metric' labels={} value=175 ]"
@Matvey-Kuk Matvey-Kuk added the feature request New feature or request label Jul 14, 2022
@Matvey-Kuk
Copy link
Contributor

@njohnstone2 thank you for implementing this feature!

@njohnstone2
Copy link
Contributor Author

No worries and thanks for accepting the change!

@mimahesh
Copy link

Hi i am trying to get Metric and value output separately and did same for web template but for Microsoft team Template same rule are not able to apply . is there any specific reason for that

jinjatemplate

Metric = {{payload.valueString|regex_replace("[ var='.' metric='", "")|regex_replace("'.", "")}}
Value = {{payload.valueString|regex_replace("[ var='.' metric='.' labels={.*} value=", "")|regex_replace(" ]", "")}}

@Matvey-Kuk
Copy link
Contributor

@matiasb could you please check this one ☝️ ? Thank you!

@PhantomPhreak
Copy link

Here are my 2cents how to filter the valueString with added regex_replace() (which is just a re.sub() under the hood).

valueString may contain more than 1 metric, i want to get only metric name and it's value, represented as K:V, to make alert message shorter and easier to read.

Slack template:

{%- if "valueString" in payload -%}
*Metrics*:
{{ payload.valueString | regex_replace("\[( var='.+?')? metric='", "\t") | regex_replace("' labels={.+?} value=", ": ") | regex_replace("\]\,?", "\n") }}
{% endif -%}

Result:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants