Search code examples
grafana-loki

Loki API - start and end params


I want to get logs using loki API. But even after checking the official documentation, I still don't know how.

What I want is to specify start and end parameters in loki/api/v1/query_range, but I don't know what to put in those parameters.

How does this time 2023-08-14 10:09:16.035 become like this 1691975356035337780?

I tried like this:

https://Mydomain/loki/api/v1/query_range?query={app=test}&start=20230813&end=20230814

and also tried putting the millisecond value of the unix time, but it didn't work...


Solution

  • How does this time become 1691975356035337780?

    It's Unix timestamp in nanoseconds. Loki supports timestamps in a couple of formats: Unix timestamp in seconds, Unix timestamp in nanoseconds, RFC3339 and RFC3339Nano.

    Excerpt from official documentation on how they are interpreted:

    The API accepts several formats for timestamps. An integer with ten or fewer digits is interpreted as a Unix timestamp in seconds. More than ten digits are interpreted as a Unix timestamp in nanoseconds. A floating point number is a Unix timestamp with fractions of a second.

    The timestamps can also be written in RFC3339 and RFC3339Nano format, as supported by Go’s time package.

    Based on this your attempt with ...&start=20230813&end=20230814 was interpreted as request for data between Aug 23 1970 03:40:13 and Aug 23 1970 03:40:14.

    You should use either ...&start=2023-08-13T00:00:00Z00:00&end=2023-08-14T00:00:00Z00:00 or ...&start=1691884800&end=1691971200 (or their "nano" counterparts.

    I also tried putting the millisecond value of the unix time, but it didn't work...

    Milliseconds aren't supported. Only seconds or nanoseconds.


    For additional information on endpoints and their parameters, please reference this documentation: query, query_range