Skip to content

Commit

Permalink
Add hints for promlog (prometheus#556)
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Comellini <[email protected]>
  • Loading branch information
lucacome authored Dec 22, 2023
1 parent 686da34 commit f094368
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 8 additions & 4 deletions promlog/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package flag

import (
"strings"

kingpin "github.com/alecthomas/kingpin/v2"
"github.com/prometheus/common/promlog"
)
Expand All @@ -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)
}
3 changes: 3 additions & 0 deletions promlog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ var (
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
Expand Down

0 comments on commit f094368

Please sign in to comment.