Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(loki): extended tailing #764

Merged
merged 9 commits into from
Jul 19, 2019
20 changes: 16 additions & 4 deletions cmd/logcli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ const (
queryPath = "/api/prom/query?query=%s&limit=%d&start=%d&end=%d&direction=%s&regexp=%s"
labelsPath = "/api/prom/label"
labelValuesPath = "/api/prom/label/%s/values"
tailPath = "/api/prom/tail?query=%s&regexp=%s&delay_for=%d"
tailPath = "/api/prom/tail?query=%s&regexp=%s&delay_for=%d&limit=%d&start=%d"
)

func query(from, through time.Time, direction logproto.Direction) (*logproto.QueryResponse, error) {
path := fmt.Sprintf(queryPath, url.QueryEscape(*queryStr), *limit, from.UnixNano(),
through.UnixNano(), direction.String(), url.QueryEscape(*regexpStr))
path := fmt.Sprintf(queryPath,
url.QueryEscape(*queryStr), // query
*limit, // limit
from.UnixNano(), // start
through.UnixNano(), // end
direction.String(), // direction
url.QueryEscape(*regexpStr), // regexp
)

var resp logproto.QueryResponse
if err := doRequest(path, &resp); err != nil {
Expand Down Expand Up @@ -105,7 +111,13 @@ func doRequest(path string, out interface{}) error {
}

func liveTailQueryConn() (*websocket.Conn, error) {
path := fmt.Sprintf(tailPath, url.QueryEscape(*queryStr), url.QueryEscape(*regexpStr), *delayFor)
path := fmt.Sprintf(tailPath,
url.QueryEscape(*queryStr), // query
url.QueryEscape(*regexpStr), // regexp
*delayFor, // delay_for
*limit, // limit
getStart(time.Now()).UnixNano(), // start
)
return wsConnect(path)
}

Expand Down
21 changes: 13 additions & 8 deletions cmd/logcli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ import (
"github.com/grafana/loki/pkg/logproto"
)

func getStart(end time.Time) time.Time {
start := end.Add(-*since)
if *from != "" {
var err error
start, err = time.Parse(time.RFC3339Nano, *from)
if err != nil {
log.Fatalf("error parsing date '%s': %s", *from, err)
}
}
return start
}

func doQuery() {
if *tail {
tailQuery()
Expand All @@ -24,14 +36,7 @@ func doQuery() {
)

end := time.Now()
start := end.Add(-*since)
if *from != "" {
var err error
start, err = time.Parse(time.RFC3339Nano, *from)
if err != nil {
log.Fatalf("error parsing --from date '%s': %s", *from, err)
}
}
start := getStart(end)

if *to != "" {
var err error
Expand Down
Loading