grafana/loki

[logcli] Allow --from and --to fields to be in any timezone and clarify documentation.

Open

#8,592 opened on Feb 22, 2023

View on GitHub
 (7 comments) (2 reactions) (1 assignee)Go (3,997 forks)batch import
help wantedtype/docs

Repository metrics

Stars
 (28,187 stars)
PR merge metrics
 (Avg merge 5d 20h) (522 merged PRs in 30d)

Description

Hi! Grafana-loki is amazing! Thank you!

Is your feature request related to a problem? Please describe. Yes, my feature request is related to a problem. I have no idea in what timezone should I give --from and --to values. Really, it's all converted to a UNIX timestamp for API call, I do not understand why the odd format with "auto" added time offset suffix. Documentation states https://grafana.com/docs/loki/latest/tools/logcli/ :

    Notice that when using --from and --to then ensure to use RFC3339Nano time
    format, but without timezone at the end. The local timezone will be added
    automatically or if using --timezone flag.

I do not understand the last sentence. Or if using --timezone flag, what happens? Using --timezone flag has no effect on interpreting --from and --to dates in my tests.

I am not able to specify time in current local time when using logcli. I am in New York, and it is time 13:10 on the clock right now. I type:

$ logcli --version
logcli, version 2.7.3 (branch: HEAD, revision: a880ea3bf)
  build user:       root@259cbf420615
  build date:       2023-02-01T10:29:15Z
  go version:       go1.19.5
  platform:         linux/amd64
$ ls -l /etc/localtime 
lrwxrwxrwx. 1 root root 38 Jan 18  2014 /etc/localtime -> ../usr/share/zoneinfo/America/New_York
$ date
Wed Feb 22 13:10:09 EST 2023
$ echo $TZ
        # empty, unset
$ logcli query --from=2023-02-22T12:57:36Z --to=2023-02-22T13:02:22Z '{namespace="distributor",job="dist_test.kc_postprod_blank.20220103.1000"}'
2023/02/22 14:28:37 http://...../loki/api/v1/query_range?direction=BACKWARD&end=1677070942000000000&limit=30&query=%7Bnamespace%3D%22distributor%22%2Cjob%3D%22dist_test.kc_postprod_blank.20220103.1000%22%7D&start=1677070656000000000
# no output

I have to pick a calculator and add 5 hours to every date.

$ logcli query --from=2023-02-22T17:57:36Z --to=2023-02-22T18:02:22Z '{namespace="distributor",job="dist_test.kc_postprod_blank.20220103.1000"}'
2023/02/22 14:32:43 http://.../loki/api/v1/query_range?direction=BACKWARD&end=1677088942000000000&limit=30&query=%7Bnamespace%3D%22distributor%22%2Cjob%3D%22dist_test.kc_postprod_blank.20220103.1000%22%7D&start=1677088656000000000
2023/02/22 14:32:43 Common labels: ...
2023-02-22T12:57:36-05:00 {} my logs, note that the timestamp on the left is correct

Or output UTC:

$ logcli query -z UTC --from=2023-02-22T17:57:36Z --to=2023-02-22T18:02:22Z 
2023-02-22T17:57:36Z {} my logs, note that the timestamp on the left is correct

Describe the solution you'd like I would to give the time in UNIX timestamp. For example, --from=@12345.67 would mean 12345.67 seconds since epoch.

I would also want to give the time in local timezone. For example, --from=2023-02-22T12:57:36 would mean 2023-02-22T12:57:36 EST, because I am in EST.

Describe alternatives you've considered The alternative is to wrap logcli in something to convert the dates.

$ mylogcli() {
      args=()
     while (($#)); do
         case "$1" in
            --from=*|--to=*)
               args+=("${1%=*}=$(date --utc -d@"$(date -d "${1#*=}" +%s)" +%Y-%m-%dT%H:%M:%SZ)")
              ;;
         *) args+=($1);; esac
        shift
     done
    (set -x ; logcli "${args[@]}")
}
$ mylogcli query --from=2023-02-22T12:57:36 --to=2023-02-22T13:02:22 '{namespace="distributor",job="dist_test.kc_postprod_blank.20220103.1000"}'
+ logcli query --from=2023-02-22T17:57:36Z --to=2023-02-22T18:02:22Z '{namespace="distributor",job="dist_test.kc_postprod_blank.20220103.1000"}'
2023/02/22 14:34:32 http://.../loki/api/v1/query_range?direction=BACKWARD&end=1677088942000000000&limit=30&query=%7Bnamespace%3D%22distributor%22%2Cjob%3D%22dist_test.kc_postprod_blank.20220103.1000%22%7D&start=1677088656000000000
# works

Contributor guide