diff --git a/cmd/logcli/main.go b/cmd/logcli/main.go index 5f55b72908a8e..b5bf5381aa8c6 100644 --- a/cmd/logcli/main.go +++ b/cmd/logcli/main.go @@ -4,6 +4,7 @@ import ( "log" "net/url" "os" + "runtime/pprof" "time" "github.com/prometheus/common/config" @@ -20,11 +21,14 @@ import ( var ( app = kingpin.New("logcli", "A command-line for loki.").Version(version.Print("logcli")) - quiet = app.Flag("quiet", "suppress query metadata").Default("false").Short('q').Bool() - statistics = app.Flag("stats", "show query statistics").Default("false").Bool() - outputMode = app.Flag("output", "specify output mode [default, raw, jsonl]. raw suppresses log labels and timestamp.").Default("default").Short('o').Enum("default", "raw", "jsonl") + quiet = app.Flag("quiet", "Suppress query metadata").Default("false").Short('q').Bool() + statistics = app.Flag("stats", "Show query statistics").Default("false").Bool() + outputMode = app.Flag("output", "Specify output mode [default, raw, jsonl]. raw suppresses log labels and timestamp.").Default("default").Short('o').Enum("default", "raw", "jsonl") timezone = app.Flag("timezone", "Specify the timezone to use when formatting output timestamps [Local, UTC]").Default("Local").Short('z').Enum("Local", "UTC") + cpuProfile = app.Flag("cpuprofile", "Specify the location for writing a CPU profile.").Default("").String() + memProfile = app.Flag("memprofile", "Specify the location for writing a memory profile.").Default("").String() + queryClient = newQueryClient(app) queryCmd = app.Command("query", `Run a LogQL query. @@ -82,6 +86,31 @@ func main() { cmd := kingpin.MustParse(app.Parse(os.Args[1:])) + if cpuProfile != nil && *cpuProfile != "" { + cpuFile, err := os.Create(*cpuProfile) + if err != nil { + log.Fatal("could not create CPU profile: ", err) + } + defer cpuFile.Close() + if err := pprof.StartCPUProfile(cpuFile); err != nil { + log.Fatal("could not start CPU profile: ", err) + } + defer pprof.StopCPUProfile() + } + + if memProfile != nil && *memProfile != "" { + memFile, err := os.Create(*memProfile) + if err != nil { + log.Fatal("could not create memory profile: ", err) + } + defer memFile.Close() + defer func() { + if err := pprof.WriteHeapProfile(memFile); err != nil { + log.Fatal("could not write memory profile: ", err) + } + }() + } + switch cmd { case queryCmd.FullCommand(): location, err := time.LoadLocation(*timezone) diff --git a/docs/getting-started/logcli.md b/docs/getting-started/logcli.md index 51bf295b9b3eb..ccbdc8263a350 100644 --- a/docs/getting-started/logcli.md +++ b/docs/getting-started/logcli.md @@ -77,19 +77,22 @@ A command-line for loki. Flags: --help Show context-sensitive help (also try --help-long and --help-man). --version Show application version. - -q, --quiet suppress query metadata - --stats show query statistics - -o, --output=default specify output mode [default, raw, jsonl]. raw suppresses log labels and timestamp. + -q, --quiet Suppress query metadata + --stats Show query statistics + -o, --output=default Specify output mode [default, raw, jsonl]. raw suppresses log labels and timestamp. -z, --timezone=Local Specify the timezone to use when formatting output timestamps [Local, UTC] - --addr="http://localhost:3100" + --cpuprofile="" Specify the location for writing a CPU profile. + --memprofile="" Specify the location for writing a memory profile. + --addr="http://localhost:3100" Server address. Can also be set using LOKI_ADDR env var. --username="" Username for HTTP basic auth. Can also be set using LOKI_USERNAME env var. --password="" Password for HTTP basic auth. Can also be set using LOKI_PASSWORD env var. --ca-cert="" Path to the server Certificate Authority. Can also be set using LOKI_CA_CERT_PATH env var. - --tls-skip-verify Server certificate TLS skip verify. Can also be set using LOKI_TLS_SKIP_VERIFY env var. + --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. Can also be set using LOKI_ORG_ID env var. + --org-id="" adds X-Scope-OrgID to API requests for representing tenant ID. Useful for requesting tenant data when + bypassing an auth gateway. Commands: help [...] @@ -106,26 +109,29 @@ Commands: The output of the log can be specified with the "-o" flag, for example, "-o raw" for the raw output format. - The "query" command will output extra information about the query and its results, such as the API URL, set of common labels, and set of excluded labels. This - extra information can be suppressed with the --quiet flag. + The "query" command will output extra information about the query and its results, such as the API URL, set of common labels, + and set of excluded labels. This extra information can be suppressed with the --quiet flag. - While "query" does support metrics queries, its output contains multiple data points between the start and end query time. This output is used to build graphs, - like what is seen in the Grafana Explore graph view. If you are querying metrics and just want the most recent data point (like what is seen in the Grafana Explore - table view), then you should use the "instant-query" command instead. + While "query" does support metrics queries, its output contains multiple data points between the start and end query time. + This output is used to build graphs, like what is seen in the Grafana Explore graph view. If you are querying metrics and just + want the most recent data point (like what is seen in the Grafana Explore table view), then you should use the "instant-query" + command instead. instant-query [] Run an instant LogQL query. - The "instant-query" command is useful for evaluating a metric query for a single point in time. This is equivalent to the Grafana Explore table view; if you want a - metrics query that is used to build a Grafana graph, you should use the "query" command instead. + The "instant-query" command is useful for evaluating a metric query for a single point in time. This is equivalent to the + Grafana Explore table view; if you want a metrics query that is used to build a Grafana graph, you should use the "query" + command instead. - This command does not produce useful output when querying for log lines; you should always use the "query" command when you are running log queries. + This command does not produce useful output when querying for log lines; you should always use the "query" command when you + are running log queries. For more information about log queries and metric queries, refer to the LogQL documentation: https://github.com/grafana/loki/blob/master/docs/logql.md - labels [