How to create a link to grafana dashboard from alert to calculated time range?

I am generating a link to grafana dashboard from alerts.

Bottom line, I have to convert .StartsAt to from= and to= in grafana url.

I want to do the following:

{{- .Annotations.URL -}}&from={{- sub .StartsAt.Unix 3600 }}&to={{- .StartsAt.Unix -}}

Create a link to the dashboard url from 1 hour before the alert, so users can nicely navigate the dashboard in the proper time interval.

However, when provisioning the template with terraform, this fails with the message:

│ Error: [PUT /v1/provisioning/templates/{name}][400] putTemplateBadRequest {"message":"invalid object specification: invalid template: template: kc:84: function \"sub\" not defined"}

How am I should I create a link to the dashboard so it properly allows users to analyze the event using the data there were there for the alert to generate?

How to create a link to grafana dashboard from alert that shows data between 1 hour before the alert was generated?

Related: Convert time to epoch in alert notification , Usage of time and time.window or subtract timestamp in notification template , Documentation of Alert Templating in Serious Need of Improvement!

Thanks.

1 Like

Actually I DID IT! YAY!

So StartsAt is an object, and you can actually call an arbitrary object methos in go template. And it happens Time object has Add and Sub methods, and duration is in milliseconds.

{{- .Annotations.URL -}}&from={{- (.StartsAt.Sub -3600000000000).UnixMilli -}}&to={{{- (.StartsAt.Add 300000000000).UnixMilli -}}
2 Likes

@kamilcuk You’re a life saver :heart_eyes:

1 Like

Thanks the Add part is working, but for some reason I’m unable to “Sub” the time :confused:

@radek6 Same for me but i fixed it by adding negative number like this .StartsAt.Add -300000000000