Skip to content

Commit

Permalink
logcli: Refactor logcli Client interface to use time.Time and time.Du…
Browse files Browse the repository at this point in the history
…ration for LiveTailQueryConn method (#3271)
  • Loading branch information
chancez authored Feb 2, 2021
1 parent 4d1c6e0 commit 054b8fc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/logcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func main() {
}

if *tail {
rangeQuery.TailQuery(*delayFor, queryClient, out)
rangeQuery.TailQuery(time.Duration(*delayFor)*time.Second, queryClient, out)
} else {
rangeQuery.DoQuery(queryClient, out, *statistics)
}
Expand Down
18 changes: 10 additions & 8 deletions pkg/logcli/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Client interface {
ListLabelNames(quiet bool, start, end time.Time) (*loghttp.LabelResponse, error)
ListLabelValues(name string, quiet bool, start, end time.Time) (*loghttp.LabelResponse, error)
Series(matchers []string, start, end time.Time, quiet bool) (*loghttp.SeriesResponse, error)
LiveTailQueryConn(queryStr string, delayFor int, limit int, start int64, quiet bool) (*websocket.Conn, error)
LiveTailQueryConn(queryStr string, delayFor time.Duration, limit int, start time.Time, quiet bool) (*websocket.Conn, error)
GetOrgID() string
}

Expand Down Expand Up @@ -131,14 +131,16 @@ func (c *DefaultClient) Series(matchers []string, start, end time.Time, quiet bo
}

// LiveTailQueryConn uses /api/prom/tail to set up a websocket connection and returns it
func (c *DefaultClient) LiveTailQueryConn(queryStr string, delayFor int, limit int, start int64, quiet bool) (*websocket.Conn, error) {
qsb := util.NewQueryStringBuilder()
qsb.SetString("query", queryStr)
qsb.SetInt("delay_for", int64(delayFor))
qsb.SetInt("limit", int64(limit))
qsb.SetInt("start", start)
func (c *DefaultClient) LiveTailQueryConn(queryStr string, delayFor time.Duration, limit int, start time.Time, quiet bool) (*websocket.Conn, error) {
params := util.NewQueryStringBuilder()
params.SetString("query", queryStr)
if delayFor != 0 {
params.SetInt("delay_for", int64(delayFor.Seconds()))
}
params.SetInt("limit", int64(limit))
params.SetInt("start", start.UnixNano())

return c.wsConnect(tailPath, qsb.Encode(), quiet)
return c.wsConnect(tailPath, params.Encode(), quiet)
}

func (c *DefaultClient) GetOrgID() string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/logcli/query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ func (t *testQueryClient) Series(matchers []string, from, through time.Time, qui
panic("implement me")
}

func (t *testQueryClient) LiveTailQueryConn(queryStr string, delayFor int, limit int, from int64, quiet bool) (*websocket.Conn, error) {
func (t *testQueryClient) LiveTailQueryConn(queryStr string, delayFor time.Duration, limit int, start time.Time, quiet bool) (*websocket.Conn, error) {
panic("implement me")
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/logcli/query/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os/signal"
"strings"
"syscall"
"time"

"github.com/fatih/color"
"github.com/gorilla/websocket"
Expand All @@ -17,8 +18,8 @@ import (
)

// TailQuery connects to the Loki websocket endpoint and tails logs
func (q *Query) TailQuery(delayFor int, c client.Client, out output.LogOutput) {
conn, err := c.LiveTailQueryConn(q.QueryString, delayFor, q.Limit, q.Start.UnixNano(), q.Quiet)
func (q *Query) TailQuery(delayFor time.Duration, c client.Client, out output.LogOutput) {
conn, err := c.LiveTailQueryConn(q.QueryString, delayFor, q.Limit, q.Start, q.Quiet)
if err != nil {
log.Fatalf("Tailing logs failed: %+v", err)
}
Expand Down

0 comments on commit 054b8fc

Please sign in to comment.