-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Lookup parts in strings using regexp.MatchString #1452
Conversation
To maintain consistency with Prometheus, this would be better done with a |
For more complex templates it is sometimes required to check whether a string contains only specific parts. Therefore I added strings.Contains/strings.ContainsAny to the FuncMap Signed-off-by: Waldemar Biller <wbiller@gmail.com>
Signed-off-by: Waldemar Biller <wbiller@gmail.com>
Thanks! Can you update the docs over in the docs repo? |
Can I use regexp.MatchString to make it? I use alertmanager v0.12. But I need this function. What are the functions of go template supported in alertmanger? How to use them? @wbiller @brian-brazil |
@szediktam the list of functions available is in the documentation: https://prometheus.io/docs/alerting/notifications/#functions |
Only the functions in https://prometheus.io/docs/alerting/notifications/#functions and What shoule i dd if I want to use some other functions? Can I create a custom function ? |
yes
You have to build your own binary including this change or wait for the next (non patch) release. |
OK. Thank you. @simonpasquier |
Signed-off-by: Waldemar Biller <wbiller@gmail.com>
Signed-off-by: Waldemar Biller <wbiller@gmail.com> (cherry picked from commit 4e8a910)
cherry picked from commit 4e8a910 Signed-off-by: Waldemar Biller <wbiller@gmail.com> Signed-off-by: Sylvain Rabot <s.rabot@lectra.com>
cherry picked from commit 4e8a910 Signed-off-by: Waldemar Biller <wbiller@users.noreply.github.com> Signed-off-by: Sylvain Rabot <s.rabot@lectra.com>
Signed-off-by: Waldemar Biller <wbiller@gmail.com>
This commit ports template functions added to alertmanager in prometheus/alertmanager#1452 and prometheus/alertmanager#2101 and brings jiralert up to date (without safeHtml) with the alertmanger code in https://github.com/prometheus/alertmanager/blob/0c0c6bdb0133e399c1a9fe4c1a170ce49ec67b58/template/template.go#L127-L147 The `match` function allows for prometheus like substring matching for more complex templates. The `stringSlice` function renders a `string` into a `[]string` that is suitable for use with `Remove`. My original use case that led to this patch was this template: ```yaml receivers: - name: jira summary: '{{ if .CommonLabels.jira_summary }}{{ .CommonLabels.jira_summary }}{{ else }}{{ .GroupLabels.SortedPairs.Values | join " " }}{{ end }}' ``` I thought, 'wouldn't it be nice to remove the name of the jira project from the title of the issue?', so I tried this template: ```yaml receivers: - name: jira summary: '{{ if .CommonLabels.jira_summary }}{{ .CommonLabels.jira_summary }}{{ else }}{{ .GroupLabels.Remove "jira_project" }}{ .SortedPairs.Values | join " " }}{{ end }}' ``` This failed with: ``` err="template: :1:105: executing \"\" at <\"jira_project\">: can't handle \"jira_project\" for arg of type []string" receiver=jira groupLabels="unsupported value type" ``` After some research, I found prometheus/alertmanager#2101, with the description: > Since it's impossible to create a string slice in a Go template by > default, add a function to work around this problem. With this patch, we should be able to pass a string directly to the template Remove function, as alertmanager does.
This commit ports template functions added to alertmanager in prometheus/alertmanager#1452 and prometheus/alertmanager#2101 and brings jiralert up to date (without safeHtml) with the alertmanger code in https://github.com/prometheus/alertmanager/blob/0c0c6bdb0133e399c1a9fe4c1a170ce49ec67b58/template/template.go#L127-L147 The `match` function allows for prometheus like substring matching for more complex templates. The `stringSlice` function renders a `string` into a `[]string` that is suitable for use with `Remove`. My original use case that led to this patch was this template: ```yaml receivers: - name: jira summary: '{{ if .CommonLabels.jira_summary }}{{ .CommonLabels.jira_summary }}{{ else }}{{ .GroupLabels.SortedPairs.Values | join " " }}{{ end }}' ``` I thought, 'wouldn't it be nice to remove the name of the jira project from the title of the issue?', so I tried this template: ```yaml receivers: - name: jira summary: '{{ if .CommonLabels.jira_summary }}{{ .CommonLabels.jira_summary }}{{ else }}{{ .GroupLabels.Remove "jira_project" }}{ .SortedPairs.Values | join " " }}{{ end }}' ``` This failed with: ``` err="template: :1:105: executing \"\" at <\"jira_project\">: can't handle \"jira_project\" for arg of type []string" receiver=jira groupLabels="unsupported value type" ``` After some research, I found prometheus/alertmanager#2101, with the description: > Since it's impossible to create a string slice in a Go template by > default, add a function to work around this problem. With this patch, we should be able to pass a string directly to the template Remove function, as alertmanager does. Signed-off-by: Greg Burek <gburek@fastly.com>
This commit ports template functions added to alertmanager in prometheus/alertmanager#1452 and prometheus/alertmanager#2101 and brings jiralert up to date (without safeHtml) with the alertmanger code in https://github.com/prometheus/alertmanager/blob/0c0c6bdb0133e399c1a9fe4c1a170ce49ec67b58/template/template.go#L127-L147 The `match` function allows for prometheus like substring matching for more complex templates. The `stringSlice` function renders a `string` into a `[]string` that is suitable for use with `Remove`. My original use case that led to this patch was this template: ```yaml receivers: - name: jira summary: '{{ if .CommonLabels.jira_summary }}{{ .CommonLabels.jira_summary }}{{ else }}{{ .GroupLabels.SortedPairs.Values | join " " }}{{ end }}' ``` I thought, 'wouldn't it be nice to remove the name of the jira project from the title of the issue?', so I tried this template: ```yaml receivers: - name: jira summary: '{{ if .CommonLabels.jira_summary }}{{ .CommonLabels.jira_summary }}{{ else }}{{ .GroupLabels.Remove "jira_project" }}{ .SortedPairs.Values | join " " }}{{ end }}' ``` This failed with: ``` err="template: :1:105: executing \"\" at <\"jira_project\">: can't handle \"jira_project\" for arg of type []string" receiver=jira groupLabels="unsupported value type" ``` After some research, I found prometheus/alertmanager#2101, with the description: > Since it's impossible to create a string slice in a Go template by > default, add a function to work around this problem. With this patch, we should be able to pass a string directly to the template Remove function, as alertmanager does. Signed-off-by: Greg Burek <gburek@fastly.com> Co-authored-by: Alin Sinpalean <alin.sinpalean@gmail.com>
@stuartnelson3 For more complex templates it is sometimes required to check whether a string contains only specific parts. Therefore I added strings.Contains/strings.ContainsAny to the FuncMap