How to remove alert labels starting/containing substring

I’m looking for a way to remove all labels starting with ‘_’ in alert template.

chatgpt gave me this:

{{- define "filterLabels" -}}
  {{- $labels := . -}}
  {{- $filteredLabels := dict -}}
  {{- range $key, $value := $labels -}}
    {{- if not (hasPrefix $key "_") -}}
      {{- $filteredLabels = merge $filteredLabels (dict $key $value) -}}
    {{- end -}}
  {{- end -}}
  {{- $filteredLabels -}}
{{- end -}}

but it doesn’t work because there is no hasPrefix function.

Any ideas?

You can use the match function in both types of templates:

Here’s a simple notification template filtering by label name:

{{ define "custom_example" -}}
{{ range .Alerts -}}
  {{ range .Labels.SortedPairs -}}
    {{ if match "alert*" .Name -}}
      - {{ .Name }} = {{ .Value }}
    {{ end -}}
  {{ end -}}
{{ end -}}
{{ end -}}

Note notification templates already filters labels starting with _ .