|
| 1 | +// Copyright 2024 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +// Package flag defines standardised flag interactions for use with promslog |
| 15 | +// across Prometheus components. |
| 16 | +// It should typically only ever be imported by main packages. |
| 17 | + |
| 18 | +package flag |
| 19 | + |
| 20 | +import ( |
| 21 | + "strings" |
| 22 | + |
| 23 | + kingpin "github.com/alecthomas/kingpin/v2" |
| 24 | + |
| 25 | + "github.com/prometheus/common/promslog" |
| 26 | +) |
| 27 | + |
| 28 | +// LevelFlagName is the canonical flag name to configure the allowed log level |
| 29 | +// within Prometheus projects. |
| 30 | +const LevelFlagName = "log.level" |
| 31 | + |
| 32 | +// LevelFlagHelp is the help description for the log.level flag. |
| 33 | +var LevelFlagHelp = "Only log messages with the given severity or above. One of: [" + strings.Join(promslog.LevelFlagOptions, ", ") + "]" |
| 34 | + |
| 35 | +// FormatFlagName is the canonical flag name to configure the log format |
| 36 | +// within Prometheus projects. |
| 37 | +const FormatFlagName = "log.format" |
| 38 | + |
| 39 | +// FormatFlagHelp is the help description for the log.format flag. |
| 40 | +var FormatFlagHelp = "Output format of log messages. One of: [" + strings.Join(promslog.FormatFlagOptions, ", ") + "]" |
| 41 | + |
| 42 | +// AddFlags adds the flags used by this package to the Kingpin application. |
| 43 | +// To use the default Kingpin application, call AddFlags(kingpin.CommandLine) |
| 44 | +func AddFlags(a *kingpin.Application, config *promslog.Config) { |
| 45 | + config.Level = &promslog.AllowedLevel{} |
| 46 | + a.Flag(LevelFlagName, LevelFlagHelp). |
| 47 | + Default("info").HintOptions(promslog.LevelFlagOptions...). |
| 48 | + SetValue(config.Level) |
| 49 | + |
| 50 | + config.Format = &promslog.AllowedFormat{} |
| 51 | + a.Flag(FormatFlagName, FormatFlagHelp). |
| 52 | + Default("logfmt").HintOptions(promslog.FormatFlagOptions...). |
| 53 | + SetValue(config.Format) |
| 54 | +} |
0 commit comments