From 014dfcdbfe7d863b8387bf23c76127d017c85fde Mon Sep 17 00:00:00 2001 From: Luca Comellini Date: Mon, 18 Dec 2023 20:15:47 -0800 Subject: [PATCH] Add hints for promlog Signed-off-by: Luca Comellini --- promlog/flag/flag.go | 12 ++++++++---- promlog/log.go | 9 ++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/promlog/flag/flag.go b/promlog/flag/flag.go index c25d818d..260f2b11 100644 --- a/promlog/flag/flag.go +++ b/promlog/flag/flag.go @@ -14,6 +14,8 @@ package flag import ( + "strings" + kingpin "github.com/alecthomas/kingpin/v2" "github.com/prometheus/common/promlog" ) @@ -23,23 +25,25 @@ import ( const LevelFlagName = "log.level" // LevelFlagHelp is the help description for the log.level flag. -const LevelFlagHelp = "Only log messages with the given severity or above. One of: [debug, info, warn, error]" +var LevelFlagHelp = "Only log messages with the given severity or above. One of: [" + strings.Join(promlog.LevelFlagOptions, ", ") + "]" // FormatFlagName is the canonical flag name to configure the log format // within Prometheus projects. const FormatFlagName = "log.format" // FormatFlagHelp is the help description for the log.format flag. -const FormatFlagHelp = "Output format of log messages. One of: [logfmt, json]" +var FormatFlagHelp = "Output format of log messages. One of: [" + strings.Join(promlog.FormatFlagOptions, ", ") + "]" // AddFlags adds the flags used by this package to the Kingpin application. // To use the default Kingpin application, call AddFlags(kingpin.CommandLine) func AddFlags(a *kingpin.Application, config *promlog.Config) { config.Level = &promlog.AllowedLevel{} a.Flag(LevelFlagName, LevelFlagHelp). - Default("info").SetValue(config.Level) + Default("info").HintOptions(promlog.LevelFlagOptions...). + SetValue(config.Level) config.Format = &promlog.AllowedFormat{} a.Flag(FormatFlagName, FormatFlagHelp). - Default("logfmt").SetValue(config.Format) + Default("logfmt").HintOptions(promlog.FormatFlagOptions...). + SetValue(config.Format) } diff --git a/promlog/log.go b/promlog/log.go index 3ac7b3fd..4128db21 100644 --- a/promlog/log.go +++ b/promlog/log.go @@ -26,14 +26,17 @@ import ( "github.com/go-kit/log/level" ) +// This timestamp format differs from RFC3339Nano by using .000 instead +// of .999999999 which changes the timestamp from 9 variable to 3 fixed +// decimals (.130 instead of .130987456). var ( - // This timestamp format differs from RFC3339Nano by using .000 instead - // of .999999999 which changes the timestamp from 9 variable to 3 fixed - // decimals (.130 instead of .130987456). timestampFormat = log.TimestampFormat( func() time.Time { return time.Now().UTC() }, "2006-01-02T15:04:05.000Z07:00", ) + + LevelFlagOptions = []string{"debug", "info", "warn", "error"} + FormatFlagOptions = []string{"logfmt, json"} ) // AllowedLevel is a settable identifier for the minimum level a log entry