You can do this with custom email templating in grafana. The grafana uses 'go templating' for this.
To add custom template do this: go to alerting -> contact points -> add template -> give a template name (say email.body), write content of template in 'go templating' format (Read further to know what to write in content). Further you may also create another template for subject line of email, say email.subject. (https://grafana.com/docs/grafana/latest/alerting/manage-notifications/template-notifications/)
To use this templates in email, Contact points -> edit contact point -> Optional email settings -> In message field add {{template "email.body" .}}
and in subject field add {{template "email.subject" .}}
As content for email.subject
template, you can use following from example in grafana documentation:
{{ define "email.subject" }}
{{ if .Alerts.Firing -}}
{{ len .Alerts.Firing }} firing alert(s)
{{end}}
{{ if .Alerts.Resolved -}}
{{ len .Alerts.Resolved }} resolved alert(s)
{{end}}
{{ end }}
Coming to the question, there are two cases of timestamps:
1. If you want the time at which the alert was activated in grafana:
As an example add this to content of email.body template
THE TIME OF EVENT IS: {{.Alerts.StartsAt}}
2. If you want to add some time from the log string:
This functionality is very limited. So this is only a work around. We can extract some information from the {{.Alerts.ValueString}}
. The value in this variable depends on what query you configured in grafana alert. I had some logs as result in alert query (from elasticsearch data source). So I was getting the logs as simple strings in this variable. So I tried to extract time from it using slice
function of go templating.
As an example suppose the value of .Alerts.ValueString is "Count 2023-05-10 14:59:22,166 ERROR [main]...", then {{ slice .Alerts.ValueString 6 25}}
will output "2023-05-10 14:59:22".
Add following as content of email.body template:
THE TIME OF EVENT IN LOG IS: {{ slice .Alerts.ValueString 6 25}}
THE ValueString IS: {{.Alerts.ValueString}}
As per my current knowledge, it is only possible to use default available functions of go templating and using a custom function seems difficult or no documentation available. If using custom go functions in grafana template was possible, everything would have been easier, using regex.