diff --git a/app/vmagent/main.go b/app/vmagent/main.go index df0fc9ee2..973e468c4 100644 --- a/app/vmagent/main.go +++ b/app/vmagent/main.go @@ -23,6 +23,7 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo" "github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup" "github.com/VictoriaMetrics/VictoriaMetrics/lib/envflag" + "github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil" "github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver" graphiteserver "github.com/VictoriaMetrics/VictoriaMetrics/lib/ingestserver/graphite" influxserver "github.com/VictoriaMetrics/VictoriaMetrics/lib/ingestserver/influx" @@ -275,8 +276,5 @@ vmagent collects metrics data via popular data ingestion protocols and routes it See the docs at https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/app/vmagent/README.md . ` - - f := flag.CommandLine.Output() - fmt.Fprintf(f, "%s\n", s) - flag.PrintDefaults() + flagutil.Usage(s) } diff --git a/app/vmalert/main.go b/app/vmalert/main.go index 40d247827..a46715206 100644 --- a/app/vmalert/main.go +++ b/app/vmalert/main.go @@ -219,8 +219,5 @@ vmalert processes alerts and recording rules. See the docs at https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/app/vmalert/README.md . ` - - f := flag.CommandLine.Output() - fmt.Fprintf(f, "%s\n", s) - flag.PrintDefaults() + flagutil.Usage(s) } diff --git a/app/vmauth/main.go b/app/vmauth/main.go index 3d6d85b09..30c33c61f 100644 --- a/app/vmauth/main.go +++ b/app/vmauth/main.go @@ -2,7 +2,6 @@ package main import ( "flag" - "fmt" "net/http" "net/http/httputil" "net/url" @@ -12,6 +11,7 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo" "github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup" "github.com/VictoriaMetrics/VictoriaMetrics/lib/envflag" + "github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil" "github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver" "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil" @@ -100,8 +100,5 @@ vmauth authenticates and authorizes incoming requests and proxies them to Victor See the docs at https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/app/vmauth/README.md . ` - - f := flag.CommandLine.Output() - fmt.Fprintf(f, "%s\n", s) - flag.PrintDefaults() + flagutil.Usage(s) } diff --git a/app/vmbackup/main.go b/app/vmbackup/main.go index 550e3fde8..2e008ad02 100644 --- a/app/vmbackup/main.go +++ b/app/vmbackup/main.go @@ -103,10 +103,7 @@ or local filesystem. Backed up data can be restored with vmrestore. See the docs at https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/app/vmbackup/README.md . ` - - f := flag.CommandLine.Output() - fmt.Fprintf(f, "%s\n", s) - flag.PrintDefaults() + flagutil.Usage(s) } func newSrcFS() (*fslocal.FS, error) { diff --git a/app/vminsert/main.go b/app/vminsert/main.go index 24ee38583..cb4b563a1 100644 --- a/app/vminsert/main.go +++ b/app/vminsert/main.go @@ -62,6 +62,7 @@ var ( func main() { // Write flags and help message to stdout, since it is easier to grep or pipe. flag.CommandLine.SetOutput(os.Stdout) + flag.Usage = usage envflag.Parse() buildinfo.Init() logger.Init() @@ -257,3 +258,12 @@ var ( return float64(atomic.LoadUint64(&storage.TooLongLabelValues)) }) ) + +func usage() { + const s = ` +vminsert accepts data via popular data ingestion protocols and routes it to vmstorage nodes configured via -storageNode. + +See the docs at https://victoriametrics.github.io/Cluster-VictoriaMetrics.html . +` + flagutil.Usage(s) +} diff --git a/app/vmrestore/main.go b/app/vmrestore/main.go index 0f0515bb4..895d37b97 100644 --- a/app/vmrestore/main.go +++ b/app/vmrestore/main.go @@ -62,10 +62,7 @@ vmrestore restores VictoriaMetrics data from backups made by vmbackup. See the docs at https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/app/vmrestore/README.md . ` - - f := flag.CommandLine.Output() - fmt.Fprintf(f, "%s\n", s) - flag.PrintDefaults() + flagutil.Usage(s) } func newDstFS() (*fslocal.FS, error) { diff --git a/app/vmselect/main.go b/app/vmselect/main.go index 1ee28c06a..4624f5959 100644 --- a/app/vmselect/main.go +++ b/app/vmselect/main.go @@ -60,6 +60,7 @@ func getDefaultMaxConcurrentRequests() int { func main() { // Write flags and help message to stdout, since it is easier to grep or pipe. flag.CommandLine.SetOutput(os.Stdout) + flag.Usage = usage envflag.Parse() buildinfo.Init() logger.Init() @@ -547,3 +548,12 @@ var ( alertsRequests = metrics.NewCounter(`vm_http_requests_total{path="/select/{}/prometheus/api/v1/alerts"}`) metadataRequests = metrics.NewCounter(`vm_http_requests_total{path="/select/{}/prometheus/api/v1/metadata"}`) ) + +func usage() { + const s = ` +vmselect processes incoming queries by fetching the requested data from vmstorage nodes configured via -storageNode. + +See the docs at https://victoriametrics.github.io/Cluster-VictoriaMetrics.html . +` + flagutil.Usage(s) +} diff --git a/app/vmstorage/main.go b/app/vmstorage/main.go index a48274009..5e5ba49cf 100644 --- a/app/vmstorage/main.go +++ b/app/vmstorage/main.go @@ -45,6 +45,7 @@ var ( func main() { // Write flags and help message to stdout, since it is easier to grep or pipe. flag.CommandLine.SetOutput(os.Stdout) + flag.Usage = usage envflag.Parse() buildinfo.Init() logger.Init() @@ -625,3 +626,12 @@ func jsonResponseError(w http.ResponseWriter, err error) { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, `{"status":"error","msg":%q}`, err) } + +func usage() { + const s = ` +vmstorage stores time series data obtained from vminsert and returns the requested data to vmselect. + +See the docs at https://victoriametrics.github.io/Cluster-VictoriaMetrics.html . +` + flagutil.Usage(s) +} diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 2c9fc06f3..3be27d898 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -5,6 +5,8 @@ * FEATURE: optimize Consul service discovery speed when discovering big number of services. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/574 * FEATURE: add `label_uppercase(q, label1, ... labelN)` and `label_lowercase(q, label1, ... labelN)` function to [MetricsQL](https://victoriametrics.github.io/MetricsQL.html) for uppercasing and lowercasing values for the given labels. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/936 +* FEATURE: do not print usage info for all the command-line flags when incorrect command-line flag is passed. Previously it could be hard reading the error message + about incorrect command-line flag because of too big usage info for all the flags. * BUGFIX: properly parse timestamps in OpenMetrics format - they are exposed as floating-point number in seconds instead of integer milliseconds unlike in Prometheus exposition format. See [the docs](https://github.com/OpenObservability/OpenMetrics/blob/master/specification/OpenMetrics.md#timestamps). diff --git a/lib/flagutil/usage.go b/lib/flagutil/usage.go new file mode 100644 index 000000000..0e1c56e48 --- /dev/null +++ b/lib/flagutil/usage.go @@ -0,0 +1,39 @@ +package flagutil + +import ( + "flag" + "fmt" + "os" + "strings" +) + +// Usage prints s and optional description for all the flags if -h or -help flag is passed to the app. +func Usage(s string) { + f := flag.CommandLine.Output() + fmt.Fprintf(f, "%s\n", s) + if hasHelpFlag(os.Args[1:]) { + flag.PrintDefaults() + } else { + fmt.Fprintf(f, `Run "%s -help" in order to see the description for all the available flags`+"\n", os.Args[0]) + } +} + +func hasHelpFlag(args []string) bool { + for _, arg := range args { + if isHelpArg(arg) { + return true + } + } + return false +} + +func isHelpArg(arg string) bool { + if !strings.HasPrefix(arg, "-") { + return false + } + arg = arg[1:] + if strings.HasPrefix(arg, "-") { + arg = arg[1:] + } + return arg == "h" || arg == "help" +}