Hi All,
Well I’ve been using Grafana for quite a bit now and i’m running into a problem while templating notifications. I’m currently using: Grafana v11.6.0
I’m attempting to use the Namespace functions to create a json based dict as is shown in the following example from the docs page:
{{ define "data.example" }}
{{- /* First, let's create some alert data as a JSON string */ -}}
{{ $jsonString := `{
"service": {
"name": "payment-api",
"environment": "production",
"thresholds": {
"error_rate": 5,
"latency_ms": 100
}
}
}` }}
{{- /* Parse the JSON string into an object we can work with */ -}}
{{ $config := $jsonString | data.JSON }}
{{- /* Create a new alert payload */ -}}
{{ $payload := coll.Dict
"service" $config.service.name
"environment" $config.service.environment
"status" .Status
"errorThreshold" $config.service.thresholds.error_rate
}}
{{- /* Output the payload in different JSON formats */ -}}
Compact JSON: {{ $payload | data.ToJSON }}
Pretty JSON with 2-space indent:
{{ $payload | data.ToJSONPretty " " }}
{{ end }}
I’m doing so in the: “New notification template group” - Under the Contact points → notification templates tab → new notification template button. For the record: I am litterally copying the code above into the editor field. And checking whether the outocme is as expected in the preview window.
So, where am I at:
- I’m going at this step by step so first I start with the empty template:
{{ define "data.example" }} {{ end }}
In with which the parser / preview is fine.
- Second step is to add the first variable to see if this already gives an excpetion ( it doesn’t)
{{ define "data.example" }}
{{ $jsonString := `{
"service": {
"name": "payment-api",
"environment": "production",
"thresholds": {
"error_rate": 5,
"latency_ms": 100
}
}
}` }}
{{ end }}
- Adding a print statement to see If my variable is as expected in the preview:
{{ define "data.example" }}
{{ $jsonString := `{
"service": {
"name": "payment-api",
"environment": "production",
"thresholds": {
"error_rate": 5,
"latency_ms": 100
}
}
}` }}
{{print $jsonString}}
{{ end }}
- And now comes the issue (!) (It always take me a while to get to the point..) Whe I add the rule:
{{ $config := $jsonString | data.JSON }}
The preview windows shows the Error:
invalid_template
template: :13: function "data" not defined
The actual code I have in the editor box at this point is as follows:
{{ define "data.example" }}
{{ $jsonString := `{
"service": {
"name": "payment-api",
"environment": "production",
"thresholds": {
"error_rate": 5,
"latency_ms": 100
}
}
}` }}
{{print $jsonString}}
{{ $config := $jsonString | data.JSON }}
{{ end }}
So, the first two steps: A-OK, as soon as I attempt to pipe into the “namespaced function” data.JSON the preview fails and the template is invalid due to it not being able to find the the called function.
So i guess the question becomes: How can I access the namespaced function “data.JSON” ?
Sorry for the long read, Thanks in advance for the replies!
Cheers,
Jelger