Skip to content

template: add the default template function stringSlice#2101

Merged
brian-brazil merged 1 commit into
prometheus:masterfrom
vrischmann:template-string-slice
Nov 13, 2019
Merged

template: add the default template function stringSlice#2101
brian-brazil merged 1 commit into
prometheus:masterfrom
vrischmann:template-string-slice

Conversation

@vrischmann
Copy link
Copy Markdown
Contributor

Since it's impossible to create a string slice in a Go template by
default, add a function to work around this problem.

The use case is to make it easy to call KV.Remove with an arbitrary
slice inside a template.

This is a follow up to #2094

Since it's impossible to create a string slice in a Go template by
default, add a function to work around this problem.

The use case is to make it easy to call KV.Remove with an arbitrary
slice inside a template.

Signed-off-by: Vincent Rischmann <vincent@rischmann.fr>
@brian-brazil brian-brazil merged commit 66a0ed2 into prometheus:master Nov 13, 2019
@brian-brazil
Copy link
Copy Markdown
Contributor

Thanks!

The docs over in the docs repo will also need updating.

@vrischmann vrischmann deleted the template-string-slice branch November 13, 2019 11:24
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants