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 series API in logcli #1861

Merged
merged 2 commits into from
Mar 31, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [1572](https://github.com/grafana/loki/pull/1572) **owen-d**: Introduces the `querier.query-ingesters-within` flag and associated yaml config. When enabled, queries for a time range that do not overlap this lookback interval will not be sent to the ingesters.
* [1558](https://github.com/grafana/loki/pull/1558) **owen-d**: Introduces `ingester.max-chunk-age` which specifies the maximum chunk age before it's cut.
* [1565](https://github.com/grafana/loki/pull/1565) **owen-d**: The query frontend's `split_queries_by_interval` can now be specified as an override
* [1861](https://github.com/grafana/loki/pull/1565) **yeya24**: Support series command in logcli

## 1.3.0 (2020-01-16)

Expand Down
70 changes: 51 additions & 19 deletions cmd/logcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (

"github.com/prometheus/common/config"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"

_ "github.com/grafana/loki/pkg/build"
"github.com/grafana/loki/pkg/logcli/client"
"github.com/grafana/loki/pkg/logcli/labelquery"
"github.com/grafana/loki/pkg/logcli/output"
"github.com/grafana/loki/pkg/logcli/query"

"gopkg.in/alecthomas/kingpin.v2"
"github.com/grafana/loki/pkg/logcli/seriesquery"
)

var (
Expand Down Expand Up @@ -72,6 +72,9 @@ https://github.com/grafana/loki/blob/master/docs/logql.md`)

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

seriesCmd = app.Command("series", "Run series query.")
seriesQuery = newSeriesQuery(seriesCmd)
)

func main() {
Expand Down Expand Up @@ -122,6 +125,8 @@ func main() {
q := newLabelQuery(*labelName, *quiet)

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

Expand Down Expand Up @@ -165,49 +170,76 @@ func newLabelQuery(labelName string, quiet bool) *labelquery.LabelQuery {
}
}

func newSeriesQuery(cmd *kingpin.CmdClause) *seriesquery.SeriesQuery {
// calculate series range from cli params
var from, to string
var since time.Duration

q := &seriesquery.SeriesQuery{}

// 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.Quiet = *quiet
return nil
})

cmd.Flag("since", "Lookback window.").Default("1h").DurationVar(&since)
cmd.Flag("from", "Start looking for logs at this absolute time (inclusive)").StringVar(&from)
cmd.Flag("to", "Stop looking for logs at this absolute time (exclusive)").StringVar(&to)
cmd.Flag("match", "eg '{foo=\"bar\",baz=~\".*blip\"}'").Required().StringsVar(&q.Matchers)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you show me an example of how it work with multiple matches ? what's the cli look like for them ? is it space based or comma seperated ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm asking cause I see that StringsVar is an array.

Copy link
Member

@owen-d owen-d Mar 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also like to know that, but otherwise LGTM!

Nice work @yeya24. Would you mind updating https://github.com/grafana/loki/blob/master/docs/getting-started/logcli.md?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
This works just the same like promtool query series

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. I won't hold up the PR if you don't want to add the docs, but I'd appreciate it :). Let me know.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add it this afternoon. Will ping you when I finish

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I have updated the docs. PTAL @owen-d


return q
}

func newQuery(instant bool, cmd *kingpin.CmdClause) *query.Query {
// calculcate query range from cli params
// calculate query range from cli params
var now, from, to string
var since time.Duration

query := &query.Query{}
q := &query.Query{}

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

if instant {
query.SetInstant(mustParse(now, time.Now()))
q.SetInstant(mustParse(now, time.Now()))
} else {
defaultEnd := time.Now()
defaultStart := defaultEnd.Add(-since)

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

cmd.Flag("limit", "Limit on number of entries to print.").Default("30").IntVar(&query.Limit)
cmd.Flag("limit", "Limit on number of entries to print.").Default("30").IntVar(&q.Limit)
if instant {
cmd.Arg("query", "eg 'rate({foo=\"bar\"} |~ \".*error.*\" [5m])'").Required().StringVar(&query.QueryString)
cmd.Arg("query", "eg 'rate({foo=\"bar\"} |~ \".*error.*\" [5m])'").Required().StringVar(&q.QueryString)
cmd.Flag("now", "Time at which to execute the instant query.").StringVar(&now)
} else {
cmd.Arg("query", "eg '{foo=\"bar\",baz=~\".*blip\"} |~ \".*error.*\"'").Required().StringVar(&query.QueryString)
cmd.Arg("query", "eg '{foo=\"bar\",baz=~\".*blip\"} |~ \".*error.*\"'").Required().StringVar(&q.QueryString)
cmd.Flag("since", "Lookback window.").Default("1h").DurationVar(&since)
cmd.Flag("from", "Start looking for logs at this absolute time (inclusive)").StringVar(&from)
cmd.Flag("to", "Stop looking for logs at this absolute time (exclusive)").StringVar(&to)
cmd.Flag("step", "Query resolution step width").DurationVar(&query.Step)
cmd.Flag("step", "Query resolution step width").DurationVar(&q.Step)
}

cmd.Flag("forward", "Scan forwards through logs.").Default("false").BoolVar(&query.Forward)
cmd.Flag("no-labels", "Do not print any labels").Default("false").BoolVar(&query.NoLabels)
cmd.Flag("exclude-label", "Exclude labels given the provided key during output.").StringsVar(&query.IgnoreLabelsKey)
cmd.Flag("include-label", "Include labels given the provided key during output.").StringsVar(&query.ShowLabelsKey)
cmd.Flag("labels-length", "Set a fixed padding to labels").Default("0").IntVar(&query.FixedLabelsLen)
cmd.Flag("store-config", "Execute the current query using a configured storage from a given Loki configuration file.").Default("").StringVar(&query.LocalConfig)
cmd.Flag("forward", "Scan forwards through logs.").Default("false").BoolVar(&q.Forward)
cmd.Flag("no-labels", "Do not print any labels").Default("false").BoolVar(&q.NoLabels)
cmd.Flag("exclude-label", "Exclude labels given the provided key during output.").StringsVar(&q.IgnoreLabelsKey)
cmd.Flag("include-label", "Include labels given the provided key during output.").StringsVar(&q.ShowLabelsKey)
cmd.Flag("labels-length", "Set a fixed padding to labels").Default("0").IntVar(&q.FixedLabelsLen)
cmd.Flag("store-config", "Execute the current query using a configured storage from a given Loki configuration file.").Default("").StringVar(&q.LocalConfig)

return query
return q
}

func mustParse(t string, defaultTime time.Time) time.Time {
Expand Down
Loading