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

Support querying labels on time range in logcli #2083

Merged
merged 1 commit into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions cmd/logcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ LogQL documentation:
https://github.com/grafana/loki/blob/master/docs/logql.md`)
instantQuery = newQuery(true, instantQueryCmd)

labelsCmd = app.Command("labels", "Find values for a given label.")
labelName = labelsCmd.Arg("label", "The name of the label.").HintAction(hintActionLabelNames).String()
labelsCmd = app.Command("labels", "Find values for a given label.")
labelsQuery = newLabelQuery(labelsCmd)

seriesCmd = app.Command("series", "Run series query.")
seriesQuery = newSeriesQuery(seriesCmd)
Expand Down Expand Up @@ -122,20 +122,12 @@ func main() {

instantQuery.DoQuery(queryClient, out, *statistics)
case labelsCmd.FullCommand():
q := newLabelQuery(*labelName, *quiet)

q.DoLabels(queryClient)
labelsQuery.DoLabels(queryClient)
case seriesCmd.FullCommand():
seriesQuery.DoSeries(queryClient)
}
}

func hintActionLabelNames() []string {
q := newLabelQuery("", *quiet)

return q.ListLabels(queryClient)
}

func newQueryClient(app *kingpin.Application) *client.Client {
client := &client.Client{
TLSConfig: config.TLSConfig{},
Expand Down Expand Up @@ -163,11 +155,31 @@ func newQueryClient(app *kingpin.Application) *client.Client {
return client
}

func newLabelQuery(labelName string, quiet bool) *labelquery.LabelQuery {
return &labelquery.LabelQuery{
LabelName: labelName,
Quiet: quiet,
}
func newLabelQuery(cmd *kingpin.CmdClause) *labelquery.LabelQuery {
var labelName, from, to string
var since time.Duration

q := &labelquery.LabelQuery{}

// executed after all command flags are parsed
cmd.Action(func(c *kingpin.ParseContext) error {

defaultEnd := time.Now()
defaultStart := defaultEnd.Add(-since)

q.Start = mustParse(from, defaultStart)
q.End = mustParse(to, defaultEnd)
q.LabelName = labelName
q.Quiet = *quiet
return nil
})

cmd.Arg("label", "The name of the label.").Default("").StringVar(&labelName)
cmd.Flag("since", "Lookback window.").Default("1h").DurationVar(&since)
cmd.Flag("from", "Start looking for labels at this absolute time (inclusive)").StringVar(&from)
cmd.Flag("to", "Stop looking for labels at this absolute time (exclusive)").StringVar(&to)

return q
}

func newSeriesQuery(cmd *kingpin.CmdClause) *seriesquery.SeriesQuery {
Expand Down
8 changes: 6 additions & 2 deletions docs/getting-started/logcli.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Args:
<query> eg '{foo="bar",baz=~".*blip"} |~ ".*error.*"'

$ logcli help labels
usage: logcli labels [<label>]
usage: logcli labels [<flags>] [<label>]

Find values for a given label.

Expand All @@ -188,7 +188,11 @@ Flags:
--tls-skip-verify Server certificate TLS skip verify.
--cert="" Path to the client certificate. Can also be set using LOKI_CLIENT_CERT_PATH env var.
--key="" Path to the client certificate key. Can also be set using LOKI_CLIENT_KEY_PATH env var.
--org-id=ORG-ID adds X-Scope-OrgID to API requests for representing tenant ID. Useful for requesting tenant data when bypassing an auth gateway.
--org-id=ORG-ID adds X-Scope-OrgID to API requests for representing tenant ID. Useful for requesting
tenant data when bypassing an auth gateway.
--since=1h Lookback window.
--from=FROM Start looking for labels at this absolute time (inclusive)
--to=TO Stop looking for labels at this absolute time (exclusive)

Args:
[<label>] The name of the label.
Expand Down
15 changes: 11 additions & 4 deletions pkg/logcli/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,26 @@ func (c *Client) QueryRange(queryStr string, limit int, from, through time.Time,
}

// ListLabelNames uses the /api/v1/label endpoint to list label names
func (c *Client) ListLabelNames(quiet bool) (*loghttp.LabelResponse, error) {
func (c *Client) ListLabelNames(quiet bool, from, through time.Time) (*loghttp.LabelResponse, error) {
var labelResponse loghttp.LabelResponse
if err := c.doRequest(labelsPath, "", quiet, &labelResponse); err != nil {
params := util.NewQueryStringBuilder()
params.SetInt("start", from.UnixNano())
params.SetInt("end", through.UnixNano())

if err := c.doRequest(labelsPath, params.Encode(), quiet, &labelResponse); err != nil {
return nil, err
}
return &labelResponse, nil
}

// ListLabelValues uses the /api/v1/label endpoint to list label values
func (c *Client) ListLabelValues(name string, quiet bool) (*loghttp.LabelResponse, error) {
func (c *Client) ListLabelValues(name string, quiet bool, from, through time.Time) (*loghttp.LabelResponse, error) {
path := fmt.Sprintf(labelValuesPath, url.PathEscape(name))
var labelResponse loghttp.LabelResponse
if err := c.doRequest(path, "", quiet, &labelResponse); err != nil {
params := util.NewQueryStringBuilder()
params.SetInt("start", from.UnixNano())
params.SetInt("end", through.UnixNano())
if err := c.doRequest(path, params.Encode(), quiet, &labelResponse); err != nil {
return nil, err
}
return &labelResponse, nil
Expand Down
7 changes: 5 additions & 2 deletions pkg/logcli/labelquery/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package labelquery
import (
"fmt"
"log"
"time"

"github.com/grafana/loki/pkg/logcli/client"
"github.com/grafana/loki/pkg/loghttp"
Expand All @@ -12,6 +13,8 @@ import (
type LabelQuery struct {
LabelName string
Quiet bool
Start time.Time
End time.Time
}

// DoLabels prints out label results
Expand All @@ -28,9 +31,9 @@ func (q *LabelQuery) ListLabels(c *client.Client) []string {
var labelResponse *loghttp.LabelResponse
var err error
if len(q.LabelName) > 0 {
labelResponse, err = c.ListLabelValues(q.LabelName, q.Quiet)
labelResponse, err = c.ListLabelValues(q.LabelName, q.Quiet, q.Start, q.End)
} else {
labelResponse, err = c.ListLabelNames(q.Quiet)
labelResponse, err = c.ListLabelNames(q.Quiet, q.Start, q.End)
}
if err != nil {
log.Fatalf("Error doing request: %+v", err)
Expand Down