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

Add hints for promlog #556

Merged
merged 1 commit into from
Dec 22, 2023
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
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