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

Lookup parts in strings using regexp.MatchString #1452

Merged
merged 2 commits into from
Jul 3, 2018
Merged

Lookup parts in strings using regexp.MatchString #1452

merged 2 commits into from
Jul 3, 2018

Conversation

wbiller
Copy link
Contributor

@wbiller wbiller commented Jul 2, 2018

@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

@brian-brazil
Copy link
Contributor

To maintain consistency with Prometheus, this would be better done with a match function: https://prometheus.io/docs/prometheus/latest/configuration/template_reference/#strings

wbiller and others added 2 commits July 2, 2018 18:53
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>
@wbiller wbiller changed the title Lookup parts in strings using Contains/ContainsAny Lookup parts in strings using regexp.MatchString Jul 2, 2018
@brian-brazil brian-brazil merged commit 4e8a910 into prometheus:master Jul 3, 2018
@brian-brazil
Copy link
Contributor

Thanks! Can you update the docs over in the docs repo?

@szediktam
Copy link

Can I use regexp.MatchString to make it? I use alertmanager v0.12. But I need this function.
How can I make it.

What are the functions of go template supported in alertmanger? How to use them? @wbiller @brian-brazil
Thank you.

@simonpasquier
Copy link
Member

@szediktam the list of functions available is in the documentation: https://prometheus.io/docs/alerting/notifications/#functions
This change will only be available in the next version of AlertManager (not yet released).

@szediktam
Copy link

Only the functions in https://prometheus.io/docs/alerting/notifications/#functions and
http://golang.org/pkg/text/template/#hdr-Functions are available in alertmanager template?

What shoule i dd if I want to use some other functions? Can I create a custom function ?
I want to judge a string is in or not in another string.

@simonpasquier
Copy link
Member

@szediktam

Only the functions in https://prometheus.io/docs/alerting/notifications/#functions and
http://golang.org/pkg/text/template/#hdr-Functions are available in alertmanager template?

yes

What shoule i dd if I want to use some other functions? Can I create a custom function ?
I want to judge a string is in or not in another string.

You have to build your own binary including this change or wait for the next (non patch) release.

@szediktam
Copy link

OK. Thank you. @simonpasquier

gabreal pushed a commit to paritytech/alertmanager that referenced this pull request Jul 31, 2018
Signed-off-by: Waldemar Biller <wbiller@gmail.com>
sylr pushed a commit to sylr/alertmanager that referenced this pull request Dec 13, 2018
Signed-off-by: Waldemar Biller <wbiller@gmail.com>
(cherry picked from commit 4e8a910)
sylr pushed a commit to sylr/alertmanager that referenced this pull request Dec 13, 2018
cherry picked from commit 4e8a910

Signed-off-by: Waldemar Biller <wbiller@gmail.com>
Signed-off-by: Sylvain Rabot <s.rabot@lectra.com>
sylr pushed a commit to sylr/alertmanager that referenced this pull request Dec 14, 2018
cherry picked from commit 4e8a910

Signed-off-by: Waldemar Biller <wbiller@users.noreply.github.com>
Signed-off-by: Sylvain Rabot <s.rabot@lectra.com>
gabreal pushed a commit to paritytech/alertmanager that referenced this pull request Jan 28, 2019
Signed-off-by: Waldemar Biller <wbiller@gmail.com>
gburek-fastly added a commit to gburek-fastly/jiralert that referenced this pull request Mar 20, 2020
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.
gburek-fastly added a commit to gburek-fastly/jiralert that referenced this pull request Apr 10, 2020
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>
free added a commit to prometheus-community/jiralert that referenced this pull request May 14, 2020
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>
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.

4 participants