Loki timestamp number to date format?

I have a timestamp epoch number value in the log row.
So if I use the following query:

{app="$app"} | json | line_format `{{.ts}} [{{.name}}] {{.message}}` |= `$search`

I would get the result like this:

1693919208435 [log.file.name] log message payload

In this result, I’d like to format the epoch value to human readable date string.
I tried this:

{app="$app"} | json | line_format `{{.ts | date "2006-01-02 15:04:05"}} [{{.name}}] {{.message}}` |= `$search`

The result looks perfect,

2023-09-06 08:43:31 [log.file.name] log message payload

But the date/time does not come from the log timestamp field.
It is always current time regardless of the timestamp value.
So, consequently every rows have the same current datetime string.

How can I make the correct datetime string in accordance with the timestamp field value?

Try something like this:

`{{date "2006-01-02 15:04:05" (unixToTime .ts)}}`

There is an example here: Template functions | Grafana Loki documentation


I got error: function "unixToTime" not defined with Grafana v10.0.0 (81d85ce802)
it seems like there should exist such function according to the manual. but I don’t know why…

It was because Loki version 2.6, after I have upgraded to Loki version 2.9, unixToTime function worked.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.