Getting from..to datetime range in alert message template

Hello, I am trying to create a message template that on alert in Slack, will allow the user to click and see the dashboard focused on the relevant datetime range. However it seems like I do not have time.ParseDuration function available to create a Duration to add to the Time, I can only convert the template data’s StartsAt to a unixMilli

my current template

{{ if gt (len .DashboardURL ) 0 }}Go to dashboard: {{ .DashboardURL }}/engine-backend?from={{ print  .StartsAt.UnixMilli.Add (time.ParseDuration "-1h") }}&to={{ print  .StartsAt.UnixMilli }}{{ end }}

I’ve only started working with Go templates for this, have no previous experience with Go.
Does anyone have any idea how to do this?

Found a solution, just need time.window variable as described in Manage dashboard links | Grafana documentation

2 Likes

Hi there! :waving_hand:

Here’s an example from the docs that shows two ways to link an alert rule to a dashboard with a time range:

  1. Grafana 12 and later – Use the built-in DashboardURL and PanelURL annotations.
  2. Custom annotation – Build a time range manually in your template. For example:
  {{- $from := (.StartsAt.Add -3600000000000).UnixMilli }}

  {{- $to := "" }}
  {{- if eq .Status "resolved" }}
     {{- $to = (.EndsAt).UnixMilli }}
  {{- else -}}
    {{/* Use current time if alert is firing */}}
    {{- $to = (time.Now).UnixMilli }}
  {{- end -}}

  Dashboard: {{.Annotations.MyDashboardURL}}?from={{$from}}&to={{$to}}

This approach uses the alert’s start and end times to create a dynamic time range in the link. Hope this helps!

1 Like